如何在android studio中将分页添加到recycleview

jm2pwxwz  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(326)

大家好,请问我如何使用java将分页添加到android studio中的recycleview中。我正在尝试从firebase数据库加载照片,该数据库工作正常,但我遇到的问题是,它试图一次加载所有照片,这会使应用程序运行缓慢。如何首先加载列表6中的照片,并在用户滚动时加载更多照片。
这是我的片段的代码
公共类memesfragment扩展了片段{

private RecyclerView recyclerViewMemePosts;
private MemePostAdapter memePostAdapter;
private List<MemePosts> memePosts;
private DatabaseReference root = FirebaseDatabase.getInstance().getReference("MemePosts");

private ProgressBar progressBar;

private Button upload;
private Context mContext;
private AdView mAdView, adView;
private ImageView shocker;
private InterstitialAd mInterstitialAd;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_memes, container, false);

    try {
        progressBar = view.findViewById(R.id.progress_circular);
        shocker = view.findViewById(R.id.memeMaker);

        shocker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, MemeMakerActivity.class));
            }
        });

        recyclerViewMemePosts = view.findViewById(R.id.recycleViewPostsMeme);
        recyclerViewMemePosts.setHasFixedSize(true);
        recyclerViewMemePosts.setItemViewCacheSize(20);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setStackFromEnd(true);
        linearLayoutManager.setReverseLayout(true);
        recyclerViewMemePosts.setLayoutManager(linearLayoutManager);
        memePosts = new ArrayList<>();
        memePostAdapter = new MemePostAdapter(getContext(), memePosts);
        recyclerViewMemePosts.setAdapter(memePostAdapter);

        mContext = getActivity();

        root.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                try {
                    for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                        MemePosts model = dataSnapshot.getValue(MemePosts.class);
                        memePosts.add(model);
                    }
                    memePostAdapter.notifyDataSetChanged();
                    progressBar.setVisibility(View.GONE);

                } catch (Exception e) {

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

        upload = view.findViewById(R.id.Upload);

        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, MemePostActivity.class));

            }
        });
        MobileAds.initialize(mContext, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

        MobileAds.initialize(mContext, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

        mAdView = view.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        adView.loadAd(adRequest);
    } catch (Exception e) {
        e.getMessage();
    }

    return view;

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题