Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android smoothScrollToPosition在ExpandableListView中的位置_Android_Scroll_Expandablelistview_Android Animation - Fatal编程技术网

Android smoothScrollToPosition在ExpandableListView中的位置

Android smoothScrollToPosition在ExpandableListView中的位置,android,scroll,expandablelistview,android-animation,Android,Scroll,Expandablelistview,Android Animation,我试图使用该方法滚动到ExpandableListView的底部,但失败了,因为我不知道如何计算position属性 都不是 adapter.getGroupCount() 也不是 也不是 成功了 TLDR:如何在API级别8+上平滑滚动到ExpandableListView的底部 expandableListView.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean o

我试图使用该方法滚动到ExpandableListView的底部,但失败了,因为我不知道如何计算position属性

都不是

adapter.getGroupCount()
也不是

也不是

成功了

TLDR:如何在API级别8+上平滑滚动到ExpandableListView的底部

expandableListView.setOnGroupClickListener(new OnGroupClickListener() {

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v,
            int groupPosition, long id) {
        // TODO Auto-generated method stub
        parent.smoothScrollToPosition(groupPosition);
        return true;
    }
});
ExpandableListView.post以等待列表完成加载项目。 ExpandableListView.getCount以获取项目组和子项的计数

提供持续时间,以便列表不会快速滚动到API 11要求的方法 smoothScrollToPositionFromTopint位置、int偏移量、int持续时间

ExpandableListView.post以等待列表完成加载项目。 ExpandableListView.getCount以获取项目组和子项的计数

提供持续时间,以便列表不会快速滚动到API 11要求的方法
smoothScrollToPositionFromTopint position,int offset,int duration

您计算的位置为:

public int getLastIndex() {
    int count = 0;
    for (int g = 0; g < getGroupCount(); g++)
        count += getChildrenCount(g) + 1;
    return count;
}
但是,如果在Activity类的onCreate方法中调用smoothScrollToPosition方法,则应在屏幕上显示UI后使用处理程序调用smoothScrollToPosition方法:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    .....
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            expandableListView.smoothScrollToPosition(getLastIndex());
        }
    },1000);
}

您将被计算到以下位置:

public int getLastIndex() {
    int count = 0;
    for (int g = 0; g < getGroupCount(); g++)
        count += getChildrenCount(g) + 1;
    return count;
}
但是,如果在Activity类的onCreate方法中调用smoothScrollToPosition方法,则应在屏幕上显示UI后使用处理程序调用smoothScrollToPosition方法:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    .....
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            expandableListView.smoothScrollToPosition(getLastIndex());
        }
    },1000);
}

要使用的位置取决于是否扩展每个组。如果组已展开,请将其所有子项添加到位置计数中。否则,不要

int getPosition(ExpandableListView listView, ExpandableListViewAdapter adapter) {
    int position = 0;
    for (int group = 0; group < adapter.getGroupCount(); ++group) {
        position += 1;
        if (listView.isGroupExpanded(group)) {
            position += adapter.getChildrenCount(group);
        }
    }
}

使用此位置会将您带到列表视图的底部。

要使用的位置取决于每个组是否展开。如果组已展开,请将其所有子项添加到位置计数中。否则,不要

int getPosition(ExpandableListView listView, ExpandableListViewAdapter adapter) {
    int position = 0;
    for (int group = 0; group < adapter.getGroupCount(); ++group) {
        position += 1;
        if (listView.isGroupExpanded(group)) {
            position += adapter.getChildrenCount(group);
        }
    }
}

使用此位置应将您带到列表视图的底部。

在什么上下文中尝试调用此方法,我的意思是在什么情况下您希望实现此功能?在什么上下文中尝试调用此方法,我的意思是在什么情况下您希望实现此功能?应滚动到单击的项目,不到最后一个位置。否?这应该滚动到单击的项目,而不是最后一个位置。否?谢谢提示,但在初始化阶段不会调用smoothScrollToPosition。谢谢提示,但在初始化阶段不会调用smoothScrollToPosition。
int getPosition(ExpandableListView listView, ExpandableListViewAdapter adapter) {
    int position = 0;
    for (int group = 0; group < adapter.getGroupCount(); ++group) {
        position += 1;
        if (listView.isGroupExpanded(group)) {
            position += adapter.getChildrenCount(group);
        }
    }
}