Reputation: 115
I am using Recyclerview for menu category and I want to handle item click listener to start other activity with put extra, please any one have experience about this answer my question.
the bellow are my code java. if posible I want to put click listener in ProductCategory.class because I want to use this module(ProductServiceCategoryContractor, ProductServiceCategoryAdapter) many time.
ProductServiceCategoryContractor.java (properties)
public class ProductServiceCategoryContractor {
private int img;
public ProductServiceCategoryContractor(String title) {
this.title = title;
}
private String title;
public ProductServiceCategoryContractor(int img, String title) {
this.img = img;
this.title = title;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
ProductServiceCategoryAdapter.java
public class ProductServiceCategoryAdapter extends RecyclerView.Adapter<ProductServiceCategoryAdapter.MyViewHolder> {
private Context mContext;
private List<ProductServiceCategoryContractor> albumList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public LinearLayout layout;
public ImageView img;
public TextView title;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
img = (ImageView) view.findViewById(R.id.img);
layout=(LinearLayout) view.findViewById(R.id.layout);
}
}
public ProductServiceCategoryAdapter(Context mContext, List<ProductServiceCategoryContractor> albumList) {
this.mContext = mContext;
this.albumList = albumList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.product_menu_model, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
ProductServiceCategoryContractor album = albumList.get(position);
holder.title.setText(album.getTitle());
Glide.with(mContext).load(album.getImg()).into(holder.img);
}
@Override
public int getItemCount() {
return albumList.size();
}
}
ProductCategory.java
public class ProductCategory extends AppCompatActivity {
private RecyclerView recyclerView;
private ProductServiceCategoryAdapter adapter;
private List<ProductServiceCategoryContractor> albumList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_category);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
recyclerView = (RecyclerView) findViewById(R.id.product_category);
albumList = new ArrayList<>();
adapter = new ProductServiceCategoryAdapter(this, albumList);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 1);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
prepareAlbums();
}
private void prepareAlbums() {
int[] covers = new int[]{
R.mipmap.ic_launcher
};
ProductServiceCategoryContractor pl_list = new ProductServiceCategoryContractor(covers[0], "Phone Spare parts & Accessory");
albumList.add(pl_list);
pl_list = new ProductServiceCategoryContractor(covers[0], "Computer Networking");
albumList.add(pl_list);
pl_list = new ProductServiceCategoryContractor("Clothing Accessory");
albumList.add(pl_list);
adapter.notifyDataSetChanged();
}
@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
}
Upvotes: 0
Views: 1180
Reputation: 26
Put your onClickListener in onBindViewHolder..
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
ProductServiceCategoryContractor album = albumList.get(position);
holder.title.setText(album.getTitle());
Glide.with(mContext).load(album.getImg()).into(holder.img);
holder.img.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, OtherActivity.class);
intent.putExtra("extra", "message");
mContext.startActivity(intent);
}
});
}
Upvotes: 0
Reputation: 8834
There are many ways to implement OnClickListener
inside RecyclerView
adapter.
You can implement click listener inside onBindViewHolder()
method like below
holder.buttoncalling.setOnClickListener(new View.OnClickListener(){
// perform click operation
});
You can also implement it inside you ItemHolder
like this
buttoncalling.setOnClickListener( new View.OnClickListener(){
// perform click operation
});
and use getAdapterPosition()
whenever you need item clicked position as recommended in official docs.
Upvotes: 0
Reputation: 4859
Implement ViewHolder with View.OnClickListener, override OnClick method and create an interface, now you can use onclick method while creating ViewHolder. Here is simple exmple ...
public class MyCustomAdapter extends RecyclerView.Adapter<MyCustomAdapter.ViewHolder> {
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public IMyViewHolderClicks mListener;
public ViewHolder(View v, IMyViewHolderClicks listener) {
super(v);
v.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mListener.myOnClick(v, getLayoutPosition());
}
public static interface IMyViewHolderClicks {
public void myOnClick(View caller, int position);
}
}
public MyCustomAdapter(Context context) {
this.context = context;
//..............
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_list_item, parent, false);
MyCustomAdapter.ViewHolder vh = new ViewHolder(v, new PostsAdapter.ViewHolder.IMyViewHolderClicks() {
@Override
public void myOnClick(View caller, int position) {
//Add your onclick event here
}
});
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
//..........
}
Upvotes: 2