我知道这个问题已被问过100次,但是我找不到与我的问题相关的任何答案,因为我是新手,而且他们的代码完全不同。我正在显示从Firebase
到RecyclerView
的图像,这些图像单击时必须像弹出窗口一样或在Activity
中打开图像。
quote.java
public class quote extends AppCompatActivity {
RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Post List");
int noc =2;
mRecyclerView=findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager(this,noc));
mFirebaseDatabase=FirebaseDatabase.getInstance();
mRef=mFirebaseDatabase.getReference("Data");
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Model,ViewHolder> firebaseRecyclerAdapter=
new FirebaseRecyclerAdapter<Model, ViewHolder>(Model.class,R.layout.row,ViewHolder.class,mRef) {
@Override
protected void populateViewHolder(ViewHolder viewHolder, Model model, int position) {
viewHolder.setDetails(model.getTitle(), model.getImage());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(quote.this,"hello",Toast.LENGTH_LONG).show();
}
});
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
}
Model.java
public class Model {
String title,image;
public Model(){}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
ViewHolder.java
public class ViewHolder extends RecyclerView.ViewHolder {
View mView;
public ViewHolder( View itemView) {
super(itemView);
mView = itemView;
}
public void setDetails(String title, String image){
TextView mTitleTv = mView.findViewById(R.id.rTitleTv);
ImageView mImageTv = mView.findViewById(R.id.rImageView);
mTitleTv.setText(title);
Picasso.get().load(image).into(mImageTv);
}
}
答案 0 :(得分:1)
您需要使用itemView。
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(quote.this,"hello",Toast.LENGTH_LONG).show();
}
});
答案 1 :(得分:1)
ViewHolder.ImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemListener.recyclerViewListClicked(view, position);
}
});
在设置/调用Adapter和OnBindviewHolder时将接口上下文或回调从片段或活动发送到Adapter构造函数,并将数据设置为接口功能,并在Activity或Fragment中实现这些功能以在单击时访问数据
答案 2 :(得分:0)
如果您想放大图像,请向下滑动/向上滑动,可以使用支持库,例如 Facebook 的 Fresco ,这是实施指南: https://frescolib.org/docs/index.html,或者您可以使用某些 Fresco 包装器,例如,https://github.com/stfalcon-studio/FrescoImageViewer
因此,通过使用 Fresco ,您可以这样做,图像将以全屏或自定义尺寸显示:
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("image url here");
draweeView.setImageURI(uri);
}
});
答案 3 :(得分:0)