我正在使用https://github.com/47deg/android-swipelistview创建包含可滑动项目的列表视图。我想知道是否可以将其应用于ExpandableListView
以便可以刷换子元素。
答案 0 :(得分:0)
我能够让它发挥作用(至少对我而言)。这是我采取的步骤列表:
-
/**
* 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"
-
//verify if it is a group:
if (child.findViewById(swipeGroupView)!=null){
return false;
}
-
/**
* 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;
}