Listitem点击不适用于Android复选框

时间:2013-04-07 05:16:38

标签: android listview checkbox android-listview onitemclicklistener

编辑(已解决)关于答案,请转到问题的底部。

我有一个listview,并使用customadapter在其中填充了一些行。 被充气的行包含一个复选框。我可以保持复选框的状态,但问题是,我的列表项不再可点击。 这是我的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    fl=(ListView)findViewById(R.id.MainMenu);//Mainmenu is listview
    fl.setAdapter(new CustomAdapter(Annotate.this,R.layout.preann2,_dir));//preann2 is row and _dir is the list of objects
    //fl.setOnItemClickListener(this); tried this too
}

public void onItemClick(AdapterView<?> a, View view, int pos, long id) 
{
      //Implementations
}

    public class CustomAdapter extends ArrayAdapter<String>
{
    public CustomAdapter(Context context, int textViewResourceId,String[] objects) {
        super(context, textViewResourceId,objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.preann2, parent, false);
            try
            {

            TextView Name=(TextView)row.findViewById(R.id.title);
            Name.setText("xyz");
            ImageView icon=(ImageView)row.findViewById(R.id.icon);
            row.setFocusable(true);
            row.setClickable(true);
            row.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    checkState[position]=!checkState[position];
                }

            });
            CheckBox c=(CheckBox)row.findViewById(R.id.checkBox);
            c.setChecked(checkState[position]);
            c.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                @Override
                public void onCheckedChanged(CompoundButton v,
                        boolean isChecked) {
                    // TODO Auto-generated method stub
                    CheckBox checkBox=(CheckBox)v;
                    if (isChecked) {
                        checkState[position]=true;
                     }
                    else
                        checkState[position]=false;
                }
            });
            /*  tried this too
                                  c.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    CheckBox checkBox=(CheckBox)v;
                    if (checkBox.isChecked()) {
                        //checkBox.setChecked(false);
                        checkState[position]=true;
                     }
                    else
                        checkState[position]=false;
                }});*/
            icon.setImageResource(R.drawable.ic_launcher);
            }catch(Throwable err)
            {
                err.printStackTrace();
            }
            return row;
        }
}

我希望listitems可以点击,以防用户只想点击一个项目。

编辑(根据Akhil的要求) - layouts文件夹中的preann2.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:padding="4dip"
  >

 <ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="4dip"/>


  <TextView
  android:id="@+id/title"
  android:layout_width="fill_parent"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:ellipsize="end"
  android:gravity="center_vertical"
  android:singleLine="true"
  android:textStyle="bold" />

<CheckBox
  android:id="@+id/checkBox"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

</LinearLayout>

layouts文件夹中的main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<ListView
    android:id="@+id/MainMenu"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</ListView>

</LinearLayout>

编辑(已解决) 只需将这些属性添加到复选框:

  android:focusable="false"
  android:focusableInTouchMode="false"

3 个答案:

答案 0 :(得分:17)

问题中提到的解决方案解决了这个问题:

android:focusable="false"
android:focusableInTouchMode="false"

AbsListView checks View#hasFocusable and won't allow presses on the item if so. Thus, disable focus on all normally focusable items.

答案 1 :(得分:5)

在列表项的rootView中使用此行 机器人:descendantFocusability = “blocksDescendants”

答案 2 :(得分:0)

我确实误解了您的问题.. please have a look here ,因为您的问题..希望它能解决您的问题..