简体   繁体   English

Android-如何获取ListView项目在绝对位置?

[英]Android - How to get ListView item at absolute position?

So ListView Adapter has the getView method and it's called everytime the display of the listview is changed... so the 0th element becomes the 1st element in the viewport. 因此,ListView Adapter具有getView方法,并且每次更改列表视图的显示时都会调用它...因此,第0个元素成为视口中的第一个元素。 I think... 我认为...

How do i basically access the 0TH element in the Listview? 我如何基本上访问Listview中的0TH元素? Not the 1st element in the viewport? 不是视口中的第一个元素? First element in the entire ListView? 整个ListView中的第一个元素? I want tot access elements by an absolute index so it doesn't matter which one is visible, i want to access them based on their index in the ListView. 我想按绝对索引访问元素,所以无论哪个可见,我都希望基于ListView中的元素访问它们。

How do i do this? 我该怎么做呢?

You can't access any View in the ListView by index, because it's recycling them and essentially there are no views other than you see plus some cache. 您无法按索引访问ListView的任何View ,因为它正在回收它们,并且除了您看到的视图和一些缓存外,基本上没有其他视图。

And actually you shouldn't want to do this. 实际上,您不应该这样做。 If you want to manipulate with the ListView 's data, change the underlying collection via Adapter , so in overriden getView() of your adapter you can decide how to render the particular view (what background color to set in your case). 如果要处理ListView的数据,请通过Adapter更改基础集合,因此在Adapter重写getView() ,您可以决定如何呈现特定视图(在您的情况下要设置的背景颜色)。

So you'll have something like this: 因此,您将获得以下内容:

private class Item {
     Color getBgColor {...}
}

private class MyAdapter extends BaseAdapter { // or WhateverAdapter you have
    Item getItem(int id) {...}

    public View getView(int position, View convertView, ViewGroup parent) {
        ...
        convertView.setBackgroundColor(getItem(position).getBgColor());
        return convertView;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:如何获取ListView中单击的项目位置? - Android: how to get the clicked item position in the ListView? 如何从Android中的“自定义ListView”获取被点击项的位置? - How to get clicked item position from Custom ListView in android? 如何获取ListView项的ID,而不是ListView上的位置? - How can I get the ID of the ListView Item instead of the position on the listview? 单击listView上的按钮后获取_id和position项目 - Android - Get _id and position item after click button on listView - Android Android Studio-单击列表视图中的项目,从该位置获取值 - Android Studio - Click item in listview, get a value from that position 如何通过列表视图 go 并在不单击它的情况下获取其中项目的 position Android Studio - How to go through a listview and get the position of a item in it WITHOUT click on it Android Studio 获取ListView中项目的位置? - Get position of an item within a ListView? 如何在ListView Android中获取位置imageView - How to get position imageView in ListView Android 如何在android中查看listview中的项目? - How to get view for an item in listview in android? 如何在适配器外部获取 ListView 中的 position 和 GridView 项目? - How to get the item position in a ListView and GridView outside the adapter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM