我使用https://github.com/47deg/android-swipelistview创建一个包含可滑动项的列表视图。我想知道是否可以将此应用于ExpandableListView
以便可以刷取子元素。
最佳答案
我已经成功了(至少对我来说)。以下是我采取的步骤:
复制SwipeListView类并重命名为ExpandableSwipeListView。
使其从ExpandableListView扩展。
复制SwipeListViewTouchListener类并重命名为ExpandableSwipeListViewTouchListener
将ExpandableSwipeListView中的SwipeListViewTouchListener调用更改为ExpandableSwipeListViewTouchListener。
将SwipeListViewTouchListener内的SwipeListView调用更改为可扩展的SwipeListView。
将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\uu 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添加到布局上可展开swipeListView的声明中。例子:
-
swipe:swipeGroupView="@+id/group"
ID应该是唯一的,并且必须在组的布局上声明。
在expandableswipelistview的init方法中,将所有样式表更改为“expandableswipelistview”,在“获取样式属性”中将其设置为r.styleable.expandableswipelistview。
在init方法上添加类似swipeFrontView的swipeGroupView,并将其传递给expandableswipeListViewTouchListener构造函数。
在ExpandableSwipeListViewTouchListener上,在“if(allowswiep&&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/