我的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);
}
});
然後我滑開音符,然後迅速按下Undo方法撤消
[IMAGE] After I press the undo delete on the snackbar
你改變顏色programmatially –
@snlvtr告訴我「mMemos.get(位置).getNotificationColor()」 –
@snlvtr的價值嘗試holder.memoTitle.setT一個測試用例的extColor(Color.RED)?並檢查它是否工作 – Raghavendra