如何在特定索引处获取ListView的子视图 [英] How to get a ListView's child view at specific index
问题描述
我需要在特定索引处动态更改ListView
的子视图的背景颜色.因此,在应用背景之前,我需要先获取视图.
I need to dynamically change the background color of a child view of a ListView
at a specific index. So I need to get the view first before applying the background.
我尝试了以下方法.
getViewAt(int index)
-不适用于ListView
.
getChildAt(int index)
-这会导致运行时NPE错误
getChildAt(int index)
- this gives runtime NPE error
Google搜索返回不相关的结果.
A Google search returns irrelevant results.
请帮帮我.谢谢.
推荐答案
我知道了如何做到这一点.
I figured out how to do this.
我真的认为,对于类似于如何设置背景图像"的问题,不必发布代码和logcat.
I really think that one should not have to post code and logcat for a question that is similar to "How to set background image".
无论如何,总之,我的答案是通过适配器的getView()将位置设置为列表视图中每个孩子的标签,然后使用findViewWithTag(Object tag)获取任何孩子.
Anyway my answer is, in short, set position as tag for every child in the listview through your adapter's getView() and then get any child using findViewWithTag(Object tag).
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = (LinearLayout)inflater.inflate(R.layout.some_layout, parent, false); .
convertView.setTag(String.valueOf(position));
}
以及每当您需要一个特定的孩子时.
And whenever you need to get a specific child.
View v = mylistview.findViewWithTag(String.valueOf(index));
更改背景颜色.
v.setBackgroundResource(R.color.some_color);
这篇关于如何在特定索引处获取ListView的子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!