我正在使用Recyclerview作为菜单类别,我想处理项目点击监听器以开始额外的活动开始其他活动,请任何人有关于此答案的经验我的问题。
下面是我的代码java。如果可以,我想在ProductCategory.class中放置click监听器,因为我想多次使用这个模块(ProductServiceCategoryContractor,ProductServiceCategoryAdapter)。
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;
}
}
答案 0 :(得分:2)
使用View.OnClickListener实现ViewHolder,重写OnClick方法并创建一个界面,现在您可以在创建ViewHolder时使用onclick方法。 这是一个简单的例子...
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) {
//..........
}
答案 1 :(得分:0)
有许多方法可以在OnClickListener
适配器中实现RecyclerView
。
您可以在onBindViewHolder()
方法中实现点击监听器,如下所示
holder.buttoncalling.setOnClickListener(new View.OnClickListener(){
// perform click operation
});
你也可以在ItemHolder
内部实现它
buttoncalling.setOnClickListener( new View.OnClickListener(){
// perform click operation
});
并在IIS Web App Deployment Using WinRM extension
中按照建议点击位置时使用getAdapterPosition()
答案 2 :(得分:0)
将onClickListener放在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);
}
});
}