当我更改在recyclerview内部的cardview的颜色时,它也会改变其他项目的颜色。我该如何解决这个问题。
以下是我要更改背景颜色的模块部分:
public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && mListener != null) {
mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
int position = recyclerView.getChildLayoutPosition(child);
child.setBackgroundColor(mContext.getResources().getColor(R.color.blue_light));
Toast.makeText(mContext, "Id " + String.valueOf(position), Toast.LENGTH_SHORT).show();
deleteItems.add(position);
}
}
});
}
适配器的代码如下:
public class DiaryAdapter extends RecyclerView.Adapter<DiaryAdapter.ViewHolder>{
List <DiaryInfo> mItems;
SQLiteDatabase mydatabase;
public DiaryAdapter() {
super();
mItems = new ArrayList <DiaryInfo> ();
mItems = MainActivity.diaryinfo;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.content_main, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
mydatabase = (MainActivity.mContext).openOrCreateDatabase("remember", android.content.Context.MODE_PRIVATE, null);
// While changing database information here change at Itemadd's done button listener also!
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS memories_pics(id VARCHAR PRIMARY KEY, path VARCHAR)");
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
try {
DiaryInfo info = mItems.get(i);
viewHolder.desc.setText(Html.fromHtml((info.getDesc()).replaceAll("''", "\"")));
// Modifying date to readable format
String tempDate = info.getDate();
Locale locale = (MainActivity.mContext).getResources().getConfiguration().locale;
String[] temp = tempDate.split("/"); // Splitting date dd/mm/yyyy
int month = Integer.valueOf(temp[1]);
DateFormatSymbols symbols = new DateFormatSymbols(locale);
String[] monthNames = symbols.getMonths();
String currentMonth = monthNames[month - 1];
viewHolder.date.setText(currentMonth + " " + temp[0]);
String id = info.getThumbnail();
String path = ""; // Final path of the thumbnail
Cursor cursor = mydatabase.rawQuery("SELECT * FROM memories_pics WHERE id=\'" + id + "\' LIMIT 1", null);
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
path = cursor.getString(cursor
.getColumnIndex("path"));
cursor.moveToNext();
break;
}
}
cursor.close();
File file = null;
// Either load image from user clicked images or a default image
if ((String.valueOf(path.split("=")[0])).equals("")) {
Glide.with(MainActivity.mContext).load(R.drawable.default_pic).centerCrop().into(viewHolder.imgThumbnail);
} else {
file = new File(String.valueOf(path.split("=")[0])); // Load path in file
Glide.with(MainActivity.mContext).load(file).centerCrop().into(viewHolder.imgThumbnail);
}
}
catch (Exception e) {
}
}
@Override
public int getItemCount() {
return mItems.size();
}
public Object getItem(int pos) { return mItems.get(pos); }
class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imgThumbnail;
public TextView date, desc;
public ViewHolder(View itemView) {
super(itemView);
imgThumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
date = (TextView)itemView.findViewById(R.id.date);
desc = (TextView)itemView.findViewById(R.id.description);
}
}
}
以下是cardview布局:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="@drawable/default_pic"/>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/thumbnail"
android:maxLines="1"
android:padding="4dp"
android:text="Title"
android:textColor="#222"
android:textStyle="bold"
android:textSize="22sp"/>
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/date"
android:maxLines="3"
android:padding="4dp"
android:text="Description"
android:textColor="#666"
android:textSize="14sp"
android:ellipsize="end"/>
</RelativeLayout>
请帮忙!