【问题标题】:SetBackgroundColor to listview item on Custom Adapter does not workSetBackgroundColor 到自定义适配器上的列表视图项不起作用
【发布时间】:2019-10-22 23:40:39
【问题描述】:

我有一个可以工作的自定义适配器,我已将其放入列表视图。
我现在成功添加了 15 个项目。

我现在尝试将项目 3 的背景颜色设置为橙色。 这项工作!

但是,当我向下和向上滚动时,背景颜色消失了。
我们如何防止这种情况发生?

//MENU.axml 与 listview1

MENU.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#70292929"
    android:layout_gravity="top|right"
    android:orientation="horizontal">
      <ListView
          android:id="@+id/listview1"
          android:background="#1b4f72"
          android:layout_weight="0.5"
          android:layout_gravity="top|left"
          android:layout_width="10px"
          android:layout_height="match_parent">
      </ListView>
</LinearLayout>

//向listview1添加项目并尝试将项目3的背景设置为橙色的C#代码

Android.Views.View MENUVIEW = LayoutInflater.Inflate(Resource.Layout.MENU, null);
ListView LISTVIEW1 = MENUVIEW.FindViewById<ListView>(Resource.Id.listview1);

                //Add Items to the listview1
                List<String> itemLIST = new List<String>(); 
                for (int i = 0; i < 15; i++)
                {
                    itemLIST.Add("hello" + i); 
                }
                Adapter1 adapter1 = new Adapter1(this, Android.Resource.Layout.SimpleListItem1, itemLIST);
                LISTVIEW1.Adapter = adapter1;

//Set backgroundcolor on item 3
LISTVIEW1.GetChildAt(3).SetBackgroundColor(Android.Graphics.Color.Orange);
TextView tview = listview.GetChildAt(3).FindViewById<TextView>(Resource.Id.itemtext);
tview.SetBackgroundColor(Android.Graphics.Color.Orange);

//适配器1的代码

   class Adapter1 : BaseAdapter
    {

        Context context;
        int item = 0;
        List<String> items = new List<String>();

        public Adapter1(Context context, int resource, List<String> itemArray)
        {
            items = itemArray;
            item = resource;
            this.context = context;
        }
   

        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }
        
        public String getItem(int position)
        {
            return items[position]; //returns list item at the specified position
        }
        public override int GetItemViewType(int position)
        {
            return position;
        }
        

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView;
            Adapter1ViewHolder holder = null;
           

            if (holder == null)
            {
                holder = new Adapter1ViewHolder();
                var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();

                view = inflater.Inflate(Resource.Layout.textview, parent, false);
                String currentItem = getItem(position);

                //get the TextView for item name and item description
                TextView textViewItemName = view.FindViewById<TextView>(Resource.Id.itemtext);

                //sets the text for item name and item description from the current item object
                textViewItemName.SetText(currentItem, TextView.BufferType.Normal);

                holder.Title = textViewItemName;
                holder.Title.Text = currentItem;
                view.Tag = holder;
            }
            return view;
        }

        //Fill in cound here, currently 0
        public override int Count
        {
            get
            {
                return items.Count;
            }
        }
    }

    class Adapter1ViewHolder : Java.Lang.Object
    {
        //Your adapter views to re-use
        public TextView Title { get; set; }
    }

【问题讨论】:

    标签: android colors custom-adapter setbackground


    【解决方案1】:

    您应该尝试使用 Adapter1 中的#getView 方法更新颜色。

      int orangeColorPosition = -1;
    
      public void setOrangeColorPosition(int position){
          this.orangeColorPosition = position;
          notifyDataSetChanged();
      }
    
      public override View GetView(int position, View convertView, ViewGroup parent)
        {
            ...
            if(position == orangeColorPosition ){
                    // set background color here
            }
            return view;
        }
    

    然后调用 adapter1.setOrangeColorPosition(3);应该更新位置的颜色

    【讨论】:

    • 是的,因为我只知道 MainActivity.cs 文件中的位置。我将如何设置 orangeColorPosition = 3;在来自 MainActivity.cs 文件的 Adapter 文件中?
    • 对不起,我在示例中混合了一些 java 代码,但我认为您应该知道我的意思。要更新,请在 mainActivity 中调用 adapter1.setOrangeColorPosition(3) 应该可以工作。
    【解决方案2】:

    使用

    setBackgroundResource(R.color.Orange);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      相关资源
      最近更新 更多