Listitem 单击不适用于复选框 Android [英] Listitem click doesn't work with checkboxes Android

查看:12
本文介绍了Listitem 单击不适用于复选框 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

EDIT (SOLVED) For the answer go to the bottom of the question.

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

I have a listview and I inflate some rows in it using a customadapter. The row being inflated contains a checkbox. I can maintain the checkbox's status, but the problem is, my list items are no more clickable. Here's my code:

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

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

I want the listitems to be clickable in case the user wants to click only a single item.

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

EDIT (on Akhil's request)- preann2.xml in layouts folder:

<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:

main.xml in layouts folder:

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

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

EDIT (SOLVED) Just add these properties to checkbox:

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

推荐答案

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

The solution mentioned in the question solves the problem:

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

AbsListView 检查 View#hasFocusable 并且不允许按下项目如果是这样.因此,禁用对所有通常可聚焦的项目的聚焦.

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

这篇关于Listitem 单击不适用于复选框 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆