【问题标题】:I want to load my gallery to fragment using recyclerview. But it doesn't work我想使用 recyclerview 将我的画廊加载到片段中。但它不起作用
【发布时间】:2021-08-01 16:47:46
【问题描述】:

我制作了 BottomNavigationView,我想在其中一张中加载我的图库图片。 我使用带有光标的 Glide 来获取图片的 uri(在片段代码中)。 并制作了 RecyclerView Adapter 来显示图片。但它不起作用。 没有错误,但是当我在手机上运行我的项目时,它没有显示任何图片... 问题是什么? :(


    private RecyclerView recyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

       View view =  inflater.inflate(R.layout.fragment_gallery, container, false);
       initData();
       return view;
    }

    public void initData(){
        listt = getPic();
        recyclerView = getView().findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        recyclerView.setAdapter(new exAdap(listt, getContext()));
    }

    public ArrayList<String> getPic() {
        ArrayList<String> fileList = new ArrayList<>();
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};

        Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, MediaStore.MediaColumns.DATE_ADDED + " desc");
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        int columnDisplayname = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
        int lastIndex;
        while (cursor.moveToNext()) {
            String absolutePathOfImage = cursor.getString(columnIndex);
            String nameOfFile = cursor.getString(columnDisplayname);
            lastIndex = absolutePathOfImage.lastIndexOf(nameOfFile);
            lastIndex = lastIndex >= 0 ? lastIndex : nameOfFile.length() - 1;

            if (!TextUtils.isEmpty(absolutePathOfImage)) {
                fileList.add(absolutePathOfImage);
            }
        }
        cursor.close();
        return fileList;
    }

    private ArrayList<String> datas;
    private Context context;

    public exAdap(ArrayList<String> datas, Context context) {
        this.datas=datas;
        this.context = context;
    }

    @NonNull
    @Override
    public exAdap.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        view = layoutInflater.inflate(R.layout.activity_gallery, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Glide.with(context).load(datas.get(position)).into(holder.getImage());
        holder.image.setImageURI(Uri.parse(datas.get(position)));
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        ImageView image;

        public ViewHolder(View itemView) {
            super(itemView);
            image=itemView.findViewById((R.id.recylcerview_row_image));

        }

        public ImageView getImage(){return this.image;}
    }

fragment_gallery.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment_gallery">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="409dp"
        android:layout_height="460dp"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_gallery.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        android:id="@+id/recylcerview_row_image"
        android:src="@mipmap/ic_launcher_round"
        android:adjustViewBounds="true"
        android:layout_height="wrap_content" />


</RelativeLayout>

【问题讨论】:

    标签: java android android-fragments android-recyclerview


    【解决方案1】:

    您同时使用了两种方法,而不是只使用一种。因为这两种方法都是在 imageview 中加载图像。所以最好一次只尝试一种方法,看看这是否会有所作为。

    【讨论】:

    • 我将onBindViewHolder修改为,Uri uri = Uri.parse(datas.get(position)); Glide.with(context).load(uri).into(holder.getImage());但它没有改变。我应该换一条线吗...?谢谢你的回答。
    • 您检查过文件列表大小吗?确保您首先获取数据。写入日志以检查文件列表大小。如果没问题,请编写您的项目布局 xml 代码,以便我查看并告诉您是否有任何问题。
    • 我检查了我的 logcat 并在加载我的图片资源时遇到了一堆错误“java.lang.NullPointerException”......!!!我正在尝试修复它。我将我的 xml 代码添加到我的问题中以防万一。非常感谢......??
    • 我只修改了 onBindViewHolder 一行 'Glide.with(context).load(datas.get(position)).into(holder.getImage());',就可以了!!!! !!!!!!谢谢............???
    • 不错。觉得有用别忘了点赞哦:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 2020-06-29
    • 2022-11-18
    • 2020-03-19
    • 1970-01-01
    • 2014-12-28
    相关资源
    最近更新 更多