将颜色更改为recyclerview项目

时间:2016-11-15 06:16:19

标签: android android-recyclerview adapter

我的recyclerview显示2个textview和1个imageview。在适配器的onbindviewholder内部,我可以设置2个textview的文本,但是当我改变颜色时,它们会变得不可见。

此外,当我删除recyclerview项目,然后“撤消”它时,它将显示颜色变化。我做错了什么?

我怀疑它与其中一个notify ...()方法有关,但当我尝试将其中一个置于onbindviewholder中时,应用程序将在启动时崩溃。

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

private CardView cv;
private List<Memo> mMemos;
private Context mContext;
public List<Memo> memosToRemove;

//pass in the list of notes to display
public MemoListAdapter(List<Memo> memos, Context context) {
    mMemos = memos;
    mContext = context;

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View rowView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_memo_list, parent, false);
    ViewHolder viewHolder = new ViewHolder(rowView, mContext, mMemos);


    return viewHolder;
}

//@param holder & position - identifies the Note we are working with
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

    holder.memoTitle.setText(mMemos.get(position).getTitle());
    holder.memoContent.setText(mMemos.get(position).getContent());
    holder.memoIcon.setColorFilter(mMemos.get(position).getNotificationColor());
    holder.memoTitle.setTextColor(mMemos.get(position).getNotificationColor());
}

这是将正确显示颜色更改的撤消方法

 public void onItemRemove(final RecyclerView.ViewHolder viewHolder, final RecyclerView mRecyclerView) {

    final int adapterPosition = viewHolder.getAdapterPosition();
    final int memoID = mMemos.get(adapterPosition).getId();
    final DatabaseHandler db = new DatabaseHandler(mContext);
    final Memo memo = db.getMemo(memoID);

    mMemos.remove(adapterPosition);
    db.deleteMemo(memo);
    notifyItemRemoved(adapterPosition);

    Snackbar snackbar = Snackbar
            .make(mRecyclerView, mContext.getString(R.string.note_deleted), Snackbar.LENGTH_LONG)
            .setAction(R.string.undo_string, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mMemos.add(adapterPosition, memo);
                    notifyItemInserted(adapterPosition);
                    mRecyclerView.scrollToPosition(adapterPosition);
                    db.addMemo(memo);
                }
            });

[IMAGE] When app opens

然后我轻扫笔记然后快速按撤消

[IMAGE] After I press the undo delete on the snackbar

2 个答案:

答案 0 :(得分:2)

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    if(position == selected_item) {
        holder.title.setTextColor(Color.parseColor("#00aaff"));
        holder.imgViewIcon.setBackgroundResource(R.drawable.ic_circle);
    } else {
        holder.title.setTextColor(Color.parseColor("#00000")); //actually you should set to the normal text color
        holder.imgViewIcon.setBackgroundResource(0);
    }
    NavDrawerItem current = data.get(position);
    holder.title.setText(current.getTitle());
    holder.imgViewIcon.setImageResource(current.getIcon());


}

编辑:删除链接(应该编辑为OP)

答案 1 :(得分:0)

通过编辑onBindViewHolder来管理解决问题:

 @Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    Memo m = mMemos.get(position);
    int id = m.getId();
    DatabaseHandler db = new DatabaseHandler(mContext);
    Memo memo = db.getMemo(id);
    int color = memo.getNotificationColor();

    holder.memoTitle.setText(mMemos.get(position).getTitle());
    holder.memoContent.setText(mMemos.get(position).getContent());
    holder.memoIcon.setColorFilter(color);
    holder.memoTitle.setTextColor(color);

基本上我必须实际拿走这个项目或者#34; Memo&#34;在我的情况下,直接从数据库,而不是依赖于基于@param位置的备忘录