Android Recycler View OnClick event is not working in the code. I can see the Recycler view grid but when I am clicking on one item nothing is happening.
Here is my code:
public class StaggeredGridAdapter extends RecyclerView.Adapter<StaggeredGridAdapter.StaggeredGridView> {
private Context context;
private List<Warehouse> warehouses = new ArrayList<Warehouse>();
int size;
public StaggeredGridAdapter(Context context) {
this.context = context;
}
public void addItems(List<Warehouse> response) {
size = response.size();
warehouses = response;
}
#Override
public StaggeredGridView onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
StaggeredGridView staggeredGridView = new StaggeredGridView(layoutView);
return staggeredGridView;
}
#Override
public void onBindViewHolder(StaggeredGridView holder, int position) {
holder.textView.setText(warehouses.get(position).getFace());
}
#Override
public int getItemCount() {
return size;
}
class StaggeredGridView extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textView;
public StaggeredGridView(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.img_name);
}
#Override
public void onClick(View v) {
Toast.makeText(context, "clicked", Toast.LENGTH_SHORT).show();
}
}
}
I inherited inner class i.e. StaggeredGridView with Views.OnClickListner and debug point is not reaching to StaggeredGridView.OnClick method. There is no exception in ADB logs.
What I am doing wrong here?
You need to do this simple thing and it will start working with you
itemView.setOnClickListener(this)
//add this
public StaggeredGridView(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.img_name);
staggeredGridView.setOnClickListener(this)
}
Related
I have an adapter class Where i Want to show a BLOCK USER dialog on particular position.
for example if i want to block position 1 of recyclerView .
So can any one help me porgrammatically?
Given below is the Code Of adapter Please check
I want to block user profile based on Recycler View position
if someone could help me in it please;
public class TableProfileAdapter extends RecyclerView.Adapter<TableProfileAdapter.MyViewHolder> {
Context context;
Activity getActivity;
List<TableProfile> profileUser;
OnRecItemClick onRecItemClick;
private boolean isScaleAnimationDone = false;
private boolean isTimerTextViewActionUpCalled = false;
public TableProfileAdapter(List<TableProfile> profileUser, Context context) {
this.profileUser = profileUser;
this.context = context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.profile_item, parent, false);
return new MyViewHolder(view, onRecItemClick);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
TableProfile data = profileUser.get(position);
holder.tvUserName.setText(data.getProfileName());
Bitmap image = BitmapManager.byteToBitmap(data.getProfileImage());
holder.profilePic.setImageBitmap(image);
if (context instanceof AccessProfileManagementActivity) {
context.startActivity(new Intent(context,HomeActivity.class));
} else if (context instanceof ProfileManagement) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, ProfileDetailsActivity.class);
TableProfile model = profileUser.get(holder.getAdapterPosition());
intent.putExtra("DATA", model);
context.startActivity(intent);
notifyDataSetChanged();
((Activity) context).finish();
}
});
}
}
#Override
public int getItemCount() {
return profileUser.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CircleImageView profilePic;
TextView tvUserName, birthday;
OnRecItemClick onRecItemClick;
RelativeLayout relativeLayout;
public MyViewHolder(#NonNull View itemView, OnRecItemClick onRecItemClick) {
super(itemView);
profilePic = itemView.findViewById(R.id.profileImage);
tvUserName = itemView.findViewById(R.id.userName);
relativeLayout = itemView.findViewById(R.id.relTop);
this.onRecItemClick = onRecItemClick;
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
onRecItemClick.onItemClick(getAdapterPosition());
}
}
public interface OnRecItemClick {
void onItemClick(int position);
}
}
I'm trying to get a View position through his view hierarchy.
When I have a RecyclerView, for get the current view position I do:
if(view.getParent() instanceof RecyclerView){
RecyclerView rv = (RecyclerView)view.getParent();
if(rv.getLayoutManager()!=null) {
position = rv.getLayoutManager().getPosition(view);
}
}
But when a ListView or GridView appears, I'm not able to get a Layout Manager to get the position of the view. The idea is to make it in a generic way, without knowing the Custom Adapter the view has.
I have tried to get the position through the ListView and GridView adapter but I dont see any method that do that.
Someone can help me?
Thank you a lot!
Create interface for get position for outside adapter class
here is adapter code:
public class ReportMediaAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<File> reportMediaList = new ArrayList<>();
private Context context;
private int selectedPos = -1;
private OnRemoveImage onRemoveImage;
public ReportMediaAdapter(Context context, ArrayList<File> reportMediaList, OnRemoveImage onRemoveImage) {
this.reportMediaList = reportMediaList;
this.onRemoveImage = onRemoveImage;
this.context = context;
}
**public interface OnRemoveImage {
void onRemove(int pos);
}**
public class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView iv_image, iv_remove;
public MyViewHolder(View itemView) {
super(itemView);
iv_image = itemView.findViewById(R.id.iv_image);
iv_remove = itemView.findViewById(R.id.iv_remove);
}
}
#Override
public int getItemCount() {
return reportMediaList.size()-1;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_media, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
try {
MyViewHolder menuItemHolder = (MyViewHolder) holder;
CommonMethods.loadImage(context, reportMediaList.get(position), menuItemHolder.iv_image);
menuItemHolder.iv_remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
**onRemoveImage.onRemove(position);**
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here is activity code:
#Override
public void onRemove(int pos) {
// use adapter position
}
I have found a solution:
if(view.getParent() instanceof ViewGroup) {
position = ((ViewGroup)view.getParent()).indexOfChild(view);
}
I'm creating an app with firebase as a backend. I'm using same adapter for different activity, now it is working good but positions were mismatched for example: The output of position 0 shows in position 1 and for position 1 shows in position 2 and so on.. How to solve this problem, The problem with the positions of the output.
Adapter:
public class FrontlistAdapter extends FirebaseRecyclerAdapter<Gamedata, FrontlistAdapter.ViewHolder> {
private static final String TAG = "GameAdapter";
Context mContext;
int positions;
public FrontlistAdapter(#NonNull FirebaseRecyclerOptions<Gamedata> options, Context context) {
super(options);
mContext = context;
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull Gamedata model) {
holder.name.setText(model.getName());
holder.address.setText(model.getAddress());
//Picasso.get().load(model.getFrontcover()).into(holder.Frontcover);
Glide.with(mContext).load(model.getFrontcover()).into(holder.Frontcover);
positions = position; //<-- here I'm having positions but output shows different for each positions
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View v = layoutInflater.inflate(R.layout.gamerow,parent,false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView Frontcover;
TextView name;
TextView address;
View v;
public ViewHolder(#NonNull View itemView) {
super(itemView);
Frontcover = (ImageView)itemView.findViewById(R.id.frontcover);
name = (TextView)itemView.findViewById(R.id.frontname);
address=(TextView)itemView.findViewById(R.id.frontaddress);
v = itemView;
itemView.setClickable(true);
itemView.setOnClickListener((View.OnClickListener) this);
}
#Override
public void onClick(View v) {
String className = mContext.getClass().getSimpleName();
Intent intent = null;
switch (className) {
case "GameActivity":
intent = new Intent(mContext, Center_details.class);
intent.putExtra("User", getRef(positions).getKey());
break;
case "KidsActivity":
intent = new Intent(mContext,AllCenterDetails.class);
intent.putExtra("Kids",getRef(positions).getKey());
break;
}
mContext.startActivity(intent);
}
}
}
first what you have to do is create the interface like this
public interface ClickListner {
void onClick(View view, int position);
}
then in your activity create object of the interface like this and pass to the adapter along with your other data
ClickListner listener = ClickListner();
FrontlistAdapter customAdapter = new
FrontlistAdapter(AcceptedOrdersActivity.this,listener);
riderView.setAdapter(customAdapter);
copy this outside onCreate and intent in this
private ClickListner ClickListner() {
ClickListner listener = new ClickListner() {
#Override
public void onClick(View view, int position) {
int tag = (int) view.getTag();
if(tag == 0)
{
//Intent here
}
}
};
return listener;
}
now your adapter should be this one
public class FrontlistAdapter extends
RecyclerView.Adapter<FrontlistAdapter .DataObjectHolder>{
ClickListner listenr;
Context context;
public FrontlistAdapter (Context context,ClickListner listenr) {
this.context = context;
this.listenr = listenr;
}
public static class DataObjectHolder extends RecyclerView.ViewHolder {
Context context;
public DataObjectHolder(View itemView,ClickListner listenr) {
super(itemView);
this.listenr = listenr;
Frontcover = (ImageView)itemView.findViewById(R.id.frontcover);
name = (TextView)itemView.findViewById(R.id.frontname);
address=(TextView)itemView.findViewById(R.id.frontaddress);
v = itemView;
setOnClickListeners();
}
private void setOnClickListeners() {
`v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setTag(0);
listenr.onClick(v, getAdapterPosition());
}
});
}
}
#NonNull
#Override
public FrontlistAdapter.DataObjectHolder onCreateViewHolder(#NonNull ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cell_orderdetail_list, parent, false);
FrontlistAdapter.DataObjectHolder dataObjectHolder = new
FrontlistAdapter.DataObjectHolder(view,listenr);
return dataObjectHolder;
}
#Override
public void onBindViewHolder(#NonNull FrontlistAdapter.DataObjectHolder holder, int
position) {
}
#Override
public int getItemCount() {
return options.size;
}
}
positions = position;
This is your problem. You're setting one global position for everything in the adapter.
Everywhere you are currently using position, you should instead be calling getBindingAdapterPosition() on the ViewHolder.
I want to make a onItemclicklistener method to recycleview of buttons and every button intent to Activity
but the Activity has Textview (every button has different Textview )
public class RecAdapter extends RecyclerView.Adapter<RecAdapter.ViewHolder> {
public class ViewHolder extends RecyclerView.ViewHolder {
Button button;
public ViewHolder(View itemView) {
super(itemView);
button = itemView.findViewById(R.id.buttonn);
}
}
private Context context;
private List<items> items;
public RecAdapter(Context c, List<items> itemList) {
this.context = c;
items = itemList;
}
#Override
public RecAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.buttonss, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(RecAdapter.ViewHolder holder, int position) {
items p = items.get(position);
holder.button.setText(p.getName());
}
#Override
public int getItemCount() {
return items.size();
}
}`
Add OnClickListener on itemView of holder like below:
#Override
public void onBindViewHolder(RecAdapter.ViewHolder holder, int position) {
items p = items.get(position);
holder.button.setText(p.getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do here
}
});
}
I have a recyclerview in ShoppingCartActivity and a button in ProductDetailActivity. I want to send some data to ShoppingCartActivity when a button clicked in ProductDetailActivity and display them in recyclerview item that creates dynamically in ShoppingCartActivity.
I added addData method to recyclerview Adapter but it doesn't work and app crashes.
shop_btn = findViewById(R.id.add_to_shopping_cart);
shop_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent addToCart = new Intent(ProductDetailActivity.this, ShoppingCartActivity.class);
startActivity(addToCart);
}
});
//HERE IS MY RECYCLERVIEW ADAPTER
public class RecyclerViewAdapter_ShoppingCart extends RecyclerView.Adapter<RecyclerViewAdapter_ShoppingCart.MyViewHolder> {
private Context context;
List<CartItems> cartItemsList;
public RecyclerViewAdapter_ShoppingCart(Context context, List<CartItems> cartItemsList) {
this.context = context;
this.cartItemsList = cartItemsList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.card_cart, parent, false);
return new MyViewHolder(view);
}
#NonNull
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, final int position) {
holder.cart_title.setText(cartItemsList.get(position).getTitle());
holder.cart_price.setText(cartItemsList.get(position).getPrice());
Picasso.get().load(cartItemsList.get(position).getImageUrl()).into(holder.cart_img);
}
#Override
public int getItemCount() {
return cartItemsList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView cart_title, cart_price;
ImageView cart_img;
public MyViewHolder(View itemView) {
super(itemView);
cart_title = itemView.findViewById(R.id.cart_title);
cart_price = itemView.findViewById(R.id.cart_price);
//cart_delete = itemView.findViewById(R.id.cart_delete);
cart_img = itemView.findViewById(R.id.cart_image);
}
}
public void addItem(List<CartItems> cartItems) {
CartItems newValue = new CartItems();
newValue.setTitle("123");
newValue.setPrice("123");
newValue.setImageUrl("");
cartItems.add(newValue);
notifyDataSetChanged();
}
}
My goal is adding recyclerview items dynamically to ShoppingCartActivity when a button clicked in ProductDetailActivity but app crashes.