SwipeListView to ExpandableListView, is it possible?

I am using https://github.com/47deg/android-swipelistview to create a list view with drawable items. I'm wondering if this can be applied to ExpandableListView

so that the children can scroll.

+3


source to share


1 answer


I was able to get it to work (at least for me). Here is a list of the steps I took:

  • Copy class SwipeListView and rename ExpandableSwipeListView.
  • Do it from ExpandableListView.
  • Copy class SwipeListViewTouchListener and rename to ExpandableSwipeListViewTouchListener
  • Change the SwipeListViewTouchListener call inside ExpandableSwipeListView to ExpandableSwipeListViewTouchListener.
  • Change SwipeListView calls inside SwipeListViewTouchListener for ExpandableSwipeListView.
  • Change the resetItems ExpandableSwipeListViewTouchListener method to this:

-

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

      

  • Create expandableswipelistview__attrs.xml file by res / values ​​as follows:

    <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>
    
          

  • Add swipe: swipeGroupView tag to your ExpandableSwipeListView declaration on layout. Example:

-

swipe:swipeGroupView="@+id/group"

      



  • The ID must be something unique and must be declared in the layout of your groups.
  • In the init method of the ExpandableSwipeListView method, change all the styles for "ExpandableSwipeListView _..." and for "getStyledAttributes" set it to R.styleable.ExpandableSwipeListView.
  • Add a swipeGroupView like swipeFrontView to the init method and pass it to the ExpandableSwipeListViewTouchListener constructor.
  • In the ExpandableSwipeListViewTouchListener add the following code after "if (allowSwipe && rect.contains (x, y)) {":

-

//verify if it is a group:
if (child.findViewById(swipeGroupView)!=null){
    return false;
}

      

  • Add these methods to ExpandableSwipeListView. They are useful for canceling callbacks and other things:

-

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

      

0


source







All Articles