android 在ExpandableListView中自动滚动

xpcnnkqh  于 2022-11-20  发布在  Android
关注(0)|答案(8)|浏览(234)

我希望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         |
+=================+                      +=================+
pepwfjgg

pepwfjgg1#

ListView.setSelection(position)

这将滚动到选定的项目,当你点击组项目时调用它。

1u4esq0p

1u4esq0p2#

下面的代码是一个适合我的解决方案

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;        
}
2wnc66cl

2wnc66cl3#

这对我很有效。把它放到你的适配器里:

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

    listView.setSelectedGroup(groupPosition);
}
xoefb8l8

xoefb8l84#

这个对我有用

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);
vcirk6k6

vcirk6k65#

android:transcriptMode="disabled"属性从xml添加到ExpandibleListView标记中。这应该可以工作。

7lrncoxx

7lrncoxx6#

android:transcriptMode="disabled"设置为我的ExpandibleListView对我也起作用。将参数设置为“正常”,没有一个方法起作用(setSelectedGroupsetSelectionFromTop等)。
只有setSmoothScroll可以工作,但效果不太好。

nr9pn0ug

nr9pn0ug7#

下面的代码对我有用。希望它能有所帮助。使用下面的代码在onGroupExpand中实现OnGroupExpandListener

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

}

pb3s4cty

pb3s4cty8#

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

相关问题