Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

android - OnItemClickListener doesn't work with ListView item containing button

I have ListView with custom Adapter which supplies View to ListView in this way:

   public View getView(int position, View convertView, ViewGroup parent)
   {
        RelativeLayout.LayoutParams lineParams;
        RelativeLayout line=new RelativeLayout(context);

        TextView tv=new TextView(context);
        tv.setText("Text in postion="+i);
        lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lineParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        line.addView(tv, lineParams);
        lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);

        //checkbox
        CheckBox checkBox=new CheckBox(context);
        lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lineParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        line.addView(checkBox, lineParams);
        return line;
    }

And somewhere inside ListView there's setOnItemClickListener(), which should intercept item clicking events. My problem that, whenever I try to add checkbox to item - I don't get any responces from my ListView. If I skip CheckBox or any other Button it works.

I am really stuck with this problem, I have tried all kind of Layouts, aligning, wrapping and so on - useless. Looks like CheckBox interferes ListView item click events.

Any ideas how to overcome?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

just add this line into the item views instead of listView itself

android:focusable="false"

check more detail about this from Android custom ListView unable to click on items


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...