问题描述
如标题所示:我想知道如何在我的 AdapterView 中获取所有当前显示项目的列表(数组).
为什么?在用户关闭 AdapterView 后,显示在我的 AdapterView 中的对象需要释放侦听器.我需要这样做以优化我的应用程序.
或者是否有任何方法(我可以覆盖)在视图销毁时执行?
implements OnScrollListener
public class NewsCategoryDC extends Activity 实现 OnScrollListener {
并在 listView 中设置 OnScrollListener
listView.setOnScrollListener(NewsCategoryDC.this);
你可以得到第一个和最后一个可见的行
@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {firstVisibleRow = listView.getFirstVisiblePosition();lastVisibleRow = listView.getLastVisiblePosition();/*现在你得到第一个和最后一个可见行,你很容易得到从第一个可见行到最后一个可见行的行并将资源分配给可见行或将资源释放到除可见行之外的行..,.*/}@覆盖public void onScrollStateChanged(AbsListView view, int scrollState) {}
试试这个...,.
如果你有其他最好的方法请发帖,这是一个非常有用的好问题..,.
谢谢...,.
编辑 .....................
在 onScroll() 方法中添加代码
@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {int firstVisibleRow = listView.getFirstVisiblePosition();int lastVisibleRow = listView.getLastVisiblePosition();for(int i=firstVisibleRow;i
As in title: I would like to know how to get list (array) of all currently displayed items in my AdapterView.
Why? Objects that are displayed in my AdapterView require releasing a listener after user closes AdapterView. I need to do it in order to optimize my app.
Or is there any method (that I could override) which is executed on views' destruction?
implements OnScrollListener
and set OnScrollListener in listView
listView.setOnScrollListener(NewsCategoryDC.this);
and you can get first and last visible rows
@Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
firstVisibleRow = listView.getFirstVisiblePosition();
lastVisibleRow = listView.getLastVisiblePosition();
/*Now you get the first and last visible rows, and you easily get the rows from first to last visible row and allocate resources to visible rows or deallocate resources to rows except visible rows..,.*/
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
TRY THIS..,.
and if some other best way you got please post, this is very useful and good question..,.
Thanks..,.
EDIT............................
add code in onScroll() method
@Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
int firstVisibleRow = listView.getFirstVisiblePosition();
int lastVisibleRow = listView.getLastVisiblePosition();
for(int i=firstVisibleRow;i<=lastVisibleRow;i++)
{
//Write your code here(allocation/deallocation/store in array etc.)
System.out.println(i + "=" + listView.getItemAtPosition(i));
}
}
这篇关于如何获取当前显示在 AdapterView 中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!