gpt4 book ai didi

android - 在 Android 中使用 Firebase 在 RecyclerView 上获取 onClickListener

转载 作者:行者123 更新时间:2023-11-30 00:18:43 27 4
gpt4 key购买 nike

我已经在我的 Activity 中使用 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;
}

最佳答案

将 onClickListener 移到 onBindViewHolder 中。

@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();
}

});
}

编辑:

删除接口(interface),因为它是多余的。您正在将值从 viewholder 传递到 onCreateViewHolder,这是不必要的。而是处理 viewholder 内部的点击。检查更新的类。

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;
}
}

关于android - 在 Android 中使用 Firebase 在 RecyclerView 上获取 onClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46702860/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com