CoordinatorLayout+NestedScrollView+ExpandableListView滚动到ExpandableListView的指定位置

appBarLayout.setExpanded(false, true);//先缩起AppBarLayout
//然后滚动NestedScrollView
scroll.post(new Runnable() {
@Override
public void run() {
scroll1(expandableListView, finalI, finalJ);
}
});
 public void scroll1(ExpandableListView expandableListView,int groupIndex, int childIndex) {
int totalHeight = 0;
int position = 0;
if(groupIndex==0)
{

View group = expandableListView.getExpandableListAdapter().getGroupView(0, true, null, expandableListView);
//测量高度,这里把子item高度和父item高度视为一样高,不一样高的需要重新测量child的高度
group.measure(0, 0);
totalHeight += group.getMeasuredHeight() ;
}
else
{
for (int i = 0; i < groupIndex; i++) {
position++;
View group = expandableListView.getExpandableListAdapter().getGroupView(i, true, null, expandableListView);
//测量高度
                group.measure(0, 0);
totalHeight += group.getMeasuredHeight() ;
if (expandableListView.isGroupExpanded(i)){
position = position + expandableListView.getExpandableListAdapter().getChildrenCount(i);
}
}
}
position++;
position = position + childIndex;

scroll.smoothScrollTo(0, position*totalHeight);


}
参考https://*.com/questions/53474588/scrolltoposition-smoothscrolltoposition-for-recyclerview-under-a-nestedscrollvi
https://blog.csdn.net/weixin_39694016/article/details/117679934


上一篇:NestedScrollView嵌套小视频app源码的WebView导致底部有空白


下一篇:【Android+Kotlin】NestedScrollView