我在RecyclerView的CardView中添加了ImageButton,并且我将它添加到收藏夹中的项目(我制作了sqlite以保存收藏夹)现在我想在我打开活动时制作它上面的RecyclerView显示带有不同图像的ImageButton,如果它在收藏夹和当按下它时改变图像 只是我想制作喜欢的按钮(星图)像gmail应用程序
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<listitem_gib> getDataAdapter;
public RecyclerViewAdapter(List<listitem_gib> getDataAdapter, Context context){
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout, parent, false);
ViewHolder viewHolder = new ViewHolder(v,context,getDataAdapter);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
listitem_gib getDataAdapter1 = getDataAdapter.get(position);
holder.name.setText(getDataAdapter1.getName());
holder.num.setText(getDataAdapter1.getnum());
Picasso.with(context).load("http://grassyhat.com/android/image/" + getDataAdapter1.getimg()).into(holder.img1);
holder.fav.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
fav(position);
}
});
}
@Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener{
public TextView name;
public TextView num;
public ImageView img1;
ImageButton fav;
Context context;
List<listitem_gib> getDataAdapter;
public ViewHolder(View itemView, Context context ,List<listitem_gib> getDataAdapter ) {
super(itemView);
itemView.setOnClickListener(this);
this.getDataAdapter = getDataAdapter;
this.context = context;
this.fav= (ImageButton) itemView.findViewById(R.id.btn_fav);
name = (TextView) itemView.findViewById(R.id.Main_Text) ;
num = (TextView) itemView.findViewById(R.id.Second_Text) ;
img1 = (ImageView) itemView.findViewById(R.id.img1) ;
}
@Override
public void onClick(View v) {
int position = getAdapterPosition();
listitem_gib getDataAdapter =this.getDataAdapter.get(position);
Intent intent = new Intent(this.context,Rewaya_info.class);
intent.putExtra("name",getDataAdapter.getName());
intent.putExtra("url",getDataAdapter.geturl());
intent.putExtra("img",getDataAdapter.getimg());
intent.putExtra("num",getDataAdapter.getnum());
intent.putExtra("size",getDataAdapter.getsize());
this.context.startActivity(intent);
}
}
public void fav(final int position) {
final DB_Sqlit db_sqlit = new DB_Sqlit(context);
final String name = getDataAdapter.get(position).name;
final String img = getDataAdapter.get(position).img;
final String url = getDataAdapter.get(position).url;
final String num = getDataAdapter.get(position).num;
final String size = getDataAdapter.get(position).size;
int count = db_sqlit.get_check_List_Favorite(name);
if (count > 0) {
Toast.makeText(context, "already exist", Toast.LENGTH_SHORT).show();
}else{
Boolean add = db_sqlit.Insert_to_favorite(name, img, url, num, size);
if (add) {
Toast.makeText(context, "added to favourite", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "error", Toast.LENGTH_SHORT).show();
}
}
}
public class DB_Sqlit extends SQLiteOpenHelper {
public static final String BDname = "mdata.db";
public DB_Sqlit(Context context) {
super(context, BDname, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table favorite ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, img TEXT, url TEXT, num TEXT, size TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS favorite");
onCreate(db);
}
public Boolean Insert_to_favorite(String name, String img, String url, String num, String size) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("img", img);
contentValues.put("url", url);
contentValues.put("num", num);
contentValues.put("size", size);
long result = db.insert("favorite", null, contentValues);
if (result == -1)
return false;
else
return true;
}
public List getAllList_Favorite() {
List<listitem_gib> list = new ArrayList<listitem_gib>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor rs = db.rawQuery("select * from favorite", null);
if (rs.moveToFirst()){
do {
listitem_gib model = new listitem_gib();
model.setId(rs.getString(0));
model.setName(rs.getString(1));
model.seturl(rs.getString(3));
model.setimg(rs.getString(2));
model.setnum(rs.getString(4));
model.setsize(rs.getString(5));
list.add(model);
}while (rs.moveToNext());
}
Log.d("rewayat data", list.toString());
return list;
}
public int get_check_List_Favorite(String Title) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor rs = db.rawQuery("select * from favorite Where name like '"+ Title +"'", null);
rs.moveToFirst();
int count = rs.getCount();
return count;
}
public Integer DeleteFav(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("favorite", "id = ?", new String[]{id});
}
<android.support.v7.widget.CardView
android:layout_width="130dp"
android:layout_height="225dp"
app:cardElevation="15dp"
app:cardBackgroundColor="#eae7cb7c"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:cardCornerRadius="8dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img1"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_gravity="center"
android:scaleType="centerCrop"/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/delete"
android:layout_gravity="end">
<ImageButton
android:id="@+id/btn_fav"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@android:drawable/btn_star"
android:background="#00ffffff"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"/>
</FrameLayout>
</FrameLayout>
<TextView
android:id="@+id/Main_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label"
android:layout_gravity="right"
android:textSize="15sp"
android:padding="5dp"
android:layout_marginTop="5dp"
android:textColor="#6a9399"/>
<TextView
android:id="@+id/Second_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LongerLabel"
android:layout_gravity="right"
android:textSize="12sp"
android:padding="5dp"
android:textStyle="bold"
android:textColor="#998c6a"/>
</LinearLayout>
</android.support.v7.widget.CardView>
答案 0 :(得分:1)
在你的onBindViewHolder中,你可以根据需要切换图像。
holder.fav.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(holder.fav.getTag() == R.drawable.fav_icon) {
unfav(position);
holder.fav.setImageResource(R.drawable.unfav_icon);
holder.fav.setTag(R.drawable.unfav_icon);
}
else {
fav(position);
holder.fav.setImageResource(R.drawable.fav_icon);
holder.fav.setTag(R.drawable.fav_icon);
}
}
});
在onBindViewHolder()方法中,您可以添加初始状态:
if(holder.fav.getTag() == R.drawable.fav_icon)
holder.fav.setImageResource(R.drawable.fav_icon);
else
holder.fav.setImageResource(R.drawable.unfav_icon);