gpt4 book ai didi

安卓 : How to add a listview inside a scrollview. ?

转载 作者:行者123 更新时间:2023-11-29 21:05:25 25 4
gpt4 key购买 nike

在 android 应用程序中,我有一个垂直方向的线性布局。此布局包含 2 个子项( ListView 和 TextView )。问题是布局必须仅在 ListView 完成滚动时显示 TextView 。请帮我设计布局?

最佳答案

您只需要知道当 ListView 位于底部或显示最后一个项目时,然后让您的 TextView 可见,这段代码可以解决问题:

yourListView.setOnScrollListener(this);//The class must implement onscrolllistener

@Override
public void onScroll(AbsListView lw, final int firstVisibleItem,
final int visibleItemCount, final int totalItemCount) {

switch(lw.getId()) {
case android.R.id.list:

// Make your calculation stuff here. You have all your
// needed info from the parameters of this function.

// Sample calculation to determine if the last
// item is fully visible.
final int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem >= totalItemCount) {
if(preLast!=lastItem){ //to avoid multiple calls for last item
Log.d("Last", "Last");
preLast = lastItem;
//Make your text view visible
}
}
}
}

同样重要的是要提到“你不应该在 ScrollView 中有一个 ListView 。”,它违反了 Android 设计指南,如果你采用这种方法,你很可能会做一些非常错误的事情。

希望对您有所帮助!

问候!

关于安卓 : How to add a listview inside a scrollview. ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24743571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com