2013年8月17日星期六

PopupWindow + ListView + OnItemClick invalid

Code is as follows:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);

button1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
// 生成ListView
ListView listview = new ListView(MainActivity.this);
String strs = new String;
strs = "asfsafdasdf";
strs = "桂 地要仍地沃尔夫仍";
strs = "asfsafdasdf";
ArrayAdapter<String> adapter=new
ArrayAdapter<String>(MainActivity.this,R.layout.common_poplist_item_single_textview,R.id.content,strs);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.common_poplist_item_single_textview, strs);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
System.out.println(position);
}
});

PopupWindow window = new PopupWindow(MainActivity.this);
// 设置宽高
window.setWidth(200);
window.setHeight(120);
// 填充
window.setContentView(listview);
// 外部可部
window.setOutsideTouchable(true);
// 加了下面这行,onItemClick才好用
window.setFocusable(true);
window.showAsDropDown(button1);

}
});
}


has been tested and found 2.3 OnItemClick can trigger on a real machine .. but in the real machine to test 4.1, 4.2 No response .. trouble you have to help me verify whether this is true, what is the solution.
--- --- Solution --------------------------------------------
click a button displayed directly popwindow like
------ Solution ------------------------------ --------------
you to write it out to try it
------ Solution -------------- ------------------------------
landlord, I at 4.1 effective ah, this is my code http://blog.csdn.net/xiaanming/article/details/9121383
------ For reference only -------------------------------------- -
you want to achieve this click event interface got
------ For reference only ------------------------ ---------------


listview.setOnItemClickListener(new OnItemClickListener() {

                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // TODO Auto-generated method stub
                        System.out.println(position);
                    }
                });


this count to achieve it
------ For reference only ---------------------------------------
, I think there's setOnItemClickListener write listview better on the inside should not okay
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 生成ListView
                ListView listview = new ListView(MainActivity.this);
                String strs = new String;
                strs = "asfsafdasdf";
                strs = "桂 地要仍地沃尔夫仍";
                strs = "asfsafdasdf";
                 ArrayAdapter<String> adapter=new
                 ArrayAdapter<String>(MainActivity.this,R.layout.common_poplist_item_single_textview,R.id.content,strs);
//                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.common_poplist_item_single_textview, strs);
                listview.setAdapter(adapter);
                listview.setOnItemClickListener(new OnItemClickListener() {

                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // TODO Auto-generated method stub
                        System.out.println(position);
                    }
                });
        final Button button1 = (Button) findViewById(R.id.button1);

        button1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
               

                PopupWindow window = new PopupWindow(MainActivity.this);
                // 设置宽高
                window.setWidth(200);
                window.setHeight(120);
                // 填充
                window.setContentView(listview);
                // 外部可部
                window.setOutsideTouchable(true);
                // 加了下面这行,onItemClick才好用
                window.setFocusable(true);
                window.showAsDropDown(button1);

            }
        });
    }

------ For reference only ----------------------------------- ----




ah, I have written this way really bad .. But my question is, OnItemClick event is not executed to ...
------ For reference only ---------------------------------------
tested, is invalid.
------ For reference only -------------------------------------- -


thanks .. comparing your code ...

layers of peeling ...

find me a damn TextView more attributes: android: textIsSelectable = "true"
Click listView
lead in Item not catch the click event of problems.
------ For reference only -------------------------------------- -

  
thanks .. comparing your code ...   
  
layers of peeling ...   
  
find me a damn TextView more attributes: android: textIsSelectable = "true"   
  Click listView
lead in Item not catch the click event of problems.  
Haha, that was it, TextView captured the focus of the future be used to give him when ListView item set android: descendantFocusability = "blocksDescendant" comparison method Caesar

1 条评论:

  1. You could read and write more than one byte at a time using a byte array.

    回复删除