在notifydatasetchanged()之后,RecyclerView不会更新onBindViewHolder中的私有字段

时间:2016-05-13 17:19:53

标签: android android-recyclerview custom-adapter

我有一个RecyclerView,我必须根据我在updateList方法中更改的值加载不同的数据。 以下是我的代码:

public class PerformanceAdapter extends RecyclerView.Adapter<PerformanceAdapter.ViewHolder> {

ArrayList<Performance> list;
Context ctx;

int searchField;

public PerformanceAdapter(ArrayList list, Context ctx) {
    this.list = new ArrayList<>();
    this.list = list;
    this.ctx = ctx;
}

public void updateList(ArrayList<Performance> list, int searchField) {
    this.searchField = searchField;
        ArrayList tempList = new ArrayList();
        tempList.addAll(list);
        Log.i("saz","Size on notifyupdate: "+list.size()+" and searchField: "+this.searchField);
        this.list.clear();
        this.list.addAll(tempList);
        notifyDataSetChanged();
}

@Override
public PerformanceAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_performance_cardview, parent, false);
    // set the view's size, margins, paddings and layout parameters

    searchField = viewType;
    ViewHolder vh = new ViewHolder(v);
    return vh;
}


@Override
public void onBindViewHolder(PerformanceAdapter.ViewHolder holder, int position) {
   /* position = (int) convertView.getTag();
    Log.i("saz","position value 2nd : "+position);*/
    Performance performance = this.list.get(position);
Log.i("saz","searchField: "+searchField);

    if (position%2 != 0) {
        holder.cv.setCardBackgroundColor(ctx.getResources().getColor(R.color.lighter_gray));
    } else {
        holder.cv.setCardBackgroundColor(ctx.getResources().getColor(R.color.white));
    }

    Log.i("saz","searchField: "+searchField);
    holder.txtModelName.setText(performance.getModelName());
    if (searchField == 0) {
        holder.cv.setVisibility(View.GONE);
    } else if (searchField == 1) {
        Log.i("saz","searchField: "+searchField);
        holder.cv.setVisibility(View.VISIBLE);
        holder.cv2.setVisibility(View.GONE);
        holder.cv3.setVisibility(View.GONE);
        holder.cv4.setVisibility(View.GONE);

    } else if (searchField == 2) {
        holder.cv.setVisibility(View.VISIBLE);
        holder.cv2.setVisibility(View.GONE);
        holder.cv3.setVisibility(View.GONE);
        holder.cv4.setVisibility(View.GONE);
    }
}

@Override
public int getItemViewType(int position) {
    return searchField;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemCount() {
    Log.i("saz","List size in recycler: "+list.size());
    return this.list.size();
}

然后我打电话给

updateList(list, 0)
updateList(list, 1)
or updateList(list, 2)

点击按钮。

但我总是在我的bindViewHolder中将searchField设为0。 请帮忙。

1 个答案:

答案 0 :(得分:0)

您使用始终为0的viewType覆盖它,因为只有一种类型。

@Override public
PerformanceAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.list_performance_cardview, parent, false); // set the view's size, margins, paddings and layout parameters 
    searchField = viewType; 
    ViewHolder vh = new ViewHolder(v); 
    return vh;
}