ListView 项不会在 onCreate 中更改其背景颜色,但会在 onItemClick 中更改

丹尼斯1611

我有两个列表,一个长的和一个短的。只显示长列表,长列表中的列表项也位于短列表中,背景颜色为蓝色。你可以猜到,这行不通。

除此之外,我可以更改 onItemClick 项目的 backgroundColor。(这有效)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_couple_file);

List<String> longList = new ArrayList<String>();
longList.add("A");
longList.add("B");
List<String> shortList = new ArrayList<String>();
longList.add("A");

//        display longList
ListView list = (ListView) findViewById(R.id.list);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_1, longList);
    list.setAdapter(listAdapter);

//        look for common elements
    for (int i = 0; i < longList.size(); i++) {
        if (shortList.contains(longList.get(i))) {
            View view = listAdapter.getView(i, null, list);
//        should change the item view's color, but it doesn't
            view.setBackgroundResource(R.color.blue);
        }
    }

    registerClick();
}

private void registerClick() {
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View viewClicked, int position, long id) {
 //       however, this does change the item view's color    
viewClicked.setBackgroundResource(R.color.red);
    }  
}

我知道 if 语句有效,我已将视图转换为 TextView 并在吐司中显示其文本。

虽然可能有完全其他的选项可以做到这一点,但我想知道为什么“A”项目的背景颜色在它应该改变的时候没有改变(它也不会崩溃),尽管它在 onItemClickListener 中工作。

编辑:我已经切换了 setAdapter 部分和 if 语句的位置(因为我在我的原始代码中逻辑上有它),并按照 android studio 的建议使用了 setBackgroundResource ,但这并没有解决问题。

Ankit Patidar

请移动您的此代码::

 for (int i = 0; i < longList.size(); i++) 
 {
       if (shortList.contains(longList.get(i)))
       { 
            View view = listAdapter.getView(i, null, list); 
            // should change the item view's color, but it doesn't 
           view.setBackgroundColor(getResources().getColor(R.color.blue));
       }
  }

在“registerClicks()”之后:

      registerClick();

      for (int i = 0; i < longList.size(); i++)   
      { 
           if (shortList.contains(longList.get(i))) 
           { 
                View view = listAdapter.getView(i, null, list);
                 // should change the item view's color, but it doesn't 
                view.setBackgroundColor(getResources().getColor(R.color.blue));
            }
     }

实际上问题在于,您在实际列出列表视图之前调用了背景更改。所以它没有得到你想要改变背景颜色的孩子的意见。

编辑:

使用此代码:

  list.post(new Runnable() {
       @Override
       public void run() {
           for (int i = 0; i < longList.size(); i++) {
               if (shortList.contains(longList.get(i))) {
                   list.getChildAt(i).setBackgroundColor(
                           getResources().getColor(R.color.blue));
               }
           }
       }
   });

为什么我使用“.post()”,因为没有这个我们正在获取它的视图,但你的列表还没有创建。

谢谢和快乐编码!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

更改枚举项不会在原始IEnumerable中更改

Oncreate()中的动画不会在android中停止

片段中的Listview onitemclick

Xamarin 表单:ListView 项不会在 UWP 中第二次触发

Javascript/Jquery 不会在 Safari 中更改 URL 参数,但会在 Chrome 中更改

ListView不会在滚动时更改元素

ProgressBar不会在Java中更改其值

Listview将不会在列中呈现

Android Studio 不会在 PasswordToggleDrawable 中更改颜色

CSS不会在Opera中更改文本颜色

Vue 子道具不会在父项中的绑定值更改时更新

在jQuery Mobile 1.4.0中选择listview项时更改其背景颜色

使用onItemClick在DialogFragment中从ListView获取数据

React Native ListView不会在数据更改时更新

ListView不会在itemsource的ObservableCollection Item属性更改上更新

更改Xamarin中ListView选定项的背景颜色

ethtool不会在预配置中更改网络速度

单击按钮不会在kivymd中更改屏幕

更改资产outputPath不会在html中更新

innerHTML不会在每个keydown事件中更改

UISwitch不会在Swift中更改值

Android:在一个ListView项中更改ImageView会在其他ListView项中更改相同的ImageView吗?

如果未在 onCreate 中声明,则不会出现 Listview

ListView不会在横轴上增长

ListView不会在拖放时更新

DAO Factory的示例,其数据源不会在不同版本中更改

Android TextView不会在oncreate之外更新

ngOnInit不会在子级路线之间触发一项更改

为什么Boxplot轮廓不会在ggplot / R中更改颜色