本文介绍了如何在javatechig示例中添加Recycler项目SetOnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为此RecyclerView
添加 onclicklistener ,但是我尝试了所有操作,但不起作用!
I'm trying to add onclicklistener for this RecyclerView
but i tryied everything and it doesnt work!:
FeedItem:
public class FeedItem {
private String title;
private String thumbnail;
private String date;
private String tags;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
public String getDate() {
return date;
}
public void getDate(String date) {
this.date = date;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
}
FeedListRowHolder:
public class FeedListRowHolder extends RecyclerView.ViewHolder {
protected ImageView thumbnail;
protected TextView title;
protected TextView date;
protected TextView tags;
public FeedListRowHolder(View view) {
super(view);
this.thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
this.title = (TextView) view.findViewById(R.id.title);
this.date = (TextView) view.findViewById(R.id.date);
this.tags = (TextView) view.findViewById(R.id.tagsItems);
}
}
适配器:
public class MyRecyclerAdapter extends RecyclerView.Adapter<FeedListRowHolder> {
private List<FeedItem> feedItemList;
private Context mContext;
public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
@Override
public FeedListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
return new FeedListRowHolder(v);
}
@Override
public void onBindViewHolder(FeedListRowHolder feedListRowHolder, int i) {
FeedItem feedItem = feedItemList.get(i);
Picasso.with(mContext).load(feedItem.getThumbnail())
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(feedListRowHolder.thumbnail);
feedListRowHolder.title.setText(Html.fromHtml(feedItem.getTitle()));
feedListRowHolder.date.setText(Html.fromHtml(feedItem.getDate()));
feedListRowHolder.tags.setText(Html.fromHtml(feedItem.getTags()));
}
@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
}
如何将OnClickListener
添加到此RecyclerView
?
谢谢
推荐答案
recyclerview.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
FeedItem bookScreenDateEntity = (FeedItem)yourList.get(position);
bookScreenDateEntity.getServiceid();
}
})
);
RecyclerItemClickListener类:
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
这篇关于如何在javatechig示例中添加Recycler项目SetOnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!