java 在android中recyclerview的特定项目上的onclicklistener
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36759744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onclicklistener on the specific item of the recyclerview in android
提问by Sarvesh
I am going to ask very basic question, but i am stuck in it for a long time.
我要问一个非常基本的问题,但我被困了很长时间。
after card view there is an recycleview which has 2 images in each row. now i want to create the click listener on the images rather than the recycleview.
在卡片视图之后有一个回收视图,每行有 2 个图像。现在我想在图像上创建点击侦听器而不是 recycleview。
the corresponding layout(layout_main.xml) of this activity(MainActivity.java) contain only recyclerview. the elements of each row is in another layout(layout_images.xml). i am getting the images from layout_images.xml and inflate them in the adapter class(Adapter.java).
此活动(MainActivity.java) 的相应布局(layout_main.xml) 仅包含recyclerview。每行的元素在另一个布局中(layout_images.xml)。我从layout_images.xml 获取图像并在适配器类(Adapter.java) 中将它们膨胀。
now how to put action listener on the images only.
现在如何仅在图像上放置动作侦听器。
secondly, i want to get the image on which i clicked. how to get that. like, when we click on a view we create some method as
其次,我想获得我点击的图像。如何得到那个。就像,当我们点击一个视图时,我们创建了一些方法
public void onClick(View view){
// some code here
}
where view is the object on which we clicked. in my case how to get the image on which i clicked. using type cast it might be throw an exception when user doesnot click on image.
其中 view 是我们点击的对象。就我而言,如何获取我单击的图像。使用类型转换可能会在用户不单击图像时引发异常。
回答by Rohitashv jain
Multiple onClick events inside a recyclerView:
recyclerView 中的多个 onClick 事件:
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public ImageView iconImageView;
public TextView iconTextView;
public MyViewHolder(final View itemView) {
super(itemView);
iconImageView = (ImageView) itemView.findViewById(R.id.myRecyclerImageView);
iconTextView = (TextView) itemView.findViewById(R.id.myRecyclerTextView);
// set click event
itemView.setOnClickListener(this);
iconTextView.setOnClickListener(this);
// set long click event
iconImageView.setOnLongClickListener(this);
}
// onClick Listener for view
@Override
public void onClick(View v) {
if (v.getId() == iconTextView.getId()) {
Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}
//onLongClickListener for view
@Override
public boolean onLongClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Hello Dialog")
.setMessage("LONG CLICK DIALOG WINDOW FOR ICON " + String.valueOf(getAdapterPosition()))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
return true;
}
}
To get which item was clicked you match the view id i.e. v.getId() == yourViewItem.getId()
要获取点击了哪个项目,您需要匹配视图 ID ievgetId() == yourViewItem.getId()
回答by inkedTechie
You have to set onClickListener
to the ImageView
s inside the onBindViewHolder
method, refer the following LOCs for reference(code to be inside onBindViewHolder
method)
你必须设置onClickListener
为方法ImageView
内部的s onBindViewHolder
,参考以下LOCs以供参考(代码在onBindViewHolder
方法内部)
holder.imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//put your code for first imageview here
}
});
holder.imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//put your code for second imageView here
}
});
回答by Madhukar Hebbar
In Recycle View Holder, Write your onclick listener code inside
在Recycle View Holder 中,在里面编写您的 onclick 侦听器代码
@Override
public void onBindViewHolder(CardHolder holder, final int position) {
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO
}
}
}
回答by Bhushan
implement the View.OnClickListener
in your ViewHolder
class and implement the onClick
method. Then set the click listener for your ImageView to this click listener. Add the required functionality in the onClick method. If you want to implement the click functionality in other class simply create an interface and declare a click method in it. You can implement this method in the activity/fragment that contains this RecycleView. Then from your view holders onClick method you can invoke the interface method.
实现View.OnClickListener
你的ViewHolder
类,并实现onClick
方法。然后将您的 ImageView 的点击侦听器设置为这个点击侦听器。在 onClick 方法中添加所需的功能。如果你想在其他类中实现点击功能,只需创建一个接口并在其中声明一个点击方法。您可以在包含此 RecycleView 的活动/片段中实现此方法。然后从您的视图持有者 onClick 方法中,您可以调用接口方法。
回答by nilkash
You can check with tag or it of element like this:
您可以像这样检查元素的标签或它:
public void onClick(View view){
if(view.getId() == image1.getId())
{
}else if(view.getId() == image2.getId())
{}
}
回答by Yarh
First of all set onclickListener to each view you want to be clicked. Good place to do it in viewHolderConstructor. eg
首先将 onclickListener 设置为要单击的每个视图。在 viewHolderConstructor 中这样做的好地方。例如
public class GalleryManyViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.im_img) RoundedCornersImageView imImg;
@BindView(R.id.tv_title) MyTextView tvTitle;
@BindView(R.id.tv_label) MyTextView tvLabel;
@BindView(R.id.tv_date) MyTextView tvDate;
@BindView(R.id.im_gallery_one) RoundedCornersImageView imGalleryOne;
@BindView(R.id.im_gallery_two) RoundedCornersImageView imGalleryTwo;
@BindView(R.id.im_gallery_three) RoundedCornersImageView imGalleryThree;
@BindView(R.id.im_gallery_four) RoundedCornersImageView imGalleryFour;
@BindView(R.id.tv_more) MyTextView tvMore;
@BindView(R.id.root) RelativeLayout root;
public GalleryManyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
view.setOnClickListener(onClickListener);
imGalleryOne.setOnClickListener(onClickListener);
imGalleryTwo.setOnClickListener(onClickListener);
imGalleryThree.setOnClickListener(onClickListener);
imGalleryFour.setOnClickListener(onClickListener);
view.setTag(this);
}
Generally you do not need to make anything specific with those view, like setting tags (Also some usefull libraries like Glied, which sets its own tags to imageviews will malfunction if you set you own tag. In on clickListener find adapter position of the view to be able to retrive the corresponding data
通常你不需要对这些视图做任何特定的事情,比如设置标签(还有一些有用的库,比如 Glied,如果你设置自己的标签,它会将自己的标签设置为 imageviews 将发生故障。在 clickListener 中找到视图的适配器位置能够检索到相应的数据
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override public void onClick(View v) {
View view = v;
View parent = (View) v.getParent();
while (!(parent instanceof RecyclerView)){
view=parent;
parent = (View) parent.getParent();
}
int position = recyclerView.getChildAdapterPosition(view);
}
as described hereThen but checking views id, evaluete what you want to do
作为描述这里然后但检查的意见ID,evaluete你想要做什么
switch (v.getId()) {
case R.id.im_gallery_one: {
p = 0;
}
break;
case R.id.im_gallery_two: {
p = 1;
}
break;
case R.id.im_gallery_three: {
p = 2;
}
break;
case R.id.im_gallery_four: {
p = 3;
}
break;
}