【问题标题】:Block expanding of Expandable List View on long click长按时阻止可扩展列表视图的扩展
【发布时间】:2016-08-05 19:36:41
【问题描述】:

我正在开发一个 android 应用程序,我在其中使用 Expandablelistview 来显示一些数据。目前,列表视图将在单击组和长按后释放时展开。但是,我需要防止在长按时扩展Expandablelistview

我的代码段如下所示:

elvItemList = (ExpandableListView) root.findViewById(R.id.elv_item_list);
elvItemList.setOnGroupClickListener(this);
elvItemList.setAdapter(smListAdapter);
elvItemList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            Utils.logit("SMLOG", "Long button pressed");
            //
        }
        return false;
    }
});

谁能帮帮我?

【问题讨论】:

    标签: java android android-studio expandablelistview onitemlongclicklistener


    【解决方案1】:

    ExpandableListView.PACKED_POSITION_TYPE_GROUP是组的id,改成 ExpandableListView.PACKED_POSITION_TYPE_CHILD,您可以通过长按组子项进行操作。

    类似的东西:

    elvItemList.setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
    // Your code with group long click
    return true;
    }
    return false;
    }
    });
    

    【讨论】:

      【解决方案2】:
      public void stopExpandOnLongClick()
      {
          expListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
              @Override
              public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                  boolean checkClick=false;
                  long packedPosition = expListView.getExpandableListPosition(position);
      
                  int itemType = ExpandableListView.getPackedPositionType(packedPosition);
                  int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
      
                   /*  if group item clicked */
                  if (itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
                      //  ...
                      if(expListView.isGroupExpanded(groupPosition))
                      {
                          Toast.makeText(MainActivity.this, "It's normal group collaspe", Toast.LENGTH_LONG).show();
                      }
                      else
                      {
                          Toast.makeText(MainActivity.this, "This Grouo is not going to expand on long click", Toast.LENGTH_LONG).show();
                          //you can put your logic when you long press on Group header
                          checkClick=true;
                      }
                  }
                  return checkClick;
              }
          });
      
      }
      

      【讨论】:

        【解决方案3】:

        这对我有用。

        expandableListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        
                    return true; // Change return false; to return true;
                }
            });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多