【问题标题】:Listitem click doesn't work with checkboxes AndroidListitem 点击不适用于复选框 Android
【发布时间】:2013-03-29 08:33:11
【问题描述】:

编辑(已解决)要获得答案,请转到问题的底部。

我有一个列表视图,我使用自定义适配器在其中膨胀了一些行。 被膨胀的行包含一个复选框。我可以保持复选框的状态,但问题是,我的列表项不再可点击。 这是我的代码:

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;
        }
}

我希望列表项可以点击,以防用户只想点击单个项目。

编辑(应 Akhil 的要求)- 布局文件夹中的 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>

布局文件夹中的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"

【问题讨论】:

  • 如果您希望在单击该行时检查复选框,则将条件与onClickListener() 一起用于该行本身。
  • 当我单击列表视图中的项目时,我希望执行一些其他操作。我可以通过单击它们直接设置复选框,因此存在问题。当我点击一个项目时,我实际上想调用一个活动。
  • 尝试在onClickListener 中为复选框添加log,并让我们知道日志是否已注册。
  • 我解决了。只需将复选框的可聚焦和 focusonTouchMode 属性设置为 false。不管怎样,谢谢。
  • 请回答您自己的问题并在 2 天后接受。以后可能会帮助别人。

标签: android listview checkbox android-listview onitemclicklistener


【解决方案1】:

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

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.

【讨论】:

    【解决方案2】:

    在列表项的 rootView 中使用这一行
    android:descendantFocusability="blocksDescendants"

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 我也这样做了。一个布局中的文本视图、图标和复选框。主菜单列表视图在另一个布局中。
      • 是的,我明白了..但是我在您的 ListItem 布局中所说的话将您的 TextView 和图标保持在一个布局中,并以与 Checkbox 相同的方式在该布局上设置侦听器
      • 我已将布局共享为编辑。你能告诉我怎么做吗..?请问?
      • 我解决了。只需将复选框的可聚焦和 focusonTouchMode 属性设置为 false。不管怎样,谢谢。
      猜你喜欢
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 2019-10-12
      • 2013-06-18
      • 1970-01-01
      相关资源
      最近更新 更多