List item with CheckBox not clickable

前端 未结 9 1857
误落风尘
误落风尘 2020-12-31 01:12

I have a list item which contains a CheckBox, and I want to be able to click on the CheckBox and on the list item itself. Unfortunately, there seems to be some sort of confl

相关标签:
9条回答
  • 2020-12-31 01:40

    insert this into the root element of the item row xml file

    android:descendantFocusability="blocksDescendants"
    
    0 讨论(0)
  • 2020-12-31 01:43

    Late, but nevertheless a solution for all those who still need it.

    It is true that the built-in mechanism using:

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        getActivity(),
        android.R.layout.simple_list_item_multiple_choice, lst);
    

    does not allow both, ie click on the checkbox AND click on the list item. Wherever one clicks, the checkbox catches the event.

    However, if you create your own ArrayAdapter with getView like this, then it works fine:

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item_ecu_fehler, null);
        }
    
        v.setFocusable(true);
        v.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            if (DEBUG)
                Log.i(this.getClass().getSimpleName(),
                    " ->>"
                        + Thread.currentThread().getStackTrace()[2]
                            .getMethodName());
            }
        });
    
                CheckBox selectedForClearingCB = (CheckBox) v
                .findViewById(R.id.checkBox);
    
    
            if (selectedForClearingCB != null) {
            selectedForClearingCB.setTag(position); //so we know position in the list       selectedForClearingCB.setChecked(true);
            selectedForClearingCB.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                if (((CheckBox) v).isChecked()) {
                    if (DEBUG)
                    Log.i(this.getClass().getSimpleName(),
                        " -> CB checked: "
                            + Integer.toString((Integer) v
                                .getTag()));
    
                }
    
                }
            });
            }
        }
        return v;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-31 01:46

    You need to add this to your custom adapter xml file android:descendantFocusability="blocksDescendants"

    0 讨论(0)
  • 2020-12-31 01:47

    As described here and here, this is either a known problem or works as designed. If you have any clickable or focusable items in a list item, the list item itself cannot be clickable. Romain Guy says "This is working as intended to support trackball/dpad navigation."

    0 讨论(0)
  • 2020-12-31 01:47

    I solved the problem this way. I implemented OnClickListener inside the Adapter and not in the Fragment/Activity and it works well. Now I can use ListView with checkboxes and can click on both. Here is my code:

    public class MyFragment extends Fragment
    {
        ...
    
        private void setView()
        {
            ListView listView = (ListView) mRootView.findViewById(R.id.listview);
            mItems = DatabaseManager.getManager().getItems();
    
            // create adapter
            if(listView.getAdapter()==null)
            {
                MyAdapter adapter = new MyAdapter(this, mItems);
                try
                {
                    listView.setAdapter(adapter);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    return;
                }
            } 
            else 
            {
                try
                {
                    ((MyAdapter) listView.getAdapter()).refill(mItems);
                    BaseAdapter adapter = (BaseAdapter) listView.getAdapter();
                    listView.requestLayout();
                    adapter.notifyDataSetChanged();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    return;
                }
            }
    
            // handle listview item click
            listView.setClickable(true);
            // listView.setOnItemClickListener(...); // this method does not work in our case, so we can handle that in adapter
        }
    
        ...
    }
    
    
    public class MyAdapter extends BaseAdapter
    {
        ...
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            View view = convertView;
            if (view == null) 
            {
                LayoutInflater inflater = (LayoutInflater) mFragment.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.listview_item, null);
            }
    
            ...
    
            // handle listview item click
            // this method works pretty well also with checkboxes
            view.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    // do something here
                    // for communication with Fragment/Activity, you can use your own listener
                }
            });
    
            return view;
        }
    
        ...
    }
    
    0 讨论(0)
  • 2020-12-31 01:52

    ok.. Make a CheckBox instance Like

    CheckBox check;
    check = (CheckBox) myView.findViewById(R.id.check);
    check.setOnClickListener(new CheckBoxSelect(position));
    

    put above code in onItemClickListener or in Adapter you are using. now make a class like below

    private class CheckBoxSelect implements OnClickListener 
        {
            int pos;
            String str;
    
            public CheckBoxSelect(int position) 
            {
                pos = position;
            }
    
            public void onClick(View v) 
            {
    
            }
    
        }
    

    perform any functionality in onClick .

    0 讨论(0)
提交回复
热议问题