我已在我的活动中使用 Firebase RecyclerAdapter实施 RecyclerView ,并希望实现 onClickListener ,尽管我已尝试实现此目的但我想我的代码中有些东西是不对的,请帮我找出错误。我已经搜索了另一个问题,但没有得到理解,也无法找到解决问题的方法。
我正在使用Android Studio
firebase适配器类文件:
public class HomePage extends AppCompatActivity {
@Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
sentToStart();
}
FirebaseRecyclerAdapter<post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(
post.class,
R.layout.post_row_recycle_home,
postViewHolder.class,
mDatabaseReference
) {
@Override
protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
viewHolder.setwebViewPost(model.getWebViewPost());
}
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class postViewHolder extends RecyclerViewPager.ViewHolder {
View mView;
public postViewHolder(View itemView) {
super(itemView);
mView = itemView;
mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mClickListener.onItemClick(v, getAdapterPosition());
}
});
}
private postViewHolder.ClickListener mClickListener;
public interface ClickListener {
public void onItemClick(View view, int position);
}
public void setOnClickListener(postViewHolder.ClickListener clickListener) {
mClickListener = clickListener;
}
public void setwebViewPost(String webViewPost) {
WebView webView = (WebView) mView.findViewById(R.id.webView_news);
webView.loadUrl(webViewPost);
}
}
@Override
public postViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
postViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
viewHolder.setOnClickListener(new postViewHolder().ClickListener() {
@Override
public void onItemClick (View view,int position){
Toast.makeText(this, "Item clicked at " + position, Toast.LENGTH_SHORT).show();
}
});
return viewHolder;
}
答案 0 :(得分:1)
在onBindViewHolder中移动onClickListener。
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new postViewHolder().ClickListener() {
@Override
public void onItemClick (View view,int position){
Toast.makeText(this, "Item clicked at " + position, Toast.LENGTH_SHORT).show();
}
});
}
修改强>
删除界面,因为它是多余的。您将值从viewholder传递给onCreateViewHolder,这是不必要的。而是处理视图内部的单击。检查更新的课程。
public class HomePage extends AppCompatActivity {
@Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
sentToStart();
}
FirebaseRecyclerAdapter<post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(post.class, R.layout.post_row_recycle_home, postViewHolder.class, mDatabaseReference) {
@Override
protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
viewHolder.setwebViewPost(model.getWebViewPost());
}
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class postViewHolder extends RecyclerViewPager.ViewHolder {
View mView;
public postViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Show the webViewPost Here
}
});
}
public void setwebViewPost(String webViewPost) {
WebView webView = (WebView) mView.findViewById(R.id.webView_news);
webView.loadUrl(webViewPost);
}
}
@Override
public postViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
postViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
return viewHolder;
}
}
答案 1 :(得分:0)
请参阅我的方法,该方法取自Google示例+ GoogleIO youtube会话:
使用:
onCreateViewHolder(ViewGroup parent,int viewType)
newsAdapter = new FirebaseRecyclerAdapter<ModelNews, NewsItemHolder>(
ModelNews.class,
R.layout.item_news,
NewsItemHolder.class,
FirebaseDatabase.getInstance()
.getReference(FIREBASE_NEWS)
.orderByChild(FIREBASE_ORDER_BY_FIELD)
.limitToFirst(20)) {
@Override
public NewsItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
final NewsItemHolder newsItemHolder = new NewsItemHolder(view);
newsItemHolder.holder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = newsItemHolder.getAdapterPosition();
if (pos != RecyclerView.NO_POSITION) {
ModelNews clicked = newsAdapter.getItem(pos);
if (clicked.getIsExternal() && !hasConnection) {
showSnackbar(R.string.news_with_wifi);
return;
}
goToDetails(clicked.getID(), clicked.getIsExternal());
}
}
});
return newsItemHolder;
}
@Override
public void populateViewHolder(
final NewsItemHolder view,
ModelNews data,
final int position) {
// Set news title
view.titleView.setText(data.getTitle());
// hide OR show wifi icon
view.needWifiIcon(data.getIsExternal());
// Set news image
mImageLoader.loadImage(data.getThumbUrl(), view.thumbView, true);
// Set news description
view.descView.setText(data.getContent());
}
@Override
public void onDataChanged() {
if (newsProgessBar.getVisibility() != View.GONE) {
newsProgessBar.setVisibility(GONE);
}
}
};