使用FirebaseRecyclerPagination创建chatapp

时间:2019-05-24 11:23:42

标签: android firebase firebase-realtime-database android-recyclerview

我正在尝试使用Firebase作为数据库来创建聊天应用程序。我想要实现FirebaseRecyclerPagination,因为我不想立即加载所有聊天记录,而是希望每次刷新仅加载一定数量的聊天记录。我正在关注本教程[使用Firebase Recycler分页库的示例应用程序] (https://firebaseopensource.com/projects/patilshreyas/firebaserecyclerpagination/app/readme.md)。但是仍然没有得到我想要的结果。

这是我当前的代码

public class ChatTestActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private DatabaseReference mDatabase;

    private SwipeRefreshLayout mSwipeRefreshLayout;
    RecyclerView.LayoutManager layoutManager;

    FirebaseRecyclerPagingAdapter<ChatMessage, PostViewHolder> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_test);


        mSwipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);

        //Initialize RecyclerView
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);

        LinearLayoutManager mManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mManager);

        //Initialize Database
        mDatabase = FirebaseDatabase.getInstance().getReference().child("conversations").child("message");

        final PagedList.Config config = new PagedList.Config.Builder()
                .setEnablePlaceholders(false)
                .setInitialLoadSizeHint(2)
                .setPrefetchDistance(2)
                .setPageSize(2)
                .build();

        final DatabasePagingOptions<ChatMessage> options = new DatabasePagingOptions.Builder<ChatMessage>()
                .setLifecycleOwner(this)
                .setQuery(mDatabase, config, ChatMessage.class)
                .build();

        mAdapter = new FirebaseRecyclerPagingAdapter<ChatMessage, PostViewHolder>(options) {
            @NonNull
            @Override
            public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


                return new PostViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_message, parent, false));
            }

            @Override
            protected void onBindViewHolder(@NonNull PostViewHolder holder,
                                            int position,
                                            @NonNull ChatMessage model) {

                holder.setItem(model);

            }

            @Override
            protected void onLoadingStateChanged(@NonNull LoadingState state) {
                switch (state) {
                    case LOADING_INITIAL:
                    case LOADING_MORE:
                        // Do your loading animation
                       mSwipeRefreshLayout.setRefreshing(true);

                        break;

                    case LOADED:
                        // Stop Animation
                        mSwipeRefreshLayout.setRefreshing(false);
                        break;

                    case FINISHED:
                        //Reached end of Data set
                        mSwipeRefreshLayout.setRefreshing(false);
                        break;

                    case ERROR:
                        retry();
                        break;
                }
            }

            @Override
            protected void onError(@NonNull DatabaseError databaseError) {
                super.onError(databaseError);
                mSwipeRefreshLayout.setRefreshing(false);
                databaseError.toException().printStackTrace();
            }
    };
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mAdapter.refresh();
            }
        });



        mRecyclerView.setAdapter(mAdapter);
    }

    //Start Listening Adapter
    @Override
    protected void onStart() {
        super.onStart();
        mAdapter.startListening();
    }

    //Stop Listening Adapter
    @Override
    protected void onStop() {
        super.onStop();
        mAdapter.stopListening();
    }

    public class PostViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView msgMessage;
        public final TextView user;
        TextView mAuthor;
        TextView mDate;
        public ChatMessage mItem;

        public PostViewHolder(View view) {
            super(view);
            mView = view;
            msgMessage = (TextView) view.findViewById(R.id.msgMessage);
            user = (TextView) view.findViewById(R.id.user);

        }

        public void setItem(ChatMessage chatMessage){
            msgMessage.setText(chatMessage.messageText);
            user.setText(chatMessage.messageUser);

        }

        public TextView getMsgMessage() {
            return msgMessage;
        }

        @Override
        public String toString() {
            return super.toString() + " '" + user.getText() + "'";
        }
    }


}

这是我的activity_chat_test.xml文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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"
    android:id="@+id/swipe_refresh_layout"
    tools:context=".ChatTestActivity">


    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recycler_view">

    </androidx.recyclerview.widget.RecyclerView>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

0 个答案:

没有答案