关于从ListView检索视图的困惑

时间:2017-09-28 12:37:14

标签: android listview view colors android-arrayadapter

我试图在Android应用中创建一个简单的ListView并更改视图的背景颜色。

我成功了,只是覆盖定义的getView()类的ArrayAdapter方法并在其中更改它。

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
         android.R.layout.simple_list_item_1, Categories) {
  @Override
  public View getView(int pos, View v, ViewGroup vg) {
    View view = super.getView(pos, v, vg);

    switch (pos) {
      case 0:
        view.setBackgroundColor(Color.RED);
        break;
      case 1:
        view.setBackgroundColor(Color.BLUE);
        break;
      default:
        break;
    }

    return view;
  }
};

但是,如果我删除switch语句并在返回视图的此函数外使用它,则颜色不会更新。我的意思是这样的:( lv是我的ListView

lv.setAdapter(arrayAdapter);
for (int i=0; i<2; ++i) {
  View V = (View) lv.getAdapter().getView(i, null, lv);

  switch (i) {
    case 0:
      V.setBackgroundColor(Color.RED);
      break;
    case 1:
      V.setBackgroundColor(Color.BLUE);
      break;
    default:
      break;
  }
}

它确实返回对视图的引用,那为什么它不能那样工作呢?

3 个答案:

答案 0 :(得分:1)

每当你改变一些东西时都会打电话:

ArrayAdapter.notifyDataSetChanged();

无论如何,这不是英国的好方法,但我想每个解决方案都是一个解决方案¯_(ツ)_ /¯

答案 1 :(得分:0)

我建议您使用RecyclerView代替ListView。一旦我创建了彩色列表。在这里,我只是复制我的代码。

public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder>{
    private final ArrayList<String> dataSet;

    class ViewHolder extends RecyclerView.ViewHolder {
        final TextView mTextView;
        final CardView cardView;
        ViewHolder(View v) {
            super(v);
            mTextView = (TextView) v.findViewById(R.id.info_text);
            cardView = (CardView) v.findViewById(R.id.item_container);
        }
    }

    public MyListAdapter(ArrayList<String> myDataset) {
        dataSet = myDataset;
    }

    @Override
    public MyListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTextView.setText(dataSet.get(position));
        switch (position%11){
            case 0: holder.cardView.setCardBackgroundColor(0xFFB71C1C);
                break;
            case 1: holder.cardView.setCardBackgroundColor(0xFF880E4F);
                break;
            case 2: holder.cardView.setCardBackgroundColor(0xFF4A148C);
                break;
            case 3: holder.cardView.setCardBackgroundColor(0xFF311B92);
                break;
            case 4: holder.cardView.setCardBackgroundColor(0xFF1A237E);
                break;
            case 5: holder.cardView.setCardBackgroundColor(0xFF01579B);
                break;
            case 6: holder.cardView.setCardBackgroundColor(0xFF004D40);
                break;
            case 7: holder.cardView.setCardBackgroundColor(0xFF1B5E20);
                break;
            case 8: holder.cardView.setCardBackgroundColor(0xFF33691E);
                break;
            case 9: holder.cardView.setCardBackgroundColor(0xFF827717);
                break;
            case 10: holder.cardView.setCardBackgroundColor(0xFFE65100);
                break;
            default: holder.cardView.setCardBackgroundColor(0xFFBF360C);
        }
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }
}

答案 2 :(得分:0)

你应该使用界面: 在YourAdapter.class中定义一个接口:

    public interface OnColorEvent{
     void change(View v,int position)
 }

在YourAdapter.class中设置侦听器:

private OnColorEvent e;
public void setOnColorEvent(OnColorEvent e)
{
this.e=e;
}

在你的getView()方法中:

    @Override
      public View getView(int pos, View v, ViewGroup vg) {
        View view = super.getView(pos, v, vg);
    if(null!=e)
    {
    e.change(v,pos);
    }
    }
In YourActivity.class:
listview.setOnColorEvent(new OnColorEvent{
@Overide
public void change(View v,int position)
{
switch (position) {
    case 0:
      V.setBackgroundColor(Color.RED);
      break;
    case 1:
      V.setBackgroundColor(Color.BLUE);
      break;
    default:
      break;
  }
}
});