我正在使用的应用程序当前包含显示多个图像的画廊视图。这些显示在recycler view
中。我最终想要实现的是让用户点击其中一张图像,并在单独的活动中将其作为image view
进行打开。我希望我能学到最好的方法。我在搜索中找不到它,请原谅我,我对编码还是很陌生。
画廊活动:
import android.app.ActionBar;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
public class GalleryActivity extends AppCompatActivity {
private final Integer image_ids[] = {
//all of my image ids from drawable//
};
@Override
public void onBackPressed() {
Intent returnhome = new Intent(GalleryActivity.this, BleachActivity.class);
startActivity(returnhome);
finish();
return;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("//name of app//");
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.gallery);
recyclerView.setHasFixedSize(false);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 3);
recyclerView.setLayoutManager(layoutManager);
ArrayList<Cell> cells = prepareData();
MyAdapter adapter = new MyAdapter(getApplicationContext(), cells);
recyclerView.setAdapter(adapter);
}
private ArrayList<Cell> prepareData() {
ArrayList<Cell> theImage = new ArrayList<>();
for(int i = 0; i < image_ids.length; i++){
Cell cell = new Cell();
cell.setImg(image_ids[i]);
theImage.add(cell);
}
return theImage;
}}
单元Java文件:
public class Cell {
private Integer img;
public Integer getImg() {
return img;
}
public void setImg(Integer img) {
this.img = img;
}
}
图像适配器Java文件:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<Cell> galleryList;
private Context context;
public MyAdapter (Context context, ArrayList<Cell> galleryList){
this.galleryList = galleryList;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, final int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell, viewGroup, false);
return new ViewHolder(view);}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, final int i){
viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
viewHolder.img.setImageResource(galleryList.get(i).getImg());
viewHolder.img.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
}
});
}
@Override
public int getItemCount(){
return galleryList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView img;
public ViewHolder(View view){
super(view);
img = (ImageView) view.findViewById(R.id.img);
}
}
}
答案 0 :(得分:0)
您首先创建一个新的活动,该活动将保存并显示图像视图,例如ImageViewActivity。 然后您有多种选择,我们将审查其中两个。
容易的选择不是很好(不明智的建筑选择)
从适配器开始活动
@Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, final int i){
viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
viewHolder.img.setImageResource(galleryList.get(i).getImg());
viewHolder.img.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Intent intent = new Intent(context, ImageViewActivity.class);
context.startActivity(intent);
}
});
}
更好的方式
更好的方法是使用委托模式。说明:单击项目不是适配器的责任。因此,为了简单起见,知识应该在其他地方。
MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
public class Delegate {
public void onClick(int imageResource);
}
private Delegate delegate;
public MyAdapter (Context context, ArrayList<Cell> galleryList, Delegate delegate){
this.galleryList = galleryList;
this.context = context;
this.delegate = delegate;
}
...
@Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, final int i){
viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
viewHolder.img.setImageResource(galleryList.get(i).getImg());
viewHolder.img.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
delegate.onClick(galleryList.get(i).getImg());
}
});
}
}
GalleryActivity
public class GalleryActivity extends AppCompatActivity implements MyAdapter.Delegate {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
...
MyAdapter adapter = new MyAdapter(getApplicationContext(), cells, this);
recyclerView.setAdapter(adapter);
...
}
@Override
public void onClick() {
Intent intent = new Intent(this, ImageViewActivity.class);
context.startActivity(intent);
}
}
如果您真的对代码的哪个部分感兴趣,可以看一下MVC,MVP,MVVM或Clean Architecture等架构模式(后者应该会取代其余的)。 Google提供了具有不同架构的示例项目: https://github.com/googlesamples/android-architecture
答案 1 :(得分:0)
您可以在适配器中创建一种方法,该方法使用所单击的项目信息并将其作为额外的信息传递给活动。
例如:
viewHolder.img.setImageResource(galleryList.get(i).getImg());
您可以使用此行获取图像并将其添加为额外内容。
要获取特定的项目数据,可以在适配器的视图保持器中使用类似的内容:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
galleryList.get(pos).getImg());
}
}
然后通过添加完成作业。
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("extraName", value);
startActivity(myIntent);