fekra fekra - 19 days ago 4
Android Question

Change imagebutton when pressed in RecyclerView

I added ImageButton in CardView in RecyclerView and i made it to add item in favourite (i made sqlite to save favourite ) now i want to make when i open activity that RecyclerView on it show ImageButton with different image if it in Favourite and when pressed on it change image
simply i want to make favourite button (Star image ) like gmail app
enter image description here

RecyclerView Adapter



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();
}

}
}


Database



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


CardLayout





<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>



Answer

In your onBindViewHolder,, you can just switch the image as needed.

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

In your onBindViewHolder() method, you can add the initial state :

if(holder.fav.getTag() == R.drawable.fav_icon)
    holder.fav.setImageResource(R.drawable.fav_icon);
else
    holder.fav.setImageResource(R.drawable.unfav_icon);