[英]How can i Add the click listeners to the child of Recycler View?
Here is my discussion.java
which contains the RecyclerView
这里是我的
discussion.java
其中包含RecyclerView
public class Discussion extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discussion);
RecyclerView mRecycleview =(RecyclerView)findViewById(R.id.cycleview);
mRecycleview.setLayoutManager(new LinearLayoutManager(this));
String[] use ={"a","b"};
String[] lo={"be","ndls"};
String[] ti={"12:00","11:00"};
String[] staus={"Today","Tommmorow"};
int[] posimg={R.drawable.img_post1,R.drawable.img_post2};
mRecycleview.setAdapter(new FeedAdapter(use,lo,ti,staus,posimg));
@NonNull
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Open Activity for posting", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// Functionality for poll
}
}
Here is my FeedAdapter
for the RecyclerView
which is taking from resource layout file content.xml
.这是我用于
RecyclerView
FeedAdapter
,它取自资源布局文件content.xml
。
public class FeedAdapter extends
RecyclerView.Adapter<FeedAdapter.FeedViewHolder> {
private String[] userdata;
private String[] locdata;
private String[] timedata;
private int[] postdata;
private String[] statusdata;
public FeedAdapter(String[] userdata,String[] locdata,String[] timedata,String[] statusdata,int[] postdata){
this.userdata=userdata;
this.locdata=locdata;
this.postdata=postdata;
this.statusdata=statusdata;
this.timedata=timedata;
}
@Override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(parent.getContext());
View view =inflater.inflate(R.layout.backupofcontent,parent,false);
return new FeedViewHolder(view);
}
@Override
public void onBindViewHolder( FeedAdapter.FeedViewHolder holder, int position) {
String user=userdata[position];
String loc=locdata[position];
String time=timedata[position];
String status=statusdata[position];
int post=postdata[position];
holder.username.setText(user);
holder.locationa.setText(loc);
holder.timea.setText(time);
holder.statusa.setText(status);
if(post==0)
holder.postimg.setVisibility(View.GONE);
else
holder.postimg.setImageResource(post);
}
@Override
public int getItemCount() {
return userdata.length;
}
public class FeedViewHolder extends RecyclerView.ViewHolder{
ImageView postimg;
TextView username;
TextView locationa;
TextView timea;
TextView statusa;
public FeedViewHolder(View itemView) {
super(itemView);
postimg=itemView.findViewById(R.id.imgView_postPic);
username=itemView.findViewById(R.id.user_name);
locationa=itemView.findViewById(R.id.Location);
timea=itemView.findViewById(R.id.Time);
statusa=itemView.findViewById(R.id.post_text);
}
}
}
Here is my content.xml
from which adapter is taking the feed.这是我的
content.xml
,适配器从中获取提要。 I want to set up the onClickListner
on the view id:likelayout"
or like
so that when it is clicked, some actions are to be performed. For now, a toast would be enough.我想成立
onClickListner
的观点id:likelayout"
或like
。使被点击时,一些动作将被执行现在,敬酒就足够了。
Please help me out how to do it.请帮我看看怎么做。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:orientation="vertical"
tools:showIn="@layout/app_bar_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_vertical"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:text="User"
android:textSize="20sp" />
<TextView
android:id="@+id/Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/user_name"
android:layout_gravity="center|center_vertical"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:text="Time"
android:textSize="10sp" />
<TextView
android:id="@+id/Location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Time"
android:layout_gravity="center|center_vertical"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:text="Location"
android:textSize="10sp" />
<LinearLayout
android:id="@+id/layout_post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Location"
android:layout_marginTop="10dp"
android:orientation="vertical">
<ImageView
android:id="@+id/imgView_postPic"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@drawable/img_post1" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="55dp"
android:layout_weight="1">
<TextView
android:id="@+id/post_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Status " />
</ScrollView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginBottom="10dp"
android:gravity="center_vertical"
android:layout_above="@id/bottomnav">
<TextView
android:id="@+id/likecount"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="0"
android:gravity="center" />
<TextView
android:id="@+id/likecounttext"
android:layout_toRightOf="@id/likecount"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Liked"/>
<TextView
android:layout_toRightOf="@id/likecounttext"
android:layout_marginLeft="15dp"
android:id="@+id/countsepartor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"/>
<TextView
android:id="@+id/commentcount"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@id/countsepartor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"/>
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/commentcount"
android:text="Comments"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/bottomnav"
android:layout_width="match_parent"
android:layout_height="30dp"
android:weightSum="3"
android:layout_marginBottom="5dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/likelayout"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="25dp"
android:layout_height="wrap_content"
android:src="@drawable/like"/>
<TextView
android:id="@+id/like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Like"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/commentlayout"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="25dp"
android:layout_height="wrap_content"
android:src="@drawable/comment"/>
<TextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comment"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/polllayout"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="25dp"
android:layout_height="wrap_content"
android:src="@drawable/poll"/>
<TextView
android:id="@+id/poll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Poll"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:textSize="15sp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
In your onBindViewHolder
function, just add a click listener like the following.在您的
onBindViewHolder
函数中,只需添加一个单击侦听器,如下所示。
@Override
public void onBindViewHolder( FeedAdapter.FeedViewHolder holder, int position) {
String user=userdata[position];
String loc=locdata[position];
String time=timedata[position];
String status=statusdata[position];
int post=postdata[position];
holder.username.setText(user);
holder.locationa.setText(loc);
holder.timea.setText(time);
holder.statusa.setText(status);
if(post==0)
holder.postimg.setVisibility(View.GONE);
else
holder.postimg.setImageResource(post);
holder.username.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// Do something.
}
});
}
If you want to set up the click listener to the other UI elements in your list item, then just declare them like the way you have declared username
, timea
, statusa
etc. and then add the listener to it.如果您想为列表项中的其他 UI 元素设置点击侦听器,只需像您声明
username
、 timea
、 statusa
等的方式声明它们,然后向其中添加侦听器。
Please keep in mind that, in order to make an ImageView
clickable you need to add the clickable
attribute to true
in your layout file.请记住,为了使
ImageView
可点击,您需要在布局文件中将clickable
属性添加为true
。 For example, if you need to make your like image clickable, you need to add the clickable
attribute like the following.例如,如果您需要使喜欢的图像可点击,则需要添加如下所示的
clickable
属性。
<ImageView
android:id="@+id/like_image"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/like"/>
In order to show a Toast
on clicking the item, you need to have the Context
of the Discussion
activity while you are creating the adapter.为了在单击项目时显示
Toast
,您需要在创建适配器时拥有Discussion
活动的Context
。 So I would like to suggest you modify the constructor of the adapter like the following.所以我建议你像下面这样修改适配器的构造函数。
public class FeedAdapter extends
RecyclerView.Adapter<FeedAdapter.FeedViewHolder> {
private String[] userdata;
private String[] locdata;
private String[] timedata;
private int[] postdata;
private String[] statusdata;
// Add another extra variable to store context
// The context is necessary for showing toast
private Context context;
public FeedAdapter(String[] userdata, String[] locdata,String[] timedata, String[] statusdata, int[] postdata, Context context){
this.userdata = userdata;
this.locdata = locdata;
this.postdata = postdata;
this.statusdata = statusdata;
this.timedata = timedata;
this.context = context; // Initialize it here
}
@Override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(parent.getContext());
View view =inflater.inflate(R.layout.backupofcontent,parent,false);
return new FeedViewHolder(view);
}
@Override
public void onBindViewHolder( FeedAdapter.FeedViewHolder holder, int position) {
String user=userdata[position];
String loc=locdata[position];
String time=timedata[position];
String status=statusdata[position];
int post=postdata[position];
holder.username.setText(user);
holder.locationa.setText(loc);
holder.timea.setText(time);
holder.statusa.setText(status);
if(post==0)
holder.postimg.setVisibility(View.GONE);
else
holder.postimg.setImageResource(post);
// Use the context here to show the Toast
holder.username.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Something", Toast.LENGTH_LOGN).show();
}
});
}
@Override
public int getItemCount() {
return userdata.length;
}
public class FeedViewHolder extends RecyclerView.ViewHolder{
ImageView postimg;
TextView username;
TextView locationa;
TextView timea;
TextView statusa;
public FeedViewHolder(View itemView) {
super(itemView);
postimg=itemView.findViewById(R.id.imgView_postPic);
username=itemView.findViewById(R.id.user_name);
locationa=itemView.findViewById(R.id.Location);
timea=itemView.findViewById(R.id.Time);
statusa=itemView.findViewById(R.id.post_text);
}
}
}
And to adopt the changes of the modified constructor, the adapter needs to be initialized like the following from your Discussion
class.为了采用修改后的构造函数的更改,需要从您的
Discussion
类中初始化适配器,如下所示。
mRecycleview.setAdapter(new FeedAdapter(use, lo, ti, staus, posimg, this));
Hope that helps!希望有帮助!
same as the other views, in the FeedViewHolder
class add a LinearLayout
variable:与其他视图一样,在
FeedViewHolder
类中添加一个LinearLayout
变量:
LinearLayout like;
then然后
like = itemView.findViewById(R.id.likelayout);
then in onBindViewHolder
use :然后在
onBindViewHolder
使用:
holder.like.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// Like Layout was clicked
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.