添加复选框可以列出行失去了我的onItemClick事件?我的、复选框、失去了、事件

2023-09-13 00:09:56 作者:丶谁解释我不懂的小情绪ヾ

我有一个ArrayList适配器的ListView。行不是很复杂(在左侧,与内部TextViews一个的LinearLayout,并在右边一个CheckBox图像...的布局被复制在下面)的目标是有一个QuickAction栏上来如果用户点击的图像或文字,并在有复选框的变化状态,如果用户点击的复选框。我的每一部分独立工作,但是当他们不在一起的布局 - 不知何故,我失去了onItemClick事件。

I have a ListView with an ArrayList adapter. The rows are not very complex (an Image on the left, a LinearLayout with TextViews inside, and a CheckBox on the right ... the layout is copied in below.) The goal is to have a QuickAction bar come up if the user clicks on the image or text, and have the CheckBox change state if the user clicks on the CheckBox. I have each part working independently, but not when they're together in the layout - somehow, I'm losing the onItemClick event.

在QuickAction栏由OnItemClickListener()被激活,并能正常工作 - 除非我有复选框的布局,在这种情况下复选框正常工作(使用的onClick()),但onItemClickListener如果是从来没有发射用户点击该行,但复选框之外。布局(减去一些风格的东西)是:

The "QuickAction" bar is activated by OnItemClickListener(), and it works fine - unless I have the CheckBox in the layout, in which case the CheckBox works fine (using onClick()) but the onItemClickListener is never fired if the user clicks in the row but outside the CheckBox. The layout (minus some style stuff) is:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vw1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView android:id="@+id/img"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <CheckBox android:id="@+id/ckb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_gravity="right"
    android:checked="false" />

<!-- stack text in middle section of the row -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <TextView android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content""/>

        <TextView android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</RelativeLayout>

其背后的code并不复杂:

The code behind it isn't complicated:

public class ListGroupsActivity extends BaseActivityForList {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        // add QuickAction bar
        ListView lv = getListView();
        lv.setOnItemClickListener(new QAListener(this));
    }
}

    //inner classes of the ListGroupsActivity
    class CbOnClickListener implements OnClickListener {
        // ...
        // test the checkbox isChecked() and keep state in 'selectedItems' array

    class GroupListAdapter extends ArrayAdapter<Group> {
        super(/*...*/)
    CbOnClickListener cblistener = null; // common listener for all the CheckBoxes

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // here do normal testing for convertView being null etc 
            // heres the view inflation from the layout into the ViewHolder
            holder.tv1 = (TextView) convertView.findViewById(R.id.text1);
            holder.tv2 = (TextView) convertView.findViewById(R.id.text2);
            holder.img = (ImageView) convertView.findViewById(R.id.img);
            holder.ckb = (CheckBox) convertView.findViewById(R.id.ckb);

            Group g = groups.get(position); // this is the data obj for the row

            holder.tv1.setText(g.getName());
            holder.tv2.setText("child groups");
            Bitmap bm = BitmapFactory.decodeFile(g.getIconUri());
            holder.img.setImageBitmap(bm);
            Integer key = (Integer) g.getId();
            holder.ckb.setChecked(selectedItems.contains(key)); 

            holder.ckb.setOnClickListener(cblistener); // hook onClick to cblistener
            return convertView;
    }

该QAListener是一个超我的所有列表活动:

The QAListener is in a superclass for all my list activities:

public class BaseActivityForList extends
    OrmLiteBaseListActivity<DatabaseHelper> {

    // QAListener inner class, constructs new quick action bar when item clicked
    class QAListener implements OnItemClickListener {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // has three ActionItem instances, 'copyAction', 'delAction', 'editAction'
            ActionItem copyAction = new ActionItem();

            copyAction.setOnClickListener(new OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     // ... does some stuff
                     mQuickAction.dismiss();
                 }
             }); // end setOnClickListener
        } // end onItemClick
    } // end QAListener
} end BaseActivityForList

所以,我怎么安排的复选框拿起onClick的,而列表项的其余拿起onItemClick()?

So, how do I arrange for the Checkbox to pick up the onClick, and the rest of the list item to pick up the onItemClick()?

推荐答案

几个小时的调试和研究之后。我得到了它正常工作 该复选框设置以下属性:

After few hours of debugging and research. I got it working by setting following properties on the checkbox:

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

另外的ImageView可能需要配置为 http://blog.sachin.name/?p=62(感谢那个人)

在我的身边,我发现项目视图必须不点击和文本的意见不应该点击,无论是。我花了一段时间才能认识到,我在code称为setClickable(真)在我的项目视图的地方。

On my side, I discovered that item view MUST NOT be clickable and text views should not be clickable, either. It took me a while to realize that I called setClickable(true) on my item view somewhere in the code.

 
精彩推荐
图片推荐