Why horizontal Items in RecycleView doesn't work onClick? - java

[Code below] Item in RecycleView doesn't click [it should open the DetailActivity], I try to find the mistake but I didn't understand where
What I try :
Change size items
Change RecycleView to VERTICAL
Write Log.v
I have the same RecycleViewAdapter but it vertical, and it is working well.
my all project : https://github.com/sanke46/E-Commerce
RecycleViewAdapter :
public class SalesRecyclerViewAdapter extends RecyclerView.Adapter<SalesRecyclerViewAdapter.ViewHolder> {
BasketActivity basketActivity = new BasketActivity();
private List<Item> itemList = basketActivity.getBasketItem();
private ArrayList arr;
private Context mContext;
public SalesRecyclerViewAdapter(Context context, ArrayList<Item> data) {
this.arr = data;
mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sale, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final Item item = (Item) arr.get(position);
Picasso.with(mContext).load(item.getImageUrl()).into(holder.imageView);
holder.price.setText(item.getPrice() + " $");
holder.price.setPaintFlags(holder.price.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
holder.fixPrice.setText(item.getDiscontPrice() + " $");
holder.name.setText(item.getName());
holder.comment.setText(item.getComment());
holder.gramm.setText(item.converGramms(String.valueOf(item.getGramms())));
holder.kal.setText(item.getKalories() + " kal");
holder.addToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
itemList.add((Item) arr.get(position));
basketActivity.setBasketItem(itemList);
}
});
holder.linerSaleClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext, DetailActivity.class);
intent.putExtra("item", item);
mContext.startActivity(intent);
Log.v("SALES", "SALES");
}
});
}
#Override
public int getItemCount() {
return arr.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private RelativeLayout linerSaleClick;
private ImageView imageView;
private TextView price;
private TextView fixPrice;
private TextView name;
private TextView comment;
private TextView gramm;
private TextView kal;
private Button addToCart;
public ViewHolder(View itemView) {
super(itemView);
linerSaleClick = itemView.findViewById(R.id.linerSaleClick);
imageView = itemView.findViewById(R.id.imageSale);
price = itemView.findViewById(R.id.price);
kal = itemView.findViewById(R.id.kal);
gramm = itemView.findViewById(R.id.gramm);
name = itemView.findViewById(R.id.name);
comment = itemView.findViewById(R.id.comments);
fixPrice = itemView.findViewById(R.id.fixPrice);
addToCart = itemView.findViewById(R.id.buttonTwo);
}
}}

Basically code on your github is different than what you posted. So probably the reason of problem is:
In list_sale.xml
I noticed that in this view com.makeramen.roundedimageview.RoundedImageView you set parametr android:clickable="true" then if you want to rise click event on parent (RelativeLayout) it will not work. Solution: remove this line android:clickable="true".

Related

Changing ImageView of selected item in recyclerview in many time

I'm trying to change an ImageView in my RecyclerView. I need to change ImageView every time I click the RecyclerView. So, when I click it once it changes to a tick, then I click it again it changes again to be like the beginning, and when I click it again I want to bring up the tick like when I pressed it the first time.
public class RecyclerViewAdapter1 extends RecyclerView.Adapter<RecyclerViewAdapter1.ViewHolder> {
private LayoutInflater inflater;
private FragmentCommunication mCommunicator;
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<String> arrayList;
private ArrayList<Integer> IDList;
private ArrayList<Integer> polikhusus;
private int tekan = 0;
private GlobalClass globalVariable;
private static int lastClickedPosition = -1;// Variable to store the last clicked item position
RecyclerViewAdapter1(ArrayList<String> pintuMasuk, ArrayList<Integer> PoliID, ArrayList<Integer> poli, OnTextClickListener listener) {
this.arrayList = pintuMasuk;
this.IDList = PoliID;
this.polikhusus = poli;
this.mListener = listener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_poli_design, parent, false);
ViewHolder VH = new ViewHolder(V);
return VH;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView PintuMasuk;
private ImageView Meme2;
private RelativeLayout ItemList3;
private int selectedIndex;
private Integer PoliID;
public ViewHolder(View itemView) {
super(itemView);
PintuMasuk = itemView.findViewById(R.id.poli);
Meme2 = itemView.findViewById(R.id.gbpoli);
ItemList3 = itemView.findViewById(R.id.item_list3);
}
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final String PintuMasuk = arrayList.get(position);
final Integer PoliID = IDList.get(position);
final Integer poli = polikhusus.get(position);
holder.PintuMasuk.setText(PintuMasuk);
holder.ItemList3.setTag(position);
holder.Meme2.setImageResource(R.drawable.pintumasuk);
holder.ItemList3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
globalVariable = (GlobalClass) view.getContext().getApplicationContext();
globalVariable.PoliID = PoliID;
if(position != lastClickedPosition){
holder.Meme2.setImageResource(R.drawable.policheck);
Snackbar.make(view, "Anda memilih : "+PoliID+" "+PintuMasuk, Snackbar.LENGTH_SHORT).show();
Log.i(LOG_TAG," Clicked on Item " + position);
}else {
// for cancel
Snackbar.make(view, "Anda membatalkan pilihan Anda", Snackbar.LENGTH_SHORT).show();
globalVariable.PoliID = 0;
}
if (lastClickedPosition != -1)
notifyItemChanged(lastClickedPosition);
lastClickedPosition = position;
}
});
}
#Override
public int getItemCount () {
return this.arrayList.size();
}
}
enter image description here

On Clicking one button, it Triggers another button in recycle view

I'm currently working on a attandance management Android app. In my recyclerview there are list of student detail being populated dynamically from database. When i click the first button in the list, it also change the button colour of the 11th button of the list Like wise to 2nd button click changes the colour of 12th View button
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;`enter code here`
List<StudentDetails> MainImageUploadInfoList;
public RecyclerViewAdapter(Context context, List<StudentDetails> TempList) {
this.MainImageUploadInfoList = TempList;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
StudentDetails studentDetails = MainImageUploadInfoList.get(position);
holder.StudentNameTextView.setText(studentDetails.getName());
holder.StudentNumberTextView.setText(studentDetails.getPhoneNumber());
holder.st.setText(studentDetails.getRollno());
holder.cardView.setCardBackgroundColor(getcolor(position));
}
private int getcolor(int position) {
return Color.parseColor("#" + Integer.toHexString(ContextCompat.getColor(context, R.color.normal)));
}
#Override
public int getItemCount() {
return MainImageUploadInfoList.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView StudentNameTextView;
public TextView StudentNumberTextView;
public TextView st;
DatabaseReference databaseReference;
String se,wh,su,d;
Button b;
CardView cardView;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
StudentNameTextView = (TextView) itemView.findViewById(R.id.ShowStudentNameTextView);
StudentNumberTextView = (TextView) itemView.findViewById(R.id.ShowStudentNumberTextView);
st = (TextView) itemView.findViewById(R.id.studentrollno);
cardView=itemView.findViewById(R.id.cardview1);
se=Teacher.getInstance().getSe();
wh=Teacher.getInstance().getWh();
su=Teacher.getInstance().getSu();
d=Teacher.getInstance().getD();
b=itemView.findViewById(R.id.button3);
itemView.findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view ) {
int position=getAdapterPosition();
String message = String.valueOf(position) ;
databaseReference= FirebaseDatabase.getInstance().getReference("attandance").child(wh).child(se).child(su).child(d).child(message);
databaseReference.setValue("p");
itemView.findViewById(R.id.button3).setBackgroundColor(Color.GREEN);
itemView.findViewById(R.id.button4).setEnabled(false);
itemView.findViewById(R.id.button3).setEnabled(false);
Toast.makeText( context,"position is "+String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
itemView.findViewById(R.id.button4).setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(final View view ) {
int position=getAdapterPosition();
String message = String.valueOf(position) ;
databaseReference= FirebaseDatabase.getInstance().getReference("attandance").child(wh).child(se).child(su).child(d).child(message);
databaseReference.setValue("A");
itemView.findViewById(R.id.button4).setBackgroundColor(Color.RED);
itemView.findViewById(R.id.button3).setEnabled(false);
itemView.findViewById(R.id.button4).setEnabled(false);
}
});
}
#Override
public void onClick(View view) {
// int position=this.getAdapterPosition();
// Intent intent=new Intent(context,mark.class);
// String message = String.valueOf(position);
// intent.putExtra("attan",message);
// context.startActivity(intent);
// Intent intent = new Intent(context,mark.class);
//String message = String.valueOf(position) ;
//intent.putExtra("attan",message);
//context.startActivity(intent);
// Toast.makeText( context,"position is "+String.valueOf(position), Toast.LENGTH_SHORT).show();
}
}
}
}

I need to implementation click listener class on item in recycle view when using adapter and array list

I want when user click on item in recycle move on second activity. I have the following adapter:
public class AdapterWassafat extends RecyclerView.Adapter<AdapterWassafat.ViewHolder> {
private ArrayList<Item> itemsList;
private Context mContext;
public AdapterWassafat(ArrayList<Item> itemsList, Context mContext) {
this.itemsList = itemsList;
this.mContext = mContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wasafat,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Item item = itemsList.get(position);
holder.text.setText(item.getText());
}
#Override
public int getItemCount() {
return itemsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView text;
public ViewHolder(#NonNull View itemView) {
super(itemView);
text = itemView.findViewById(R.id.text_card_main);
}
}
}
and I have this class to add on item list:
public class Item {
private String text;
public Item(String text) {
this.text = text;
}
public Item() {
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
and this class has recycle:
private AdapterWassafat adapter;
private RecyclerView recyclerView ;
private ArrayList<Item> myList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wasafat);
recyclerView = findViewById(R.id.wasafat_recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
adapter = new AdapterWassafat(getMyitem(),getBaseContext());
recyclerView.setAdapter(adapter);
}
private ArrayList<Item> getMyitem() {
ArrayList<Item> list = new ArrayList<>();
Item tips = new Item();
tips.setText("وصفة التمر ");
list.add(tips);
tips = new Item();
tips.setText("وصفة اللبن ");
list.add(tips);
tips = new Item();
tips.setText("وصفة الشاي الاخضر ");
list.add(tips);
return list;}
What do I need to make a recycle view when user click on item move on another activity? Like, as user clicks on item وصفة الشاي, to swap or move on into this activity and so on. I tried a lot of ways to make it both select or click, but that didn't succeed.
In the Adapter:
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Item item = itemsList.get(position);
Context context = holder.itemView.getContext();
holder.text.setText(item.getText());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = Intent(context, YourActivity.class);
intent.putExtra("item", item.getText())
context.startActivity(intent);
}
});
}
In your Other Activity, get data as:
// onCreate()
String value = getIntent().getStringExtra("item", "");

How to load image from an arraylist?

I can't get the layout.xml to show all of the images, it only shows the image that I added into the android:src="blabla.jpg" and all of the list use "blabla.jpg".
I can show the text from the arraylist perfectly fine, but the problem is only the image so i tried this code
public RecyclerViewAdapter(Context mContext,ArrayList<Integer> mImageHolder,ArrayList<String> mItemHolder, ArrayList<String> mDescHolder) {
this.mImageHolder = mImageHolder;
this.mItemHolder = mItemHolder;
this.mDescHolder = mDescHolder;
this.mContext = mContext;
}
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> mItemHolder = new ArrayList<>();
private ArrayList<String> mDescHolder = new ArrayList<>();
private ArrayList<Integer> mImageHolder = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(Context mContext,ArrayList<Integer> mImageHolder,ArrayList<String> mItemHolder, ArrayList<String> mDescHolder) {
this.mImageHolder = mImageHolder;
this.mItemHolder = mItemHolder;
this.mDescHolder = mDescHolder;
this.mContext = mContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_list_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: called.");
holder.itemHolder.setText(mItemHolder.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked on: " + mItemHolder.get(position));
//ini ke intent
Intent intent = new Intent(mContext, ListDetail.class);
Bundle extras = new Bundle();
extras.putString("EXTRA_ITEM", mItemHolder.get(position));
extras.putString("EXTRA_DESC", mDescHolder.get(position));
extras.putInt("EXTRA_IMAGE", mImageHolder.get(position));
intent.putExtras(extras);
mContext.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return mItemHolder.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView itemHolder;
ImageView imageHolder;
RelativeLayout parentLayout;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageHolder = itemView.findViewById(R.id.ImageItem);
itemHolder = itemView.findViewById(R.id.NameItem);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
}
Any help would be appreciated,
In your onBindViewHolder add:
holder.imageHolder.setImageResource(image);
use the setImageresource property of imageView and get the image from the list of that position inside onBindViewHolder.
holder.imageHolder.setImageResource(mImageHolder.get(position));

Select only one image in RecyclerView

I have a RecyclerView.Adapter which has some Arrays there.
ArrayList with Strings and ArrayList with Integer. Strings are like url and Integer is the photo.
When the app is open for first time the first item is selected.
I have another method for click which makes another item as selected and this works, but the problem is that the first item stays as selected and so for every image click makes as selected, I want only one item to be selected and take a color.
This is my code.
Adapter of RecyclerView
public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder>{
private int selectedItem;
private ArrayList<Integer> mImages = new ArrayList<>();
private ArrayList<String> mSearchUrl = new ArrayList<>();
private Context mContext;
public ListViewAdapter(ArrayList<Integer> images, ArrayList<String> SearchUrl, Context context) {
mImages = images;
mContext = context;
mSearchUrl = SearchUrl;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.s_engine_item, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, final int i) {
selectedItem = 0;
if (selectedItem == i) {
viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
}
Glide.with(mContext).load(mImages.get(i))
.into(viewHolder.image);
viewHolder.searchUrl.setText(mSearchUrl.get(i));
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
selectedItem = i;
}
});
}
#Override
public int getItemCount() {
return mImages.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView searchUrl;
public ViewHolder(#NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.ivEngine);
searchUrl = itemView.findViewById(R.id.ivEngineText);
}
}
}
And this is the MainActivity.class
public void intSearch() {
mImages.add(R.drawable.s_bing);
mSearchUrl.add("https://www.bing.com/search?q=");
mImages.add(R.drawable.s_google);
mSearchUrl.add("https://www.google.com/search?q=");
mImages.add(R.drawable.s_yahoo);
mSearchUrl.add("www.yahoo.com");
mImages.add(R.drawable.amazon_white256);
mSearchUrl.add("www.amazon.com");
mImages.add(R.drawable.amazon_white256);
mSearchUrl.add("www.amazon.com");
mImages.add(R.drawable.amazon_white256);
mSearchUrl.add("www.amazon.com");
initRecyclerView();
}
private void initRecyclerView() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = findViewById(R.id.lvEngines);
recyclerView.setLayoutManager(layoutManager);
ListViewAdapter adapter = new ListViewAdapter(mImages, mSearchUrl, this);
recyclerView.setAdapter(adapter);
}
Initialize your selected item globally
public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder>{
private int selectedItem = 0;
.....
Then inside your onBindViewHolder whenever you click a new Image notify your adapter for changes in the last selected item cell.
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousSelectedItem = selectedItem;
selectedItem = i;
notifyItemChanged(previousSelectedItem);
viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
}
});
Just remove this line from onBindViewHolder
selectedItem = 0;
and add an else to the background condition, like:
if (selectedItem == i) {
viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
}else{
viewHolder.image.setBackgroundColor(“YOUR_DEFAULT_COLOR”);
}
and update the onClick:
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedItem = i;
notifyDataSetChanged();
}
});

Categories