使用FirebaseRecyclerAdapter

时间:2018-06-08 14:27:24

标签: android firebase android-recyclerview firebaseui

我正在使用新版本的FirebaseRecyclerAdapter(firebase-ui-database:3.1.0)来填充RecyclerView。

这是我的主要活动:

DatabaseReference mFirebaseDatabaseReference;
ChatFirebaseAdapter firebaseAdapter;

mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
      firebaseAdapter = new ChatFirebaseAdapter(new FirebaseRecyclerOptions.Builder<ChatModel>().setQuery(mFirebaseDatabaseReference.child(CHAT_REFERENCE),ChatModel.class).build(),userModel.getName(),this);
    firebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            int friendlyMessageCount = firebaseAdapter.getItemCount();
            int lastVisiblePosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
            if (lastVisiblePosition == -1 ||
                    (positionStart >= (friendlyMessageCount - 1) &&
                            lastVisiblePosition == (positionStart - 1))) {
                rvListMessage.scrollToPosition(positionStart);
            }
        }
    });
    rvListMessage.setLayoutManager(mLinearLayoutManager);
    rvListMessage.setAdapter(firebaseAdapter);

我在onStart / onStop方法中调用startListeningstopListening,如果我不调用startListening / stopListening,则不会发生异常,但recyclelerView不会显示任何内容。

这是ChatFirebaseAdapter类:

    public class ChatFirebaseAdapter extends FirebaseRecyclerAdapter<ChatModel,ChatFirebaseAdapter.MyChatViewHolder> {

    private static final int RIGHT_MSG = 0;
    private static final int LEFT_MSG = 1;
    private static final int RIGHT_MSG_IMG = 2;
    private static final int LEFT_MSG_IMG = 3;

    private ClickListenerChatFirebase mClickListenerChatFirebase;

    private String nameUser;



    public ChatFirebaseAdapter(FirebaseRecyclerOptions<ChatModel> options, String nameUser,ClickListenerChatFirebase mClickListenerChatFirebase) {
        super(options);
        this.nameUser = nameUser;
        this.mClickListenerChatFirebase = mClickListenerChatFirebase;
        Log.wtf("tagg","constr");

    }

    @NonNull
    @Override
    public MyChatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        Log.wtf("tagg","create");
        Log.wtf("tagg",Integer.toString(viewType));

        if (viewType == RIGHT_MSG){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_right,parent,false);
            return new MyChatViewHolder(view);
        }else if (viewType == LEFT_MSG){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_left,parent,false);
            return new MyChatViewHolder(view);
        }else if (viewType == RIGHT_MSG_IMG){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_right_img,parent,false);
            return new MyChatViewHolder(view);
        }else{
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_left_img,parent,false);
            return new MyChatViewHolder(view);
        }
    }

    protected void populateViewHolder(MyChatViewHolder viewHolder, ChatModel model, int position) {
        Log.wtf("tagg",model.getMessage());
        viewHolder.setIvUser(model.getUserModel().getPhoto_profile());
        viewHolder.setTxtMessage(model.getMessage());
        viewHolder.setTvTimestamp(model.getTimeStamp());
        viewHolder.tvIsLocation(View.GONE);
        if (model.getFile() != null){
            viewHolder.tvIsLocation(View.GONE);
            viewHolder.setIvChatPhoto(model.getFile().getUrl_file());
        }else if(model.getMapModel() != null){
            viewHolder.setIvChatPhoto(Util.local(model.getMapModel().getLatitude(),model.getMapModel().getLongitude()));
            viewHolder.tvIsLocation(View.VISIBLE);
        }
    }

    @Override
    public int getItemViewType(int position) {
        Log.wtf("tagg","type");

        ChatModel model = getItem(position);
        if (model.getMapModel() != null){
            if (model.getUserModel().getName().equals(nameUser)){
                return RIGHT_MSG_IMG;
            }else{
                return LEFT_MSG_IMG;
            }
        }else if (model.getFile() != null){
            if (model.getFile().getType().equals("img") && model.getUserModel().getName().equals(nameUser)){
                return RIGHT_MSG_IMG;
            }else{
                return LEFT_MSG_IMG;
            }
        }else if (model.getUserModel().getName().equals(nameUser)){
            return RIGHT_MSG;
        }else{
            return LEFT_MSG;
        }
    }



    @Override
    protected void onBindViewHolder(final MyChatViewHolder holder, int position, ChatModel model) {
        super.onBindViewHolder(holder, position);
        populateViewHolder(holder, model, position);
        Log.wtf("tagg","bind");


    }

    public class MyChatViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView tvTimestamp,tvLocation;
        EmojiconTextView txtMessage;
        ImageView ivUser,ivChatPhoto;

        public MyChatViewHolder(View itemView) {
            super(itemView);
            tvTimestamp = (TextView)itemView.findViewById(R.id.timestamp);
            txtMessage = (EmojiconTextView)itemView.findViewById(R.id.txtMessage);
            tvLocation = (TextView)itemView.findViewById(R.id.tvLocation);
            ivChatPhoto = (ImageView)itemView.findViewById(R.id.img_chat);
            ivUser = (ImageView)itemView.findViewById(R.id.ivUserChat);
        }

        @Override
        public void onClick(View view) {
            int position = getAdapterPosition();
            ChatModel model = getItem(position);
            if (model.getMapModel() != null){
                mClickListenerChatFirebase.clickImageMapChat(view,position,model.getMapModel().getLatitude(),model.getMapModel().getLongitude());
            }else{ 

mClickListenerChatFirebase.clickImageChat(view,position,model.getUserModel().getName(),model.getUserModel().getPhoto_profile(),model.getFile().getUrl_file());

            }
        }

public void setTxtMessage(String message){
            if (txtMessage == null)return;
            txtMessage.setText(message);
        }

        public void setIvUser(String urlPhotoUser){
            if (ivUser == null)return;
            Glide.with(ivUser.getContext()).load(urlPhotoUser).into(ivUser);
        }

        public void setTvTimestamp(String timestamp){
            if (tvTimestamp == null)return;
            tvTimestamp.setText(converteTimestamp(timestamp));
        }



public void setIvChatPhoto(String url){
        if (ivChatPhoto == null)return;
        Glide.with(ivChatPhoto.getContext()).load(url)
                .into(ivChatPhoto);
        ivChatPhoto.setOnClickListener(this);
    }

    public void tvIsLocation(int visible){
        if (tvLocation == null)return;
        tvLocation.setVisibility(visible);
    }
}

private CharSequence converteTimestamp(String mileSegundos){
    return 

DateUtils.getRelativeTimeSpanString(Long.parseLong(mileSegundos),System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);

} }

这是我在logcat中得到的错误:

FATAL EXCEPTION: main

java.lang.StackOverflowError: stack size 8MB

.....

Error reporting crash

android.os.TransactionTooLargeException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:504)

编辑:

在我从onBindViewHolder方法中删除super.onBindViewHolder(holder, position);后,应用程序正在运行。但我仍然不知道为什么之前没有用。

0 个答案:

没有答案