在ExpandableListView自动滚屏(Auto scrolling in Expandab

2019-09-23 15:57发布

我想我的ExpandableListView当用户展开一个组,使得膨胀组头位于屏幕顶部的自动滚动。 我试过smoothScrollToPosition ,但是这仅仅是保证了扩展群是可见的地方在屏幕上。 我想明确地滚动它,因此扩展群是在顶部,就像这个例子:

Before expanding Group 3:                After expanding Group 3:

+=================+                      +=================+
| Group 1         |                      | Group 3         |
+-----------------+                      +-----------------+
| Group 2         |                      |   Grp 3 Child 1 |
+-----------------+                      +-----------------+
| Group 3         |                      |   Grp 3 Child 2 |
+-----------------+                      +-----------------+
| Group 4         |                      | Group 4         |
+=================+                      +=================+

Answer 1:

ListView.setSelection(position)

这将滚动到所选的项目,把这个当U点击组项目。



Answer 2:

下面的代码是为我工作的解决方案

public boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {
    // TODO Auto-generated method stub
    //mExpandableList.setSelectionFromTop(groupPosition, 0);

Boolean shouldExpand = (!mExpandableList.isGroupExpanded(groupPosition));        
    mExpandableList.collapseGroup(lastClickedPosition);

    if (shouldExpand){
        //generateExpandableList();
        mExpandableList.expandGroup(groupPosition);
        mExpandableList.setSelectionFromTop(groupPosition, 0);
    }                
    lastClickedPosition = groupPosition;
    return true;        
}


Answer 3:

这为我工作。 把它放在你的适配器:

public void onGroupExpanded(final int groupPosition) {
    super.onGroupExpanded(groupPosition);

    listView.setSelectedGroup(groupPosition);
}


Answer 4:

这个属性添加android:transcriptMode="disabled" ,以从XML您ExpandibleListView标签。 这应该工作。



Answer 5:

这一个是为我工作

expandList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

            if (!parent.isGroupExpanded(groupPosition)) {
                parent.expandGroup(groupPosition);
            } else {
                parent.collapseGroup(groupPosition);
            }
            parent.setSelectedGroup(groupPosition);

            return true;
        }
    });

至于滚动的主要工作部件是

parent.setSelectedGroup(groupPosition);


Answer 6:

设置android:transcriptMode="disabled"ExpandibleListView工作对我来说太。 置为“正常”的参数,没有一个方法的工作( setSelectedGroupsetSelectionFromTop等)。

只有setSmoothScroll作品,但不喜欢的效果。



Answer 7:

下面的代码适用于me.Hope将helps.Implements的OnGroupExpandListener内onGroupExpand使用下面的代码

public void onGroupExpand(final int groupPosition) {
super.onGroupExpand(groupPosition);

expandableListView.post(new Runnable() {

    @Override
    public void run() {
        expandableListView.setSelection(groupPosition);
        if(expandableListView.getChildAt(groupPosition)!=null)
        expandableListView.requestChildRectangleOnScreen(expandableListView.getChildAt(groupPosition),
                new Rect(0, 0, expandableListView.getChildAt(groupPosition).getRight(), expandableListView.getChildAt(groupPosition).getHeight()), false);
    }
});

}



Answer 8:

setSelectedGroup工作,但如果你想有一个平滑滚动的效果,使用smoothScrollToPositionFromTop ,如下所示:

    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            parent.smoothScrollToPositionFromTop(groupPosition,0);
            if (expandableListView.isGroupExpanded(groupPosition))
                expandableListView.collapseGroupWithAnimation(groupPosition);
            else expandableListView.expandGroupWithAnimation(groupPosition);
            return true;
        }
    });


文章来源: Auto scrolling in ExpandableListView