android– 用firebase在无尽的回收站视图中分页

android– 用firebase在无尽的回收站视图中分页,第1张

概述我正在开发Q/A应用程序.我已成功加载firebase的问题.但我无法从Firebase中应用分页数据库.如何识别我们已经到达了回收站视图的末尾,以便可以加载下几个问题.解决方法:要认识到我们已经到达RecyclerView的末尾,您可以使用此类EndlessRecyclerOnScrollListener.java要加载更多下

我正在开发Q / A应用程序.我已成功加载firebase的问题.但我无法从Firebase中应用分页数据库.如何识别我们已经到达了回收站视图的末尾,以便可以加载下几个问题.

解决方法:

要认识到我们已经到达RecyclerVIEw的末尾,您可以使用此类EndlessRecyclerOnScrollListener.java

要加载更多下一个问题,您应该在Question类中定义另一个字段,如number

public class Question {    private int number; // it must unique and auto increase when you add new question    ...}

然后,当您从FireBase加载问题时,您可以这样做

public class MainActivity extends AppCompatActivity {    private static final int TOTAL_ITEM_EACH_LOAD = 10;    private DatabaseReference mDatabase;    final List<Question> questionList = new ArrayList<>();    private int currentPage = 0;    private RecyclerVIEw recyclerVIEw;    private RecyclerVIEwAdapter mAdapter;     @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        ...        // init and set layout manager for your RecyclerVIEw        ...        mAdapter = new RecyclerVIEwAdapter(questionList);        recyclerVIEw.setAdapter(mAdapter);        recyclerVIEw.setonScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {            @OverrIDe            public voID onl oadMore(int current_page) { // when we have reached end of RecyclerVIEw this event fired                loadMoreData();            }        });        loadData(); // load data here for first time launch app    }    private voID loadData() {        // example        // at first load : currentPage = 0 -> we startAt(0 * 10 = 0)        // at second load (first loadmore) : currentPage = 1 -> we startAt(1 * 10 = 10)        mDatabase.child("questions")                .limitToFirst(TOTAL_ITEM_EACH_LOAD)                .startAt(currentPage*TOTAL_ITEM_EACH_LOAD)                .orderByChild("number")                .addValueEventListener(new ValueEventListener() {                    @OverrIDe                    public voID onDataChange(DataSnapshot dataSnapshot) {                        if(!dataSnapshot.hasChildren()){                            Toast.makeText(MainActivity.this, "No more questions", Toast.LENGTH_SHORT).show();                            currentPage--;                        }                        for (DataSnapshot data : dataSnapshot.getChildren()) {                            Question question = data.getValue(Question.class);                            questionList.add(question);                            mAdapter.notifyDataSetChanged();                        }                    }                   @OverrIDe public voID onCancelled(DatabaseError databaseError) {}});    }    private voID loadMoreData(){        currentPage++;        loadData();    }}

Here is my DEMO project

总结

以上是内存溢出为你收集整理的android – 用firebase在无尽的回收站视图中分页全部内容,希望文章能够帮你解决android – 用firebase在无尽的回收站视图中分页所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1115185.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存