我正在使用Recyclerview作为菜单类别,我想处理项目点击侦听器以启动其他活动,请将额外的任何一个关于此答案的经验告诉我的问题。如何处理Itemclick listener recyclerview
波纹管是我的代码java。如果可能,我想在ProductCategory.class中放置点击监听器,因为我想多次使用此模块(ProductServiceCategoryContractor,ProductServiceCategoryAdapter)。
ProductServiceCategoryContractor.java(属性)
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;
}
}
为了处理单击recyclerview,使用** ** RecyclerItemClickListener并使用** recyclerview1.addOnItemTouchListener(lisenter)**。 –