gpt4 book ai didi

java - 如何使用 AsyncTask、RecyclerView 和 RecyclerView.Adapter 将手机中的图像获取到 RecyclerView 中?

转载 作者:行者123 更新时间:2023-12-01 21:31:49 28 4
gpt4 key购买 nike

我正在尝试将手机中的图像加载到我的回收 View 中。

到目前为止,我已经创建了一个 recyclerview.adapter 和一个 gridlayoutmanager,并将它们附加到我的 recyclerview 中。我可以成功检索图像的完整路径并使用异步任务将它们添加到适配器。

 @Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);

settingUp();
run();

}


public void settingUp(){

//setting up recycler view
recyclerView = findViewById(R.id.imageGallery);
recyclerView.setHasFixedSize(true);

//setting up recyclerview layout manager
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),2);
recyclerView.setLayoutManager(layoutManager);

//setting up recyclerview adapter
adapter = getInstance(getApplicationContext());

recyclerView.setAdapter(adapter);

// instance of load task
loadTask = new LoadTask();
}

private void run() {

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
else
{
loadTask.execute();
}
}

/**
* Loads the images into the cursor using a background thread
*/

private class LoadTask extends AsyncTask<Void,Void, Cursor>
{

// images are loaded into the cursor here, in the background.
@Override
protected Cursor doInBackground(Void... voids) {
loadImagesIntoCursor();
return imageCursor;
}

// this method runs after doInBackground finishes
@Override
protected void onPostExecute(Cursor cursor) {
transferImagesToAdapter(cursor);
}
}

//*********************************************************************************************************

/**
* Loads all the Images from external storage into the cursor.
*/
private void loadImagesIntoCursor()
{
imageCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media.DATA}, null, null,
MediaStore.Images.Media.DATE_ADDED + " DESC");
}

private void transferImagesToAdapter(Cursor imageCursor)
{
String path;
if(imageCursor != null)
{
imageCursor.moveToFirst();
while (!imageCursor.isAfterLast()){
path = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
adapter.add(path);
imageCursor.moveToNext();
}
imageCursor.close();

}

}

/** MY ADAPTER CLASS **/

class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder>{

private ArrayList<String> images;
//private static ImageAdapter singleton = null;
Context context;

public ImageAdapter(Context context, ArrayList<String> paths)
{
images = paths;
this.context = context;
}

public ImageAdapter(Context context)
{

this.context = context;
}



//--------- OVERRIDE METHODS ---------------------------------------------
@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_layout,parent);
return new ImageViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
// load image into image holder view using picasso library
Picasso.with(context).load(images.get(position)).into(holder.image);
}

@Override
public int getItemCount() {
return images.size();
}

//------------------------------------------------------------------------

public void add(String path){
this.images.add(path);
}

//------------------------------------------------------------------------

public class ImageViewHolder extends RecyclerView.ViewHolder{
protected ImageView image;
public ImageViewHolder(View view){
super(view);
this.image = view.findViewById(R.id.img);
}
}

}

但是,添加图像路径后,我不知道是什么触发了适配器中的 onCreateViewHolder 和 onBindViewHolder 方法,这些方法将在 recyclerview 中显示我的图像。请帮忙!

最佳答案

更新 RecyclerView 列表项需要做两件事。

  1. 更新适配器内的数据列表
  2. 通知数据集更改。
 private void transferImagesToAdapter(Cursor imageCursor)
{
String path;
if(imageCursor != null)
{
imageCursor.moveToFirst();
while (!imageCursor.isAfterLast()){
path = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
adapter.add(path); // If it update data list in your adapter then fine.
adapter.notifyDataSetChanged(); // It will do all the callbacks and update your screen.

imageCursor.moveToNext();
}
imageCursor.close();

}

}

关于java - 如何使用 AsyncTask、RecyclerView 和 RecyclerView.Adapter 将手机中的图像获取到 RecyclerView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58811179/

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