2017-10-02 10 views
0

ユーザーがビューを上下にスクロールしようとするとスクロールに問題が発生します。リサイクルが適切に機能しないように見えますが、コードのどの部分がそのような動作の原因になるかはわかりませんでした。スクロール時にRecyclerViewが正しく動作しない

視覚的な表現については、スクリーンショットをご覧ください。

public class ChatRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ 
    private static final int VIEW_TYPE_ME=1; 
    private static final int VIEW_TYPE_OTHER=2; 

    private List<Chat>myChat; 

    public ChatRecyclerAdapter(List<Chat> myChat) { 
     this.myChat = myChat; 
    } 

    public void add(Chat chat){ 
     myChat.add(chat); 
     notifyItemInserted(myChat.size()-1); 
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext()); 
     RecyclerView.ViewHolder viewHolder=null; 
     switch (viewType){ 
      case VIEW_TYPE_ME: 
       View viewChatMine=layoutInflater.inflate(R.layout.item_chat_mine,parent,false); 
       viewHolder=new MyChatViewHolder(viewChatMine); 
       break; 
      case VIEW_TYPE_OTHER: 
       View viewChatOther=layoutInflater.inflate(R.layout.item_chat_other,parent,false); 
       viewHolder=new OtherChatViewHolder(viewChatOther); 
       break; 
     } 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
     if(TextUtils.equals(myChat.get(position).senderUid, FirebaseAuth.getInstance().getCurrentUser().getUid())){ 
       configureMyChatViewHolder((MyChatViewHolder)holder,position); 
     }else{ 
      configureOtherChatViewHolder((OtherChatViewHolder)holder,position); 
     } 
    } 
    public void configureMyChatViewHolder(MyChatViewHolder myChatViewHolder,int position){ 
     Chat chat=myChat.get(position); 

     myChatViewHolder.txtChatMessage.setText(chat.message); 
    } 
    public void configureOtherChatViewHolder(OtherChatViewHolder otherChatViewHolder,int position){ 
     Chat chat=myChat.get(position); 
     otherChatViewHolder.txtChatMessage.setText(chat.message); 
    } 

    @Override 
    public int getItemViewType(int position) { 
     if(TextUtils.equals(myChat.get(position).senderUid,FirebaseAuth.getInstance().getCurrentUser().getUid())){ 
      return VIEW_TYPE_ME; 
     }else{ 
      return VIEW_TYPE_OTHER; 
     } 
    } 

    @Override 
    public int getItemCount() { 
     if(myChat!=null){ 
      return myChat.size(); 
     } 
     return 0; 
    } 

    private static class MyChatViewHolder extends RecyclerView.ViewHolder{ 
     private TextView txtChatMessage; 

     public MyChatViewHolder(View itemView){ 
      super(itemView); 
      txtChatMessage=(TextView)itemView.findViewById(R.id.chatMineTextView); 
     } 

    } 
    private static class OtherChatViewHolder extends RecyclerView.ViewHolder{ 
     private TextView txtChatMessage; 

     public OtherChatViewHolder(View itemView){ 
      super(itemView); 
      txtChatMessage=(TextView) itemView.findViewById(R.id.chatOtherTextView); 
     } 
    } 
} 

ChAT活性フラグメント:

package com.example.ayselkas.myapplication.Fragments; 

import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.widget.RecyclerView; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 


import com.example.ayselkas.myapplication.Activity.ChatActivity; 
import com.example.ayselkas.myapplication.Adapters.ChatRecyclerAdapter; 
import com.example.ayselkas.myapplication.LocalStorage.Constants; 
import com.example.ayselkas.myapplication.Models.Chat; 
import com.example.ayselkas.myapplication.R; 
import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.database.ChildEventListener; 
import com.google.firebase.database.DataSnapshot; 
import com.google.firebase.database.DatabaseError; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 
import com.google.firebase.database.ValueEventListener; 

import org.greenrobot.eventbus.EventBus; 

import java.util.ArrayList; 

import static com.google.android.gms.internal.zzs.TAG; 


/** 
* A placeholder fragment containing a simple view. 
*/ 
public class ChatActivityFragment extends Fragment { 
    private RecyclerView myRecyclerViewChat; 
    private EditText myTxtMessage; 
    private ProgressDialog myProgressDialog; // progress bar can be used instead 
    private ChatRecyclerAdapter myChatRecyclerAdapter; 
    private Button sendButton; 
    private Chat chat; 

    /////////// 

    /////////// 



    public static ChatActivityFragment newInstance(String receiver, 
                String receiverUid, 
                String firebaseToken) { 
     Bundle args = new Bundle(); 
     args.putString(Constants.ARG_RECEIVER, receiver); 
     args.putString(Constants.ARG_RECEIVER_UID, receiverUid); 
     args.putString(Constants.ARG_FIREBASE_TOKEN, firebaseToken); 
     ChatActivityFragment fragment = new ChatActivityFragment(); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View fragmentView = inflater.inflate(R.layout.fragment_chat, container, false); 
     sendButton=(Button) fragmentView.findViewById(R.id.sendMessageButton); 
     sendButton.setEnabled(false); 
     sendButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       sendMessage(); 
      } 
     }); 
     bindViews(fragmentView); 
     return fragmentView; 
    } 

    private void bindViews(View view) { 
     myRecyclerViewChat = (RecyclerView) view.findViewById(R.id.chatRecyclerView); 
     myTxtMessage = (EditText) view.findViewById(R.id.typeText); 
     myTxtMessage.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

      } 

      @Override 
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
       if(charSequence.toString().trim().length() > 0) { 
        sendButton.setEnabled(true);} 
      } 

      @Override 
      public void afterTextChanged(Editable editable) { 

      } 
     }); 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     init(); 
    } 

    private void init() { 
     myProgressDialog = new ProgressDialog(getActivity()); 
     myProgressDialog.setTitle("LOading"); 
     myProgressDialog.setMessage("Please Wait"); 
     myProgressDialog.setIndeterminate(true); 
     getMessageFromFirebaseUser(FirebaseAuth.getInstance().getCurrentUser().getUid(), 
       getActivity().getIntent().getExtras().getString(Constants.ARG_RECEIVER_UID)); 

//  mChatPresenter.getMessage(FirebaseAuth.getInstance().getCurrentUser().getUid(), 
//    getArguments().getString(Constants.ARG_RECEIVER_UID)); 
    } 


    private void sendMessage() { 
     String message = myTxtMessage.getText().toString(); 
     String receiver = getActivity().getIntent().getExtras().getString(Constants.ARG_RECEIVER); 
     String receiverUid = getActivity().getIntent().getExtras().getString(Constants.ARG_RECEIVER_UID); 
     String sender = FirebaseAuth.getInstance().getCurrentUser().getEmail(); 
     String senderUid = FirebaseAuth.getInstance().getCurrentUser().getUid(); 
     Chat chat = new Chat(sender, 
       receiver, 
       senderUid, 
       receiverUid, 
       message, 
       System.currentTimeMillis()); 
     sendMessageToFirebaseUser(chat); 
    } 

    public void sendMessageToFirebaseUser(final Chat chat) { 
     final String room_type_1 = chat.senderUid + "_" + chat.receiverUid; 
     final String room_type_2 = chat.receiverUid + "_" + chat.senderUid; 

     final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference(); 

     databaseReference.child(Constants.ARG_CHAT_ROOMS).getRef().addListenerForSingleValueEvent(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       if (dataSnapshot.hasChild(room_type_1)) { 
        Log.e(TAG, "sendMessageToFirebaseUser: " + room_type_1 + " exists"); 
        databaseReference.child(Constants.ARG_CHAT_ROOMS).child(room_type_1).child(String.valueOf(chat.timestamp)).setValue(chat); 
       } else if (dataSnapshot.hasChild(room_type_2)) { 
        Log.e(TAG, "sendMessageToFirebaseUser: " + room_type_2 + " exists"); 
        databaseReference.child(Constants.ARG_CHAT_ROOMS).child(room_type_2).child(String.valueOf(chat.timestamp)).setValue(chat); 
       } else { 
        Log.e(TAG, "sendMessageToFirebaseUser: success"); 
        databaseReference.child(Constants.ARG_CHAT_ROOMS).child(room_type_1).child(String.valueOf(chat.timestamp)).setValue(chat); 
        getMessageFromFirebaseUser(chat.senderUid, chat.receiverUid); 
       } 
       // send push notification to the receiver 
//    sendPushNotificationToReceiver(chat.sender, 
//      chat.message, 
//      chat.senderUid, 
//      new SharedPrefUtil(context).getString(Constants.ARG_FIREBASE_TOKEN), 
//      receiverFirebaseToken); 
        onSendMessageSuccess(); 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
      } 
     }); 
    } 
    public void getMessageFromFirebaseUser(String senderUid, String receiverUid) { 
     final String room_type_1 = senderUid + "_" + receiverUid; 
     final String room_type_2 = receiverUid + "_" + senderUid; 
     final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference(); 

     databaseReference.child(Constants.ARG_CHAT_ROOMS).getRef() 
       .addListenerForSingleValueEvent(new ValueEventListener() { 

        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 
         if(dataSnapshot.hasChild(room_type_1)) { 
          FirebaseDatabase.getInstance() 
            .getReference() 
            .child(Constants.ARG_CHAT_ROOMS) 
            .child(room_type_1).addChildEventListener(new ChildEventListener() { 
           @Override 
           public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
            Chat chat = dataSnapshot.getValue(Chat.class); 
            onGetMessageSuccess(chat); 
           } 

           @Override 
           public void onChildChanged(DataSnapshot dataSnapshot, String s) { 

           } 

           @Override 
           public void onChildRemoved(DataSnapshot dataSnapshot) { 

           } 

           @Override 
           public void onChildMoved(DataSnapshot dataSnapshot, String s) { 

           } 

           @Override 
           public void onCancelled(DatabaseError databaseError) { 

           } 
          }); 
         }else if(dataSnapshot.hasChild(room_type_2)) { 
          FirebaseDatabase.getInstance().getReference().child(Constants.ARG_CHAT_ROOMS) 
            .child(room_type_2).addChildEventListener(new ChildEventListener() { 
           @Override 
           public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
            Chat chat =dataSnapshot.getValue((Chat.class)); 
           } 

           @Override 
           public void onChildChanged(DataSnapshot dataSnapshot, String s) { 

           } 

           @Override 
           public void onChildRemoved(DataSnapshot dataSnapshot) { 

           } 

           @Override 
           public void onChildMoved(DataSnapshot dataSnapshot, String s) { 

           } 

           @Override 
           public void onCancelled(DatabaseError databaseError) { 

           } 
          }); 
         }else{ 
          Log.e(TAG,"getMessageFromFirebaseUser: no such room available"); 
         } 
        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) { 

        } 
       }); 
    } 




    public void onGetMessageSuccess(Chat chat){ 
     if(myChatRecyclerAdapter==null||myChatRecyclerAdapter.getItemCount()==0){ 
      myChatRecyclerAdapter=new ChatRecyclerAdapter(new ArrayList<Chat>()); 
      myRecyclerViewChat.setAdapter(myChatRecyclerAdapter); 
     } 
     myChatRecyclerAdapter.add(chat); 
     myRecyclerViewChat.smoothScrollToPosition(myChatRecyclerAdapter.getItemCount()-1); 

    } 
    public void onSendMessageSuccess(){ 
     myTxtMessage.setText(""); 
    } 

} 

そして最後にXMLコードをチャット

1) Just after launch

2)ここで

After scrollingは私のリサイクルビュー・アダプタです。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:id="@+id/fragment_chat" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.ayselkas.myapplication.Fragments.ChatActivityFragment" 
    tools:showIn="@layout/activity_chat"> 


    <android.support.v7.widget.RecyclerView 
     android:id="@+id/chatRecyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/typeText" 
     app:layoutManager="LinearLayoutManager"/> 

    <EditText 
     android:id="@+id/typeText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="@drawable/send_box" 
     android:ems="10" 
     android:layout_margin="8dp" 
     android:hint="Type a message..." 
     android:imeOptions="actionSend" 
     android:inputType="text" 
     android:padding="8dp" /> 

    <Button 
     android:id="@+id/sendMessageButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="send" /> 

</RelativeLayout> 
+1

ようこそ!何がうまくいかないか、具体的な質問をしてください。 – DSway

+0

「スクロール後」の画像を見てください。ここでスクロールが正しく動作しないことがわかります。私が最初にアプリケーションを起動すると、9つの読み込まれたメッセージ(「起動直後」のイメージを参照)がアダプタに送られ、次に私がスクロールしているときに、この9つの読み込まれたメッセージは画面に残ります。私はリサイクルはうまくいかないと思うが、どこに問題があるのか​​わからない。 –

+0

私は3つのリンクを添付することはできませんので、私はここにビデオへのリンクを入れますhttps://youtu.be/gCrqx1Qa5dw –

答えて

0

問題はアクティビティフラグメント宣言ででした。私はactivity.xmlの宣言とフラグメントトランザクションの両方を行ったので、このようなアニメーションの問題が発生しました。

関連する問題