【问题标题】:Android: get view from listview by positionAndroid:按位置从列表视图中获取视图
【发布时间】:2013-07-29 19:07:33
【问题描述】:

我想以编程方式更改特定位置的列表视图(simple_list_item_2 适配器)子项的颜色。 (这里例如所有带有 a = lv.getCount();) 的项目

ListView lv = getListView();
int a = lv.getCount();
for (int i = 0; i < a; i++) {
            ((TextView) lv.getChildAt(i).findViewById(android.R.id.text1)).setTextColor(Color
                    .parseColor("#EEC900"));
}

getChildAt();并不总是对我有用。如果列表项不在屏幕上,getChild 不会返回视图或其他内容。 如果getChildAt没有更好的解决方案吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    您必须在 Adapter 类中执行此操作。 Android 缓存并重新循环列表视图中的视图以节省内存。所以没有你看不到的视图来改变颜色。

    例如,如果您有一个 arrayAdapter,您将覆盖 getView 函数并在那里运行您的检查:

    @Override
        public View getView(int position, View view, ViewGroup parent) {
            // use "position" to determine which item you have.
            // Then set the properties of "view" which is your list row.
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 2014-08-22
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多