2016-11-15 133 views
0

我的recyclerview顯示2個文字瀏覽和1個imageview。在適配器的onbindviewholder內部,我可以設置2個textview的文本,但是當我更改顏色時,它們變得不可見。將顏色更改爲recyclerview項目

此外,當我刪除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

然後我滑開音符,然後迅速按下Undo方法撤消

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

+0

你改變顏色programmatially –

+2

@snlvtr告訴我「mMemos.get(位置).getNotificationColor()」 –

+1

@snlvtr的價值嘗試holder.memoTitle.setT一個測試用例的extColor(Color.RED)?並檢查它是否工作 – Raghavendra

回答

0

設法解決這個問題:

@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); 

基本上,我不得不採取實際的項目或「備忘錄」在我的情況下,直接從數據庫中,而不是依靠服用基於@param位置備忘錄

1
@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)通過編輯onBindViewHolder這個