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