Recycler View突然重置所选卡片视图的背景

时间:2018-02-17 10:08:38

标签: android android-recyclerview

选择的卡片视图
card view

我只是想从上图中找到解决方案。为什么每次我向下滚动然后再向上滚动选定的卡片视图会回到正常的白色背景。我已经找了2天以上的答案了。我希望有人能找到解决问题的方法。

RecyclerViewAdapter

public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder> {

private ArrayList<SectionDataModel> dataList;
private Context mContext;

public RecyclerViewDataAdapter(Context context, ArrayList<SectionDataModel> dataList) {
    this.dataList = dataList;
    this.mContext = context;
}

@Override
public ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);
    ItemRowHolder mh = new ItemRowHolder(v);
    return mh;
}

@Override
public void onBindViewHolder(ItemRowHolder itemRowHolder, int i) {

    final String sectionName = dataList.get(i).getHeaderTitle();

    ArrayList singleSectionItems = dataList.get(i).getAllItemsInSection();

        itemRowHolder.itemTitle.setText(sectionName);

        CandidateListDataAdapter itemListDataAdapter = new CandidateListDataAdapter(mContext, singleSectionItems);

        itemRowHolder.recycler_view_list.setHasFixedSize(true);
        itemRowHolder.recycler_view_list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapter);

        itemRowHolder.recycler_view_list.setNestedScrollingEnabled(false);

}

@Override
public int getItemCount() {
    return (null != dataList ? dataList.size() : 0);
}

public class ItemRowHolder extends RecyclerView.ViewHolder {

    protected TextView itemTitle;
    protected RecyclerView recycler_view_list;

    public ItemRowHolder(View view) {
        super(view);

        this.itemTitle = (TextView) view.findViewById(R.id.itemTitle);
        this.recycler_view_list = (RecyclerView) view.findViewById(R.id.recycler_view_list);
    }

}

}

候选适配器(水平)

public class CandidateListDataAdapter extends RecyclerView.Adapter<CandidateListDataAdapter.SingleItemRowHolder> {

private ArrayList<CandidateDetailModel> itemsList;
private Context mContext;

public CandidateListDataAdapter(Context context, ArrayList<CandidateDetailModel> itemsList) {
    this.itemsList = itemsList;
    this.mContext = context;
}

@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.candidate_card, null);
    SingleItemRowHolder mh = new SingleItemRowHolder(v);
    return mh;
}

@Override
public void onBindViewHolder(SingleItemRowHolder holder, int i) {

    CandidateDetailModel candidate = itemsList.get(i);

    holder.name.setText(candidate.getName());
    holder.gender.setText(candidate.getGender());
    holder.dept.setText(candidate.getDept());
    holder.year.setText(String.valueOf(candidate.getYear()));

    if(holder.profile.getBorderWidth() == 3){
        holder.profile.setBorderColor(Color.parseColor("#000000"));
        holder.profile.setBorderWidth(3);
    }

    Glide.with(mContext).load("http://192.168.43.87/Election/profile/"+candidate.getImgUrl())
            .thumbnail(0.5f)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .dontAnimate()
            .placeholder(R.drawable.prof)
            .into(holder.profile);

   /* Glide.with(mContext)
            .load(feedItem.getImageURL())
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .error(R.drawable.bg)
            .into(feedListRowHolder.thumbView);*/
}

@Override
public int getItemCount() {
    return (null != itemsList ? itemsList.size() : 0);
}

public class SingleItemRowHolder extends RecyclerView.ViewHolder {

    protected CircleImageView profile;
    protected LinearLayout layout;
    protected TextView name;
    protected TextView gender;
    protected TextView dept;
    protected TextView year;
    protected ImageView deptIcon;
    protected ImageView genderIcon;

    public SingleItemRowHolder(View view) {

        super(view);

        this.profile = (CircleImageView) view.findViewById(R.id.prof);
        this.layout = (LinearLayout) view.findViewById(R.id.card);
        this.name = (TextView) view.findViewById(R.id.candName);
        this.gender = (TextView) view.findViewById(R.id.gender);
        this.dept = (TextView) view.findViewById(R.id.dept);
        this.year = (TextView) view.findViewById(R.id.year);
        this.deptIcon = (ImageView) view.findViewById(R.id.deptIcon);
        this.genderIcon = (ImageView) view.findViewById(R.id.genderIcon);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), name.getText(), Toast.LENGTH_SHORT).show();
                profile.setBorderColor(Color.parseColor("#000000"));
                profile.setBorderWidth(3);
                layout.setBackgroundColor(Color.parseColor("#80cbc4"));
            }
        });


    }

}

1 个答案:

答案 0 :(得分:0)

如果不符合条件,您需要提供默认颜色。见下文。

if(holder.profile.getBorderWidth() == 3){
        holder.profile.setBorderColor(Color.parseColor("#000000"));
        holder.profile.setBorderWidth(3);
    } else{
//Set views' color to a default color.
}