gpt4 book ai didi

android - ExpandableListView中的SwipeListView,可以吗?

转载 作者:太空狗 更新时间:2023-10-29 15:01:24 25 4
gpt4 key购买 nike

我正在使用 https://github.com/47deg/android-swipelistview创建带有可滑动项目的 ListView 。我想知道是否可以将此应用到 ExpandableListView 以便可以滑动子元素。

最佳答案

我已经能够让它发挥作用(至少对我而言)。以下是我采取的步骤列表:

  • 复制 SwipeListView 类并重命名为 ExpandableSwipeListView。
  • 让它从 ExpandableListView 扩展。
  • 复制 SwipeListViewTouchListener 类并重命名为 ExpandableSwipeListViewTouchListener
  • 将 ExpandableSwipeListView 内部的 SwipeListViewTouchListener 调用更改为 ExpandableSwipeListViewTouchListener。
  • 将 SwipeListViewTouchListener 内的 SwipeListView 调用更改为 ExpandableSwipeListView。
  • 将 ExpandableSwipeListViewTouchListener 的方法 resetItems 更改为:

-

/**
* Adds new items when adapter is modified
*/
public void resetItems() {
ExpandableListAdapter adp=swipeListView.getExpandableListAdapter();
if (adp != null) {
int count = 0;
for (int i=0; i<adp.getGroupCount();i++){
//Add the total children and the group itself.
count+=adp.getChildrenCount(i) + 1;
}
for (int i = opened.size(); i <= count; i++) {
opened.add(false);
openedRight.add(false);
checked.add(false);
}
}
}
  • 使用以下内容在 res/values 上创建 expandableswipelistview__attrs.xml:

    <declare-styleable name="ExpandableSwipeListView">
    <attr name="swipeOpenOnLongPress"/>
    <attr name="swipeAnimationTime"/>
    <attr name="swipeOffsetLeft"/>
    <attr name="swipeOffsetRight"/>
    <attr name="swipeCloseAllItemsWhenMoveList"/>
    <attr name="swipeFrontView"/>
    <attr name="swipeBackView"/>
    <attr name="swipeGroupView" format="reference"/>
    <attr name="swipeMode"/>
    <attr name="swipeActionLeft"/>
    <attr name="swipeActionRight"/>
    <attr name="swipeDrawableChecked"/>
    <attr name="swipeDrawableUnchecked"/>
    </declare-styleable>

  • 将标签 swipe:swipeGroupView 添加到布局上 ExpandableSwipeListView 的声明中。示例:

-

swipe:swipeGroupView="@+id/group"
  • id 应该是唯一的,并且必须在您的组的布局上声明。
  • 在 ExpandableSwipeListView 的初始化方法中,将所有样式更改为“ExpandableSwipeListView_...”,并在“obtainStyledAttributes”中将其设置为 R.styleable.ExpandableSwipeListView。
  • 在 init 方法上添加 swipeGroupView,如 swipeFrontView,并将其传递给 ExpandableSwipeListViewTouchListener 构造函数。
  • 在 ExpandableSwipeListViewTouchListener 上,在“if (allowSwipe && rect.contains(x, y)) {”之后添加以下代码:

-

//verify if it is a group:
if (child.findViewById(swipeGroupView)!=null){
return false;
}
  • 将这些方法添加到 ExpandableSwipeListView。它们有助于进行取消回调和其他操作:

-

/**
* Returns the group and child positions for a child element.
* This values are passed inside an array of dimension 2 where the index 0 is the group position and the index 1 is the child position.
* @param general_position used on the list (compatible with getChildAt)
* @return int[2] 0 => group position; 1 => child position
*/
public int[] getGroupAndChildPositions(int general_position){
//get group and child ids
int groupPosition=0;
int childPosition=0;
int helper=general_position;
for (int i=0;i<getExpandableListAdapter().getGroupCount();i++){
if (helper-getExpandableListAdapter().getChildrenCount(i)-1<=0){
groupPosition=i;
childPosition=helper-1;
break;
} else {
helper-=getExpandableListAdapter().getChildrenCount(i)+1;
}
}
return new int[]{groupPosition,childPosition};
}

/**
* Returns the general position of an element on the list (used by getChildAt)
* @param groupPosition
* @param childPosition
* @return the position on the list
*/
public int getGeneralPosition(int groupPosition, int childPosition){
int position=0;
for (int i=0;i<=groupPosition;i++){
if (i<groupPosition)
position+=getExpandableListAdapter().getChildrenCount(i)+1;
else{
position+=childPosition+1;
}
}
return position;
}

关于android - ExpandableListView中的SwipeListView,可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26200952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com