这是我的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
}
}
这是我FeedAdapter
的{{1}},它是从资源布局文件RecyclerView
中提取的。
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);
}
}
}
,适配器从中获取数据。我想在视图content.xml
或onClickListner
上设置id:likelayout"
,以便在单击该视图时执行一些操作。现在,干一杯就足够了。
请帮助我完成操作。
like
答案 0 :(得分:1)
在您的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.
}
});
}
如果您想为列表项中的其他UI元素设置点击侦听器,则只需像声明username
,timea
,statusa
等的方式声明它们即可。然后将侦听器添加到它。
请记住,为了使ImageView
可点击,您需要在布局文件中将clickable
属性添加到true
中。例如,如果您需要使自己的图片可点击,则需要添加clickable
属性,如下所示。
<ImageView
android:id="@+id/like_image"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/like"/>
为了在单击项目时显示Toast
,在创建适配器时,您需要具有Context
活动的Discussion
。因此,我建议您像下面那样修改适配器的构造函数。
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);
}
}
}
要采用修改后的构造函数的更改,需要像从Discussion
类中进行以下初始化那样来对适配器进行初始化。
mRecycleview.setAdapter(new FeedAdapter(use, lo, ti, staus, posimg, this));
希望有帮助!
答案 1 :(得分:0)
与其他视图相同,在FeedViewHolder
类中添加一个LinearLayout
变量:
LinearLayout like;
然后
like = itemView.findViewById(R.id.likelayout);
然后在onBindViewHolder
中使用:
holder.like.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// Like Layout was clicked
}
});