我想使用 recyclerview 将我的画廊加载到片段中。但它不起作用

时间:2021-08-01 16:47:46

标签: java android android-fragments android-recyclerview

我制作了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>

1 个答案:

答案 0 :(得分:0)

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