android recyclerview滚动使用firebase聊天聊天

时间:2017-06-21 07:46:16

标签: android firebase firebase-realtime-database

我只是实现RecyclerView来显示两者之间的聊天。一切正常,但是当我向上滚动并再次向下滚动发送者消息的方向时,有一个问题是左(GRAVITY),我只为收到的设置。请看下面的代码,希望你能理解。

滚动前的

屏幕截图。

enter image description here

滚动后的屏幕截图。

enter image description here

这是我的RecycleActivity.java

public class RecycleActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    DatabaseReference databaseReference,temp;
    EditText editText;
    Button btnSend;
    public static String nameOFUser = "";
    LinearLayoutManager linearLayoutManager;
    SharedPreferences sharedPreferences;

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

        sharedPreferences = getSharedPreferences("MyLoginPref",MODE_PRIVATE);
        nameOFUser = sharedPreferences.getString("name","");
        Log.e("name",nameOFUser);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerid);
        recyclerView.setHasFixedSize(true);
        linearLayoutManager= new LinearLayoutManager(this);

        linearLayoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(linearLayoutManager);

        databaseReference = FirebaseDatabase.getInstance().getReference().child("msg");
        editText = (EditText) findViewById(R.id.edittextmessagetosend);
        btnSend = (Button) findViewById(R.id.button41);

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                temp=databaseReference.push();
                temp.child("name").setValue(nameOFUser);
                temp.child("message").setValue(editText.getText().toString().trim());
                editText.setText("");
                recyclerView.setLayoutManager(linearLayoutManager);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        final FirebaseRecyclerAdapter<MessageBlog,MessageHolder> firebaseRecyclerAdapter
                = new FirebaseRecyclerAdapter<MessageBlog, MessageHolder>(
                MessageBlog.class,R.layout.message_row,MessageHolder.class,databaseReference
        ) {
            @Override
            protected void populateViewHolder(MessageHolder viewHolder, MessageBlog model, int position) {
                String name = model.getName();
                viewHolder.setSender(name);
                viewHolder.SetMessage(model.getMessage());
                viewHolder.setDirection(name);
            }
        };

        recyclerView.setAdapter(firebaseRecyclerAdapter);
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                recyclerView.smoothScrollToPosition(firebaseRecyclerAdapter.getItemCount());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

ViewHolder.java

public static class MessageHolder extends RecyclerView.ViewHolder{

   View view;
   LinearLayout lay,linearLayoutback;
   CardView cardViewreal;
   DatabaseReference mdatabase;

   public MessageHolder(View itemView) {
        super(itemView);
        view = itemView;
        linearLayoutback = (LinearLayout) 

        itemView.findViewById(R.id.linearlayoutback);

        mdatabase=FirebaseDatabase.getInstance().getReference().child("msg");

        lay = (LinearLayout) itemView.findViewById(R.id.idcardview);
        cardViewreal = (CardView) itemView.findViewById(R.id.idrealcard);
    }

    void setDirection(final String name){
        if(!name.equalsIgnoreCase(nameOFUser)) {
            lay.setGravity(Gravity.LEFT);
        cardViewreal.setCardBackgroundColor(Color.parseColor("#ffffff"));

        } else {
            lay.setGravity(Gravity.LEFT);
            cardViewreal.setCardBackgroundColor(Color.parseColor("#f5f4f5"));

        }
   }

   void setSender(String sender){
        TextView tvsender = (TextView) view.findViewById(R.id.senderidtv);
        tvsender.setText(sender);
    }

    void SetMessage(String message){
        TextView tvmessage = (TextView) view.findViewById(R.id.messageidtv);
        tvmessage.setText(message);
    }
}

1 个答案:

答案 0 :(得分:0)

检查您的设定方向。它始终设置左侧的方向

void setDirection(final String name){

            if(!name.equalsIgnoreCase(nameOFUser)) {
                lay.setGravity(Gravity.LEFT);
                cardViewreal.setCardBackgroundColor(Color.parseColor("#ffffff"));
                //linearLayoutback.setBackgroundResource(R.drawable.white);
            }
            else{
               lay.setGravity(Gravity.LEFT);
                cardViewreal.setCardBackgroundColor(Color.parseColor("#f5f4f5"));
              //  linearLayoutback.setBackgroundResource(R.drawable.white);
            }