【问题标题】:Android listview item change background colorAndroid listview项目更改背景颜色
【发布时间】:2019-07-04 23:48:52
【问题描述】:

我无法更改列表视图项的背景颜色。

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info_layout);
    ListView listView;
    listView = (ListView) findViewById(R.id.infolistView);
    final String[] values = new String[] { "TO1", "TO2", "TO3" };
    ArrayAdapter<String> adapter =
        new ArrayAdapter<String>(this,
                                 android.R.layout.simple_list_item_1,
                                 android.R.id.text1,
                                 values);
    listView.setAdapter(adapter);

    // This is not make anything , why ?
    View vi=adapter.getView(0, null, null);
    vi.setBackgroundColor(Color.YELLOW);

listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                //But here is running good
                view.setBackgroundColor(Color.RED);}
    });

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    你所做的只是获取一个填充了位置数据的视图实例,然后在调用该方法后死掉并被垃圾收集。

    要更改列表中的实际视图,您必须从适配器更改颜色并编写自定义适配器,或者让颜色以该状态开始。

    基本上getView 不会从视图列表中获取,而只是创建一个新视图,除非您将其传递给视图。

    【讨论】:

      【解决方案2】:

      您必须创建自定义适配器来更改项目的背景颜色。

      这是自定义适配器的示例:

      public class PaListAdapter  extends BaseAdapter{
              private LayoutInflater mInflater;
      
               private ArrayList<String> platevalue = new ArrayList<String>();
      
                 ViewHolder holder;
              public PaListAdapter(Context context,ArrayList<String> value)
              {
                  // Cache the LayoutInflate to avoid asking for a new one each time.
                  mInflater = LayoutInflater.from(context);
      
      
      
                  //mycontext = context;
                  platevalue.clear();
                  platevalue =value;
      
      
      
              }
      
      
              public int getCount() 
              {
                  return platevalue.size();
              }
      
              public Object getItem(int position) 
              {
                  return position;
              }
      
              public long getItemId(int position) 
              {
                  return position;
              }
      
              public View getView(int position, View convertView, ViewGroup parent) 
              {
      
      
      
      
      
                  if (convertView == null) 
                  {
                       convertView = mInflater.inflate(R.layout.select_dialog, null);
      
                      holder = new ViewHolder();
                      holder.hTransID =(TextView) convertView.findViewById(R.id.txtChoice);
      
                      holder.ch   =(CheckBox) convertView.findViewById(R.id.chk);
      
      
                      convertView.setTag(holder);
                  }
                  else 
                  {
                      holder = (ViewHolder) convertView.getTag();
                  }
      
                  holder.hTransID.setText(platevalue.get(position));
      
      
      
      
                  return convertView;
              }
      
              static class ViewHolder 
              {      
                    TextView    hTransID;
      
      
              }
          }
      
      
      select_dialog.xml
      
      
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:descendantFocusability="blocksDescendants"
       android:background="#000000"
          >
      
          <TextView
              android:id="@+id/txtChoice"
      
              android:layout_gravity="center_vertical|left"
              android:gravity="center_vertical|left"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" 
              android:textColor="#000000"/> 
      
      </LinearLayout>
      

      在 Activity 类中。定义如下:

         simpleefficientadapter efficientadapter;
      
         efficientadapter=new simpleefficientadapter(CLASSNAME.this, VALUES);
         listView.setAdapter(efficientadapter);
      

      【讨论】:

      • 何时更改背景颜色?
      【解决方案3】:

      您可以创建 listView 项目的实例并使用接受整数的getChildAt() 方法。然后设置视图实例背景颜色 -

      View focusedChild = listView.getChildAt(r);
      focusedChild.setBackgroundColor(Color.RED);
      

      【讨论】:

      • 欢迎来到 Stack Overflow。每当您想提供编码建议时,您都应该考虑使用编辑器的格式选项。
      猜你喜欢
      • 2012-03-10
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多