ExpandableListView中的SwipeListView,有可能吗?

时间:2014-10-05 08:29:52

标签: android listview expandablelistview

我正在使用https://github.com/47deg/android-swipelistview创建包含可滑动项目的列表视图。我想知道是否可以将其应用于ExpandableListView以便可以刷换子元素。

1 个答案:

答案 0 :(得分:0)

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

  • 复制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>
    

  • 添加标记滑动:swipeGroupView到布局上的ExpandableSwipeListView的声明。例如:

-

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

-

//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;
}