在ExpandableListView组项目的Andr​​oid LongClickListener [英] Android LongClickListener on ExpandableListView group items

查看:87
本文介绍了在ExpandableListView组项目的Andr​​oid LongClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了这个教程的帮助下ExpandableListView:链接。 据我所知,code或多或少和一直试图设置对用户组longclicklistener。

I created an ExpandableListView with the help of this tutorial: link. I understand the code more or less and been trying to set a longclicklistener on the groups.

有对孩子的项目一个setOnChildClickListener已经和我设法设置他们longclicklistener:

There is a setOnChildClickListener on the child items already and I managed to set a longclicklistener on them:

exList.setOnItemLongClickListener(new OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
          if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
              groupPosition = ExpandableListView.getPackedPositionGroup(id);
              childPosition = ExpandableListView.getPackedPositionChild(id);
//[....]
 return false;
      }
  });  

我怎样才能在组项目设置longclicklistener?

How can I set a longclicklistener on the group items?

我知道code是太难看了,所以我创建了一个示例项目,并上传到这里 。 这对儿童的无onlongclicklistener,因为这几乎是原从上述链接。 我想AP preciate如果有人可以帮助我。

I know the code is hard to read so I created a sample project and uploaded it to here. This has no onlongclicklistener on the childs, since this is almost the original from the above link. I would appreciate if someone could help me with this.

推荐答案

集团项目是所有项目的一个子集,所以上面的方法应该被称为在两种情况下。然后你会使用getPackedPositionType,按上述程序计算出,如果所选的项目是一个组,一个项目,或者为null。

Group items are a subset of all items, so the method above should be called in either case. You'd then use getPackedPositionType as above to figure out if the selected item is a group, an item, or null.

在code表示这将是:

The code for this would be:

exList.setOnItemLongClickListener(new OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
          int itemType = ExpandableListView.getPackedPositionType(id);

          if ( itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
              childPosition = ExpandableListView.getPackedPositionChild(id);
              groupPosition = ExpandableListView.getPackedPositionGroup(id);

              //do your per-item callback here
              return retVal; //true if we consumed the click, false if not

          } else if(itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
              groupPosition = ExpandableListView.getPackedPositionGroup(id);
              //do your per-group callback here
              return retVal; //true if we consumed the click, false if not

          } else {
              // null item; we don't consume the click
              return false;
          }
  });

如果这是一个组,您将使用getPackedPositionGroup如上获得该组的ID是长期pressed。如果它是一个项目,你会使用getPackedPositionGroup和getPackedPositionChild的组合。

If it's a group, you'll use getPackedPositionGroup as above to get the group ID that is being long-pressed. If it's an item, you'll use the combination of getPackedPositionGroup and getPackedPositionChild.

这篇关于在ExpandableListView组项目的Andr​​oid LongClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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