【问题标题】:listview scrolling changes background color of item views (simpleadapter)列表视图滚动更改项目视图的背景颜色(简单适配器)
【发布时间】:2019-08-07 19:40:43
【问题描述】:

我对 listview 和 simpleadapter 有疑问。当第一次绑定listview simpleadapter 时,我更改了某些行视图的背景颜色。问题是当我滚动列表视图时,它会随机更改视图的背景颜色。我真的不明白这里发生了什么。我使用变量(coloringDone)来检查列表视图是否已经绑定,因此避免再次更改颜色(在 simpleadapter getView 方法中),我在第一次加载 listview 时在 onLayoutChange 方法中将此变量设置为 true。我在coloringDone 之后在getView 方法中设置了一个断点,如果它没有命中。

我的 lisview 项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:orientation="horizontal">



    <com.toptoche.searchablespinnerlibrary.SearchableSpinner
        android:id="@+id/person_spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <TextView
        android:id="@+id/Receiver_Name"
        android:layout_width="190dip"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textAlignment="gravity" />

    <TextView
        android:id="@+id/Asset_Name"
        android:layout_width="190dip"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textAlignment="gravity" />


</LinearLayout>

我的简单适配器:

public class AssetSimpleAdapter extends SimpleAdapter {
HashMap<String, String> map = new HashMap<String, String>();
public AssetSimpleAdapter(Context context, List<? extends Map<String, String>> data,
                          int resource, String[] from, int[] to) {
    super(context, data, resource, from, to);
    mContext = context;
}

Context mContext;

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = super.getView(position, convertView, parent);



    if (!((MainActivity) mainActivity).coloringDone && some other conditions) {


            ((TextView) ((LinearLayout) view).findViewById(R.id.Asset_Name)).setBackgroundColor(mContext.getResources().getColor(R.color.red));
            ((TextView) ((LinearLayout) view).findViewById(R.id.Receiver_Name)).setBackgroundColor(mContext.getResources().getColor(R.color.red));




    }


    return view;

}

}

和 MainActivity:

public boolean coloringDone = false;

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

    mainActivity = this;

  ...

 tagListVU.setAdapter(adapter);
    tagListVU.deferNotifyDataSetChanged();
    tagListVU.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {

        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            tagListVU.removeOnLayoutChangeListener(this);


               coloringDone = true;

        }
    });

编辑:通过随机改变颜色,我的意思是有些行变成红色,有些行变成白色

【问题讨论】:

    标签: android listview simpleadapter


    【解决方案1】:

    最后我解决了这个问题。我将适配器更改为 ArrayAdapter 但仍然存在同样的问题,所以问题不在于适配器类型。通过在适配器的 getView 方法中放置 else 子句并将视图的背景颜色更改为白色来解决问题(并且不需要 coloringDone 变量):

       if (some other conditions) {
    
        ((TextView) ((LinearLayout) view).findViewById(R.id.Asset_Name)).setBackgroundColor(mContext.getResources().getColor(R.color.red));
        ((TextView) ((LinearLayout) view).findViewById(R.id.Receiver_Name)).setBackgroundColor(mContext.getResources().getColor(R.color.red));
    
    }
    else{
    
        ((TextView) ((LinearLayout) view).findViewById(R.id.Asset_Name)).setBackgroundColor(mContext.getResources().getColor(android.R.color.white));
        ((TextView) ((LinearLayout) view).findViewById(R.id.Receiver_Name)).setBackgroundColor(mContext.getResources().getColor(android.R.color.white));
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多