在我的片段中使用 AsyncTAsk 和 Pagination 后,我无法在我的 recyclerview 中获取任何项目

After using AsyncTAsk and Pagination in my fragment, I am not able to get any items in my recyclerview

提问人:NoTrEALMAURYA 提问时间:7/7/2023 最后编辑:NoTrEALMAURYA 更新时间:7/7/2023 访问量:46

问:

我正在使用 asynctask 和分页从我的存储中获取文件,但在使用 asynctask 和分页后我无法获取任何文件

fileList.addAll(findFiles(存储));这是我在 asyncTask 和分页之前用来加载我的文件的

我的 fileList 在片段中是 List,即private List<File> fileList;

我的 MyFragment.class 的 OncreateView

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

        internalStorageBinding = FragmentInternalBinding.inflate(inflater, container, false);
        View view = internalStorageBinding.getRoot();


        String internalStorage = System.getenv("EXTERNAL_STORAGE");
        storage = new File(internalStorage);

        txtPath = view.findViewById(R.id.txt_Path);
        txtPath.setText(storage.getAbsolutePath());

        // Retrieve the path argument from the fragment's arguments
        Bundle bundle = getArguments();
        if (bundle != null) {
            String path = bundle.getString("path");
            if (path != null) {
                txtPath.setText(path);
            }
        }

        try {
            data = getArguments().getString("path");
            File file = new File(data);
            storage = file;
        } catch (Exception e) {
            e.printStackTrace();
        }


        displayFiles();

        return view;
    }

    

    private void displayFiles() {
        internalStorageBinding.recyclerInternal.setVisibility(View.VISIBLE);
        internalStorageBinding.alternateView.setVisibility(View.GONE);

        fileList = new ArrayList<>();
        //fileList.addAll(findFiles(storage));  //this is i am using before my asyncTask and pagination to load my files
        fileAdapter = new FileAdapter(getContext(), fileList, this, getActivity().getPackageManager());
        internalStorageBinding.recyclerInternal.setAdapter(fileAdapter);

          fetchData();
        setupPagination();
    }

    private void setupPagination() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        internalStorageBinding.recyclerInternal.setLayoutManager(layoutManager);

        filePagination = new FilePagination(layoutManager) {
            @Override
            protected void loadMoreItems() {
                fetchData();
            }

            @Override
            public boolean isLastPage() {
                return false;
            }

            @Override
            public boolean isLoading() {
                return isLoading;
            }
        };

        internalStorageBinding.recyclerInternal.addOnScrollListener(filePagination);
    }

    private void fetchData() {
        internalStorageBinding.bottomProgressBarInternal.setVisibility(View.VISIBLE);
        isLoading = true;
        fileList.add(null);
        fileAdapter.notifyItemInserted(fileList.size() - 1);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                fileList.remove(fileList.size() - 1);
                int scrollPosition = fileList.size();
                fileAdapter.notifyItemRemoved(scrollPosition);
                // Call your AsyncTask here to fetch more items
                new FileAsyncTask(storage, currentPage, fileList, fileAdapter, internalStorageBinding).execute();
                isLoading = false;
            }
        }, 2000);
    }

这是MyAsync.java

import android.os.AsyncTask;
import android.view.View;
import com.maurya.simplefileexplorer.Adapters.FileAdapter;
import com.maurya.simplefileexplorer.databinding.FragmentInternalBinding;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileAsyncTask extends AsyncTask<Void, Void, ArrayList<File>> {
    private File storage;
    private int currentPage;
    private int ITEMS_PER_PAGE = 20;
    private List<File> fileList;
    private FileAdapter fileAdapter;
    private FragmentInternalBinding internalStorageBinding;
    private boolean isLoading = false;

    public FileAsyncTask(File storage, int currentPage,
                         List<File> fileList, FileAdapter fileAdapter,
                         FragmentInternalBinding internalStorageBinding) {
        this.storage = storage;
        this.currentPage = currentPage;
        this.fileList = fileList;
        this.fileAdapter = fileAdapter;
        this.internalStorageBinding = internalStorageBinding;
    }

    @Override
    protected ArrayList<File> doInBackground(Void... voids) {
        return findFiles(storage, currentPage, ITEMS_PER_PAGE);
    }

    @Override
    protected void onPostExecute(ArrayList<File> newFiles) {
        super.onPostExecute(newFiles);

        if (newFiles != null) {
            fileList.addAll(newFiles);
            fileAdapter.notifyDataSetChanged();
        } else {

        }
        isLoading = false;
        internalStorageBinding.bottomProgressBarInternal.setVisibility(View.GONE);
    }

    @Override
    protected void onPreExecute() {
        internalStorageBinding.bottomProgressBarInternal.setVisibility(View.VISIBLE);
        isLoading = true;
        fileList.add(null);
        fileAdapter.notifyItemInserted(fileList.size() - 1);
    }

    @Override
    protected void onProgressUpdate(Void... values) {

    }

    private ArrayList<File> findFiles(File file, int page, int itemsPerPage) {
        ArrayList<File> arrayList = new ArrayList<>();
        File[] files = file.listFiles();

        if (files != null) {
            int startIndex = (page - 1) * itemsPerPage;
            int endIndex = Math.min(startIndex + itemsPerPage, files.length);

            for (int i = startIndex; i < endIndex; i++) {
                File singleFile = files[i];
                if (singleFile.isDirectory() && !singleFile.isHidden()) {
                    arrayList.add(singleFile);
                } else if (singleFile.isFile()) {
                    arrayList.add(singleFile);
                }
            }

        }
        return arrayList;
    }
}

这是mypagination.java

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public abstract class FilePagination extends RecyclerView.OnScrollListener {
    public static final int PAGE_START = 1;
    @NonNull
    private LinearLayoutManager layoutManager;

    private static final int PAGE_SIZE = 10;

    public FilePagination(@NonNull LinearLayoutManager layoutManager) {
        this.layoutManager = layoutManager;
    }

    @Override
    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        int visibleItemCount = layoutManager.getChildCount();
        int totalItemCount = layoutManager.getItemCount();
        int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
        if (!isLoading() && !isLastPage()) {
            if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
                    && firstVisibleItemPosition >= 0
                    && totalItemCount >= PAGE_SIZE) {
                loadMoreItems();
            }
        }
    }

    protected abstract void loadMoreItems();

    public abstract boolean isLastPage();

    public abstract boolean isLoading();
}
    ```
java 分页 android-asynctask 本机-android

评论


答: 暂无答案