4 回答
TA贡献2019条经验 获得超9个赞
您必须在 ExampleAdapter 的 onBindViewHolder 方法内执行此操作。
作为一个例子,你可以这样做:
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
holder.mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// your code here
}
});
}
在 onBindViewHolder() 方法内部“逻辑发生”。在那里您可以为每个 RecyclerView 项目设置所有内容。
TA贡献1828条经验 获得超4个赞
此代码在单击的每个项目上显示一个对话框
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.recycler_item, null);
final ItemViewHolder viewHolder = new ItemViewHolder(view);
dialoge = new Dialog(parent.getContext());
dialoge.setContentView(R.layout.dialog);
dialoge.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
viewHolder.container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setDialog(dialoge, viewHolder);
dialoge.show();
}
});
return viewHolder;
}
在方法 setDialog() 中,您可以使用以下方法获取当前项目位置
final RecyclerItem currentItem = itemList.get(viewHolder.getAdapterPosition());
但我认为最好的做法是考虑使用接口,可以遵循tutorialRecyclerItemOnClickListenerInterface
我希望这可以帮助你
编辑:从 onClickListnere 启动活动:
Intent myIntent = new Intent(parent.getContext(),yourActivityName.class); myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(myIntent);
TA贡献1826条经验 获得超6个赞
您需要创建一个与普通点击侦听器类似的界面,但它也有位置作为参数。
public interface RecyclerViewClickListener { void onClick(View view, int position); }
然后你可以在你的适配器中声明
private final RecyclerViewClickListener listener;
您可以在适配器中创建一个设置器
public void setListner(RecyclerViewClickListener listener){ this.listener=listener; }
TA贡献1864条经验 获得超2个赞
您必须将回调接口添加到适配器中,并将该接口的实例从活动传递到适配器构造函数。当您单击 recyclerview 项的不同 ViewGroup 时调用接口的方法,并且 Activity 中应该有一个实现。你的代码看起来像这样
活动
public class ProductPage1 extends AppCompatActivity implements ClickCallback {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_page1);
ArrayList<ExampleItem> exampleList = new ArrayList<>();
exampleList.add(new ExampleItem(R.drawable.mickeymouse, "Line1" , "Line2"));
exampleList.add(new ExampleItem(R.drawable.donalduck, "Line3" , "Line4"));
exampleList.add(new ExampleItem(R.drawable.popey, "Line5" , "Line6"));
exampleList.add(new ExampleItem(R.drawable.mickeymouse, "Line1" , "Line2"));
exampleList.add(new ExampleItem(R.drawable.donalduck, "Line3" , "Line4"));
exampleList.add(new ExampleItem(R.drawable.popey, "Line5" , "Line6"));
exampleList.add(new ExampleItem(R.drawable.mickeymouse, "Line1" , "Line2"));
exampleList.add(new ExampleItem(R.drawable.donalduck, "Line3" , "Line4"));
exampleList.add(new ExampleItem(R.drawable.popey, "Line5" , "Line6"));
exampleList.add(new ExampleItem(R.drawable.mickeymouse, "Line1" , "Line2"));
exampleList.add(new ExampleItem(R.drawable.donalduck, "Line3" , "Line4"));
exampleList.add(new ExampleItem(R.drawable.popey, "Line5" , "Line6"));
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
@Override
onItemClick(int position, View view){
switch(view.getId()){
case R.id.mImageView:
//do your view click events here
break;
case R.id.mImageView2:
//do your view click events here
break;
//so on
}
}
适配器
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder
{
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public Button mButton;
public ExampleViewHolder(@NonNull View itemView,ClickCallback clickCallback) {
super(itemView);
this.clickCallback=clickCallback;
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView2);
mButton = itemView.findViewById(R.id.mybutton);
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList)
{
mExampleList = exampleList;
}
@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
holder.mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickCallback.onItemClick(position, holder.mImageView);
//and same for other views
}
}
@Override
public int getItemCount() {
return mExampleList.size();
}
public interface ClickCallback{
void onItemClick(int position, View view);
//here you can send the object of the list at the position if you require
//that in activity
}
}
希望你得到答案。快乐编码:)
添加回答
举报