检测ExpandableListView组中的单击图像

时间:2014-07-10 13:16:21

标签: android expandablelistview

我的group_row中有一张图片

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#8fbfff" >

<TextView
    android:id="@+id/tv_category_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:text="TextView"
    android:layout_weight="1" />

<ImageView
    android:id="@+id/img_add_category"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ico_add_item"
    android:layout_weight="1"
    android:layout_gravity="center_vertical"
    android:layout_marginRight="10dp" />

</LinearLayout>

我想检测单击此图像的时间和组的ID,然后是StartActivityForResult。我能够在我的自定义ExpandableListView_Adapter中执行此操作:

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

    final Category category = arr_categories.get(groupPosition);

    convertView = inflater.inflate(R.layout.row_category, parent, false);
    ((TextView)convertView.findViewById(R.id.tv_category_name)).setText(category.getCategory_name());
    ImageView img_add = (ImageView) convertView.findViewById(R.id.img_add_category);
    img_add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(context, "Add item to category: "+category.getCategory_id(), Toast.LENGTH_LONG).show();

            // Declare on intent to use Add/Edit Note activity
            Intent open_add_new_item = new Intent(context, Activity_Add_Edit_Base_Item.class);
            // Pass the currently selected category ID to the Intent
            open_add_new_item.putExtra("CURR_ITEM_CATEGORY", category.getCategory_id());
            // Start the activity
            ((Activity) context).startActivityForResult(open_add_new_item, Constants.Request_Codes.REQUEST_CODE_CREATE_NEW_ITEM);
        }
    });
    return convertView;
}

这很好用。但是,如果有可能这样做,我想将此图像点击检测与适配器分开并在我的片段中执行(我认为这样更容易实现OnActivityResult) 。我尝试通过为我的ExpandableListView设置OnGroupClickListener来实现它:

master_lv.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            final Category c = arr_all_categories.get(groupPosition);
            ImageView img_add = (ImageView) v.findViewById(R.id.img_add_category);
            img_add.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    //Category c = arr_all_categories.get(groupPosition);
                    Toast.makeText(getActivity(), "group position: "+c.getCategory_id(), Toast.LENGTH_LONG).show();

                    // Declare on intent to use Add/Edit Note activity
                    Intent open_add_new_item = new Intent(getActivity(), Activity_Add_Edit_Base_Item.class);
                    // Pass the currently selected category ID to the Intent
                    open_add_new_item.putExtra("CURR_ITEM_CATEGORY", c.getCategory_id());
                    // Start the activity
                    startActivityForResult(open_add_new_item, Constants.Request_Codes.REQUEST_CODE_CREATE_NEW_ITEM);

                }
            });
            return false;
        }
    });

然而,这根本不起作用:我没有收到Toast消息,并且意图没有触发。

有可能这样做吗?如果是 - 怎么样?谢谢!

1 个答案:

答案 0 :(得分:1)

您需要做的第一件事是将clickable=true设置为自定义单元格xml的根布局。

之后,我们要做的是自定义事件提升。我们将使用接口。

创建一个接口类

示例:

public interface OnImageClickListener { public void onImageClicked(); }

然后在适配器

中创建一个实例

public OnImageClickListener mListener;

还在适配器的getView方法中将OnClickListener设置为imageview,并在OnClick方法中添加以下行。

mListener.OnImageClicked();

最后,在Activity;

mAdapter.mListener = new OnImageClickListener(); 魔法会在这里发生:)

或者你可以实现这个界面,如

public MyActivity implements OnItemClickListener并让实现方法。

然后你可以

mAdapter.mListener = this;

祝你好运:)