Good day!
There is a code:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private LayoutInflater inflater;
private List<PojoClassPrc> prc;
Context context;
RecyclerViewAdapter(Context context, List<PojoClassPrc> procedures) {
this.prc = procedures;
this.inflater = LayoutInflater.from(context);
}
#Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {
PojoClassPrc procedures = prc.get(position);
holder.sName1.setText(procedures.getsText1());
holder.sName3.setText(procedures.getsText3());
holder.sName2.setText(procedures.getsText2());
holder.sName1.setTextColor(Color.parseColor("#010101"));
holder.sName2.setTextColor(Color.parseColor("#ACACAC"));
holder.sName3.setTextColor(Color.parseColor("#737373"));
}
#Override
public int getItemCount() {
return prc.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
final TextView sName1, sName2, sName3;
final CheckBox sName5;
final CardView sName4;
ViewHolder(View view) {
super(view);
sName1 = (TextView) view.findViewById(R.id.lblListItem);
sName5 = (CheckBox) view.findViewById(R.id.checkBox2);
sName3 = (TextView) view.findViewById(R.id.lblListItem3);
sName2 = (TextView) view.findViewById(R.id.lblListItem2);
sName4 = (CardView) view.findViewById(R.id.item_card);
final boolean[] clicked = {false};
SharedPreferences prefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
boolean cbSelect = prefs.getBoolean("sName", false);
if (cbSelect){
sName3.setTextColor(Color.parseColor("#178DFC"));
sName4.setCardBackgroundColor(Color.parseColor("#C9FDFE"));
} else {
sName3.setTextColor(Color.parseColor("#737373"));
sName4.setCardBackgroundColor(Color.WHITE);
}
sName4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!clicked[0]) {
sName5.setChecked(true);
sName3.setTextColor(Color.parseColor("#178DFC"));
sName4.setCardBackgroundColor(Color.parseColor("#C9FDFE"));
clicked[0] = true;
savePrefs(true, "sName");
} else {
sName5.setChecked(false);
sName3.setTextColor(Color.parseColor("#737373"));
sName4.setCardBackgroundColor(Color.WHITE);
clicked[0] = false;
savePrefs(false, "sName");
}
}});
}
}
private void savePrefs(boolean value, String name) {
SharedPreferences.Editor editor = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE).edit();
editor.putBoolean(name, value);
editor.apply();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
}
sName4 is CardView, the point is that I want to implement such a thing:
I clicked on the card, the color of the text changed and the checkbox was set, but in the second press it is necessary to make it as before, say the checkbox is removed, and the color turns white, I can’t realize it in any way with if else, I don’t catch up with what and how, tell me please!
Thank you in advance!!!
P.S. I have a lot of fragments, so I do everything in the adapter
You can use state of CheckBox by calling method isChecked()
Try this:
sName4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (sName3.isChecked()){
sName3.setChecked(false);
sName3.setTextColor(Color.parseColor("#17fc6b")); // set your color
}else{
sName3.setChecked(true);
sName3.setTextColor(Color.parseColor("#178DFC"));
}
}});
more about checkbox
Related
I have a ListView with many items. Each item has a button and when I click on it, it becomes invisible and another button becomes visible.
The issue is: when I scroll the ListView, many other items buttons are invisible. How can I fix it?
public class homebuyer_fruits_adapter extends BaseAdapter {
private ArrayList<Itemssetget> listData;
private LayoutInflater layoutInflater;
public static int cout=0;
List position_item=new ArrayList();
Context context;
String check;
int lastpostition=-1;
public static List<cartitemslist> cartlist=new ArrayList<>();
public homebuyer_fruits_adapter(Context aContext, ArrayList<Itemssetget> listData,String number) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
context=aContext;
check=number;
this.notifyDataSetChanged();
}
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View v, ViewGroup parent) {
final ViewHolder holder;
final cartitemslist homeitemslist = new cartitemslist();
if (v == null) {
v = layoutInflater.inflate(R.layout.food_vegitablews_view, null);
holder = new ViewHolder();
holder.name = (TextView) v.findViewById(R.id.name_item);
holder.price = (TextView) v.findViewById(R.id.price_item);
holder.add_q = (ImageView) v.findViewById(R.id.add_quantity);
holder.delete_q = (ImageView) v.findViewById(R.id.delete_quantity);
holder.quantity = (TextView) v.findViewById(R.id.add_quantity_items);
//holder.description = (TextView) v.findViewById(R.id.description_item);
//holder.id = (TextView) v.findViewById(R.id.);
holder.imageView = (ImageView) v.findViewById(R.id.image_items);
holder.discount = (TextView) v.findViewById(R.id.discout_price);
holder.add = (Button) v.findViewById(R.id.addto_cart);
holder.units=(TextView)v.findViewById(R.id.item_units);
holder.percentage=(TextView)v.findViewById(R.id.discount_percentage);
holder.linearLayou=(LinearLayout)v.findViewById(R.id.Quantity_control_linnear_view) ;
holder.phone=check;
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
this.notifyDataSetChanged();
}
int i=Integer.parseInt(listData.get(position).getPrice())-Integer.parseInt(listData.get(position).getDiscountprice());
int d=i*100;
if((d/Integer.parseInt(listData.get(position).getPrice()))==0)
{
holder.percentage.setVisibility(View.GONE);
}
holder.percentage.setText(""+d/Integer.parseInt(listData.get(position).getPrice())+"% OFF");
holder.name.setText(listData.get(position).getName());
holder.price.setText("Rs " + listData.get(position).getPrice());
if(listData.get(position).getPrice().equals(listData.get(position).getDiscountprice()))
{
holder.price.setVisibility(View.GONE);
}
holder.price.setPaintFlags(holder.price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
holder.discount.setText("Rs " + listData.get(position).getDiscountprice());
Picasso.with(context).load(listData.get(position).getImageurl())
.fit().centerCrop().into(holder.imageView);
holder.units.setText(listData.get(position).getUnits());
holder.add_q.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Toast.makeText(context,listData.get(position).getName(),Toast.LENGTH_LONG).show();
if(H.containsKey(position))
{
holder.quantity.setText(""+(1+Integer.parseInt(holder.quantity.getText().toString())));
cartlist.get(H.get(position)).setQuantity(holder.quantity.getText().toString());
}
}
});
holder.delete_q.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(H.containsKey(position)) {
if(holder.quantity.getText().toString().equals("1"))
{
holder.add.setVisibility(View.VISIBLE);
holder.linearLayou.setVisibility(View.GONE);
int i=H.get(position);
cartlist.remove(i);
cout--;
Fruits.numberofitems.setText(cout + "");
}
else {
holder.quantity.setText("" + (Integer.parseInt(holder.quantity.getText().toString()) - 1));
cartlist.get(H.get(position)).setQuantity(holder.quantity.getText().toString());
}
}
}
});
holder.add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Toast.makeText(context,"Add to cart"+holder.name.getText(),Toast.LENGTH_SHORT).show();
holder.add.setVisibility(View.GONE);
holder.linearLayou.setVisibility(View.VISIBLE);
// check=listData.get(position).getPhonenumber_seller();
if(cartlist.isEmpty()) {
cout++;
homeitemslist.setName(holder.name.getText().toString());
homeitemslist.setPrice(holder.price.getText().toString());
homeitemslist.setDiscountp(holder.discount.getText().toString());
homeitemslist.setQuantity(holder.quantity.getText().toString());
homeitemslist.setPhone_id(holder.phone);
homeitemslist.setImage(listData.get(position).getImageurl());
if(Fruits.numberofitems!=null)
{
Fruits.numberofitems.setText(cout + "");
}
shop_items.numberofitems.setText(cout + "");
buyer_home.numberofitems.setText(cout + "");
cartlist.add(homeitemslist);
H.put(position,cartlist.indexOf(homeitemslist));
}
else {
if(check.equals(cartlist.get(0).getPhone_id()))
{
cout++;
cartitemslist homeitemslist = new cartitemslist();
homeitemslist.setName(holder.name.getText().toString());
homeitemslist.setPrice(holder.price.getText().toString());
homeitemslist.setDiscountp(holder.discount.getText().toString());
homeitemslist.setQuantity(holder.quantity.getText().toString());
homeitemslist.setPhone_id(holder.phone);
homeitemslist.setImage(listData.get(position).getImageurl());
if(Fruits.numberofitems!=null)
{
Fruits.numberofitems.setText(cout + "");
}
shop_items.numberofitems.setText(cout + "");
buyer_home.numberofitems.setText(cout + "");
cartlist.add(homeitemslist);
H.put(position,cartlist.indexOf(homeitemslist));
}
else
{
Toast.makeText(context,"Clear the Previous cart First than you can buy from this shop",Toast.LENGTH_LONG).show();
}
}
}
});
// holder.description.setText(listData.get(position).getDiscription());
// holder.id.setText(listData.get(position).getId());
//holder.imageView.setImageAlpha(R.drawable.banana);
setanimation(v,position);
return v;
}
static class ViewHolder {
LinearLayout linearLayou;
TextView name;
TextView price;
TextView discount;
ImageView add_q;
ImageView delete_q;
TextView quantity;
ImageView imageView;
TextView description;
TextView units;
TextView percentage;
String phone;
Button add;
}
public void setanimation(View viewanim,int position)
{
if(position>lastpostition)
{
ScaleAnimation animation=new ScaleAnimation(0.0f,1.0f,0.0f,1.0f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(1500);
viewanim.setAnimation(animation);
lastpostition=position;
}
}
}
Here is my adapter code.
I tried many ways but still stuck on this issue.
You set the visibility only in the OnClickListener, but you need to set it in getView() too, because your views will get recycled (re-used for a different item) when you scroll.
So in getView() you have to restore the complete state of the view, including button visibility. Store all state information in your list items. Inside your getView() do something like this:
#Override
public View getView(final int position, View v, ViewGroup parent) {
...
// the view (v) could be recycled and showing a state from another item
// e.g. the add button is visible
final Itemssetget listItem = listData.get(position);
// so we have to restore the state of the view for listItem above
// e.g. update the button visibility
holder.add.setVisibility(listItem.isAddButtonVisible() ? View.VISIBLE : View.GONE);
holder.add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
...
holder.add.setVisibility(View.GONE);
// save the state of the button, so it can be restored later
listItem.setAddButtonVisible(true);
...
}
}
...
}
Add the necessary member variables and getter/setter functions in Itemssetget:
public class Itemssetget {
...
private boolean isAddButtonVisible = false;
public boolean isAddButtonVisible() {
return isAddButtonVisible;
}
public void setAddButtonVisible(boolean isVisible) {
this.isAddButtonVisible = isVisible;
}
...
}
PS: Your code looks terrible. Have a look at naming conventions.
I'm creating an app for ordering food, and I created a Dish class and a Dish array adapter.
Currently you can add quantity for a specific dish by clicking on the view.
However, I wish to have 2 buttons to add or sub the quantity.
How can I make the 2 buttons for each dish, without the need to write for each element its ows button code? is there a way to make an "add" and "sub" methods and that the listview will know on which view it was clicked and by that update its quantity?
public Dish(String dishName, int dishPrice, int Image, int quantity) {
mdishName = dishName;
mdishPrice = dishPrice;
mdishPic = Image;
mquantity = quantity;
}
public class DishAdapter extends ArrayAdapter<Dish> {
public DishAdapter(Activity context, ArrayList<Dish> dishes){
super(context, 0, dishes);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_layout, parent, false);
}
final Dish currenDish = getItem(position);
TextView dishName = (TextView) listItemView.findViewById(R.id.dishName);
dishName.setText(currenDish.getDishName());
TextView dishPrice = (TextView) listItemView.findViewById(R.id.dishPrice);
dishPrice.setText(String.valueOf(currenDish.getDishPrice()));
ImageView image = (ImageView) listItemView.findViewById(R.id.dishPic);
image.setImageResource(currenDish.getDishPic());
image.setVisibility(View.VISIBLE);
TextView quantity = (TextView) listItemView.findViewById(R.id.quantity);
quantity.setText(String.valueOf(currenDish.getQuantity()));
return listItemView;
}
public class DesertsActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deserts_activity);
final ArrayList<Dish> dishes = new ArrayList<Dish>();
dishes.add(new Dish("Number Cake",180, R.drawable.cake_number, 0));
dishes.add(new Dish("Ear of Haman", 40, R.drawable.ozen_haman, 0));
dishes.add(new Dish("Alphachores", 35, R.drawable.alphachores, 0));
dishes.add(new Dish("Snow Cookies", 35, R.drawable.snow_cookies, 0));
DishAdapter adapter = new DishAdapter(this, dishes);
final ListView listView = (ListView) findViewById(R.id.deserts_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
dishes.set(position, new Dish(dishes.get(position).getDishName(),
dishes.get(position).getDishPrice(), dishes.get(position).getDishPic(),
dishes.get(position).getQuantity()+1));
TextView quantity = (TextView) view.findViewById(R.id.quantity);
quantity.setText(String.valueOf(dishes.get(position).getQuantity()));
}
});
}
}
You have to create a separate Adaptor Class and then apply button click listeners on the buttons. I am attaching the image for the reference.
and My product Adaptor Is
public class ProductAdaptor extends RecyclerView.Adapter<ProductAdaptor.ViewHolder> {
List<ProductsItem> productsItems;
Context context;
public ProductAdaptor(Context context, List<ProductsItem> productsItems)
{
this.context = context;
this.productsItems = productsItems;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.single_product_design,viewGroup, false);
return new ProductAdaptor.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
final ProductsItem productsItem = productsItems.get(i);
viewHolder.productname.setText(productsItem.getProductName());
Glide.with(context).load(productsItem.getProductImageUrl()).into(viewHolder.productImage);
viewHolder.stock.setText(productsItem.getStock());
viewHolder.farmername.setText(productsItem.getFarmerName());
// add listener on single Item
viewHolder.btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Clicked", Toast.LENGTH_LONG).show();
}
});
viewHolder.btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// apply code to increment the number
// first of all get the value form text counter and increment after that bind on the UI
}
});
viewHolder.btnMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// first of all get the value form text counter and decrement after that bind on the UI
}
});
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getItemCount() {
return productsItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView productname;
ImageView productImage;
TextView stock, farmername, specility,qty;
Button btnPlus, btnMinus;
LinearLayout btnAdd;
public ViewHolder(#NonNull View itemView)
{
super(itemView);
productname = itemView.findViewById(R.id.productname);
productImage = itemView.findViewById(R.id.productimage);
stock = itemView.findViewById(R.id.stcokValue);
farmername = itemView.findViewById(R.id.farmerName);
qty = itemView.findViewById(R.id.qty);
specility = itemView.findViewById(R.id.specility);
btnPlus = itemView.findViewById(R.id.plusbutton);
btnMinus = itemView.findViewById(R.id.minusbtn);
btnAdd = itemView.findViewById(R.id.add_button);
}
}
}
I have a recyclerview that gets data as the realm results and then displays it as recyclerview item. My idea is that when a user clicks on an item a bottomsheet pops up and shows deatils.
Is there a way that in onBindViewHolder I can pass the data to bottomsheet and then in onClick method show bottomsheet? Thanks
Here's my code
public class EmployeeRVAdapterTable extends RecyclerView.Adapter<EmployeeRVAdapterTable.EmployeeViewHolderTable> implements RealmChangeListener {
private RealmResults<Predavanja> mEmployees;
public EmployeeRVAdapterTable(RealmResults<Predavanja> employee) {
this.mEmployees = employee;
mEmployees.addChangeListener(this);
}
#Override
public EmployeeViewHolderTable onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_row_item, parent, false);
return new EmployeeViewHolderTable(view);
}
#Override
public void onBindViewHolder(EmployeeViewHolderTable holder, int position) {
Predavanja predavanja = mEmployees.get(position);
holder.tablename.setText(predavanja.getPredmetPredavanja());
holder.tabletype.setText(predavanja.getRasponVremena());
holder.tablemjesto.setText(predavanja.getDvorana());
switch (predavanja.getPredavanjeIme()){
case("Predavanja,"):
holder.tableboja.setBackgroundResource(R.color.blue_nice);
break;
case("Auditorne vježbe,"):
holder.tableboja.setBackgroundResource(R.color.green_nice);
break;
case("Kolokviji,"):
holder.tableboja.setBackgroundResource(R.color.purple_nice);
break;
case("Laboratorijske vježbe,"):
holder.tableboja.setBackgroundResource(R.color.red_nice);
break;
case("Konstrukcijske vježbe,"):
holder.tableboja.setBackgroundResource(R.color.grey_nice);
break;
case("Seminar,"):
holder.tableboja.setBackgroundResource(R.color.blue_nice);
break;
case("Ispiti,"):
holder.tableboja.setBackgroundResource(R.color.purple_dark);
break;
}
}
#Override
public int getItemCount() {
return mEmployees.size();
}
public class EmployeeViewHolderTable extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView tablename, tabletype, tablemjesto;
RelativeLayout tableboja;
public EmployeeViewHolderTable(View itemView) {
super(itemView);
tablename = (TextView) itemView.findViewById(R.id.table_name);
tabletype = (TextView) itemView.findViewById(R.id.table_type);
tableboja = (RelativeLayout)itemView.findViewById(R.id.colorMe);
tablemjesto = (TextView)itemView.findViewById(R.id.table_mjesto);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Context context = view.getContext();
TextView info, info2,info3;
final BottomSheetDialog dialog;
View views = LayoutInflater.from(context).inflate(R.layout.bottom_sheep, null);
info = (TextView) views.findViewById(R.id.predavanjeImeDialog);
dialog = new BottomSheetDialog(context);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(views);
dialog.show();
}
}
#Override
public void onChange(Object element) {
notifyDataSetChanged();
}
}
The best approach is to get the position of the item and then get that item in the collection and use the data you want.
#Override
public void onClick(View v) {
int position= getAdapterPosition();
String detail= mEmployees.get(position).getDetail();
// Do what you want with the detail
}
I have to hide all the checkboxes for every [Position] product until User click button. When ever user clicks button check boxes will be show to select items for delete. Only check box will appear to change not whole grid view.
public class CartAdapter extends BaseAdapter {
Context context;
ArrayList<ProductCount> productCounts;
private LayoutInflater inflater;
private ImageButton plusButton;
private ImageButton minusButton;
private CheckBox selectToDelete;
private onDeleteCartItem onDeleteCartItem = null;
public CartAdapter(Context context, ArrayList<ProductCount> productCounts, onDeleteCartItem selectChangeListener) {
this.context = context;
this.productCounts = productCounts;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.onDeleteCartItem = selectChangeListener;
}
#Override
public int getCount() {
if(productCounts!=null)
return productCounts.size();
return 0;
}
#Override
public Object getItem(int position) {
if(productCounts!=null && position >=0 && position<getCount())
return productCounts.get(position);
return null;
}
#Override
public long getItemId(int position) {
if(productCounts!=null && position >=0 && position<getCount()){
ProductCount temp = productCounts.get(position);
return productCounts.indexOf(temp);
}
return 0;
}
public class ProductsListHolder{
public ImageView cart_item_img;
public TextView cart_item_desc;
public TextView cart_item_count;
public TextView cart_item_price_tag;
public TextView cart_item_price;
public ImageButton cart_item_minus;
public ImageButton cart_item_plus;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ProductsListHolder productsListHolder;
if(view == null){
view = inflater.inflate(R.layout.cart_adapter, parent, false);
productsListHolder = new ProductsListHolder();
productsListHolder.cart_item_img = (ImageView) view.findViewById(R.id.cart_item_img);
productsListHolder.cart_item_desc = (TextView) view.findViewById(R.id.cart_item_desc);
productsListHolder.cart_item_count = (TextView) view.findViewById(R.id.cart_item_count);
productsListHolder.cart_item_price_tag = (TextView) view.findViewById(R.id.cart_item_price_tag);
productsListHolder.cart_item_price = (TextView) view.findViewById(R.id.cart_item_price);
plusButton = (ImageButton) view.findViewById(R.id.cart_item_plus);
minusButton = (ImageButton) view.findViewById(R.id.cart_item_minus);
selectToDelete = (CheckBox) view.findViewById(R.id.select_to_delete);
selectToDelete.setTag(position);
view.setTag(productsListHolder);
}
else{
productsListHolder = (ProductsListHolder) view.getTag();
}
final ProductCount cat = productCounts.get(position);
selectToDelete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
if(onDeleteCartItem != null){
onDeleteCartItem.onSelectToDelete((Integer)buttonView.getTag(),isChecked);
}
}
}
});
minusButton.setOnClickListener(new View.OnClickListener() {
int itemcount = 0;
#Override
public void onClick(View v) {
itemcount = productCounts.get(position).getCount();
productCounts.get(position).setCount(itemcount-1);
setProduct(position,productsListHolder,cat);
}
});
plusButton.setOnClickListener(new View.OnClickListener() {
int itemcount = 0;
#Override
public void onClick(View v) {
itemcount = productCounts.get(position).getCount();
productCounts.get(position).setCount(itemcount+1);
setProduct(position,productsListHolder,cat);
}
});
setProduct(position,productsListHolder,cat);
return view;
}
private void setProduct(int position, final ProductsListHolder productsListHolder, ProductCount pCount) {
Picasso.with(context).load(pCount.products.getImageResours()).into(new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
productsListHolder.cart_item_img.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
}
});
productsListHolder.cart_item_desc.setText(pCount.getProducts().getDescription());
productsListHolder.cart_item_price_tag.setText((String.valueOf(pCount.getCount()).concat(" x Rs. ").concat(String.valueOf((pCount.products.getPrice())))));
productsListHolder.cart_item_price.setText("Rs. ".concat(String.valueOf(pCount.getCount()* pCount.products.getPrice())));
productsListHolder.cart_item_count.setText(String.valueOf(pCount.getCount()));
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
Just handling your checkBox in your adapter because you got the position then can hide/show, OnClickListener, OnCheckedChangeListener however you want.
Reference to my answer here: Is there a simple way to check all CheckBox items in custimize BaseAdapter in android?
Android recyclerview item click event after update Recyclerview
Image description here
Mail 1 READ
Mail 2 Dont Read
In the item the click event I want to do this to me
How can you help make
public class Mesajlar_Adapter extends RecyclerView.Adapter<Mesajlar_Adapter.ViewHolder>{
private List<Mesajlar> resultModels;
private Context context;
public Mesajlar_Adapter(List<Mesajlar> resultModels, Context context) {
this.resultModels = resultModels;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.mesajlar_listview_desing, parent, false);
ViewHolder view_holder = new ViewHolder(v);
return view_holder;
}
public void resetList() {
int size = this.resultModels.size();
if (size > 0) {
for (int i = 0; i < size; i++) {
resultModels.remove(0);
}
this.notifyItemRangeRemoved(0, size);
}
}
public void addItem(Mesajlar item){
this.resultModels.add(item);
this.notifyDataSetChanged();
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mesaj_id.setText(resultModels.get(position).getMESAJ_ID());
holder.kullanici_id.setText(resultModels.get(position).getKULLANICI());
holder.gelen_detay.setText(resultModels.get(position).getDETAY());
holder.kategori.setText(resultModels.get(position).getKATEGORI());
holder.konu.setText(resultModels.get(position).getKONU());
holder.tarih.setText(resultModels.get(position).getKAYIT_TARIHI());
String a = resultModels.get(position).getOKUNDU();
holder.okunma.setText(a);
if (a.equals("Hayir")){
holder.kategori.setTextColor(Color.BLACK);
holder.kategori.setTextSize(19);
holder.konu.setTextColor(Color.BLACK);
holder.konu.setTextSize(19);
holder.tarih.setTextColor(Color.BLACK);
}
}
#Override
public int getItemCount() {
return resultModels.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
protected TextView mesaj_id;
protected TextView kullanici_id;
protected TextView gelen_detay;
protected TextView konu;
protected TextView kategori;
protected TextView tarih;
protected CheckBox secim;
protected TextView okunma;
public ViewHolder(View view) {
super(view);
this.okunma =(TextView) view.findViewById(R.id.okunma);
this.mesaj_id = (TextView) view.findViewById(R.id.mesaj_id);
this.kullanici_id = (TextView) view.findViewById(R.id.kullanici_id);
this.gelen_detay = (TextView) view.findViewById(R.id.gelen_detay);
this.konu = (TextView) view.findViewById(R.id.Konu);
this.kategori = (TextView) view.findViewById(R.id.Kategori);
this.tarih = (TextView) view.findViewById(R.id.Tarih);
// this.secim = (CheckBox) view.findViewById(R.id.secim);
view.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Intent i=new Intent(context, Mesaj_Detay_Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String detay = gelen_detay.getText().toString();
String mesajid = mesaj_id.getText().toString();
String kullaniciid = kullanici_id.getText().toString();
i.putExtra("detay", detay);
i.putExtra("mesaj_id",mesajid);
i.putExtra("kullanici_id",kullaniciid);
context.startActivity(i);
}
}
}
First of all, add this interface for click event
public interface OnRecyclerItemViewClickListener {
void onClicked(int position, Mesajlar jar);
}
Model
public class Mesajlar {
...
boolean isClicked;
public boolean isClicked() {
return icClicked;
}
public void setClicked(boolean isClicked) {
this.isClicked = isClicked;
}
...
}
Adapter
private OnRecyclerItemViewClickListener listener;
public Mesajlar_Adapter(List<Mesajlar> resultModels, Context context, OnRecyclerItemViewClickListener listener) {
...
this.listener = listener;
}
...
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
if (resultModels.get(position).isClicked()) {
// change pressed background color here!
} else {
// change default background color here!
}
holder.itemView.setOnClickListener(v -> {
listener.onClicked(position, resultModels.get(position));
});
...
}
Activity
When you create RecyclerView Adapter, add OnRecyclerItemViewClickListener to constructor of adapter in Activity
Mesajlar_Adapter adapter = new Mesajlar_Adapter(list, context, new OnRecyclerItemViewClickListener {
#Override
public void onClicked(int position, Mesajlar jar) {
jar.setClicked(true);
adapter.notifyItemChanged(position);
Intent i = new Intent(YourActivity.this, Mesaj_Detay_Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("detay", jar.getDETAY());
i.putExtra("mesaj_id",jar.getMESAJ_ID());
i.putExtra("kullanici_id", jar.getKULLANICI());
startActivity(i);
}
});
Instead of handling click event in adapter level you can use interface and do it in activity level. Pass the adapter position to the activity, from activity you can do the actual process
see the sample http://wiki.workassis.com/android-recyclerview-example/