我关注了帖子here。我基本上试图在RecyclerView
中设置一个点击监听器。当我点击RecyclerView
中的某个项目时,我没有得到任何回复。我已经记录了它,当我点击时我没有收到对日志的调用。因此,我知道.onClick()
方法没有触发。我在.onStart()
中初始化适配器,如下所示:
@Override
public void onStart() {
super.onStart();
mFireAdapter = new FirebaseRecyclerAdapter<Poll, PollHolder>(Poll.class, R.layout.latest_item, PollHolder.class, mBaseRef.child("Polls")) {
@Override
protected void populateViewHolder(PollHolder viewHolder, Poll model, final int position) {
viewHolder.mPollQuestion.setText(model.getQuestion());
Picasso.with(getActivity().getApplicationContext())
.load(model.getImage_URL())
.fit()
.into(viewHolder.mPollImage);
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.v("TAG","ONCLICKED");
Intent toPoll = new Intent(getActivity(), PollHostActivity.class);
toPoll.putExtra("POLL_ID", mFireAdapter.getRef(position).getKey());
startActivity(toPoll);
}
});
Log.v("TAG", mFireAdapter.getRef(position).getKey());
Log.v("TAG", String.valueOf(position));
Log.v("TAG", model.getQuestion());
Log.v("TAG", model.getImage_URL());
}
};
mRecyclerview.setAdapter(mFireAdapter);
mFireAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int pollCount = mFireAdapter.getItemCount();
int lastVisiblePosition = mLayoutManager.findLastCompletelyVisibleItemPosition();
// If the recycler view is initially being loaded or the user is at the bottom of the list, scroll
// to the bottom of the list to show the newly added message.
if (lastVisiblePosition == -1 ||
(positionStart >= (pollCount - 1) && lastVisiblePosition == (positionStart - 1))) {
mRecyclerview.scrollToPosition(positionStart);
}
}
});
}
这是我创建ViewHolder()
:
public static class PollHolder extends RecyclerView.ViewHolder {
TextView mPollQuestion;
ImageView mPollImage;
View mView;
public PollHolder(View itemView) {
super(itemView);
mPollQuestion = (TextView) itemView.findViewById(R.id.latest_item_question);
mPollImage = (ImageView) itemView.findViewById(R.id.pollThumbNailImage);
this.mView = itemView;
}
}
答案 0 :(得分:1)
在recyclerView中,您可以在recyclerview适配器中设置clicklistener 这对我有用
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView imageView;
public MyViewHolder(View view) {
super(view);
view.setOnClickListener(this); //clicklistner on recyclerview Item
imageView = (ImageView) view.findViewById(R.id.grid_item_image);
}
@Override
public void onClick(View v) {
//Log.d(TAG, "onClick " + getPosition());
Toast.makeText(activity, "onClick "+ getPosition(), Toast.LENGTH_SHORT).show();
}
}
答案 1 :(得分:0)
原来这一行是搞砸了一切,阻止了RecyclerView
次观看次数:
android:clickable="true"