2017-09-07 17 views
-1

私はexpandable recyclerviewライブラリ(this)を使用しています。このrecyclerviewを作成するために、私はfirebaseバックエンドサービスを使用しています。私はこの拡張可能なrecyclerviewを使用してチャットユーザーを表示し、それは動作します。しかし、私の問題は、一部の人々がオンライン状態をオフライン状態に変更したり、逆の場合に、私のrecyclerviewの更新とオンライン状態の変更後に展開されたすべてのグループが崩壊したことです。拡張可能なrecyclerviewとfirebase

マイコード:

ViewHolder

GroupHolder

public class ClubNameViewHolder extends GroupViewHolder { 

View view; 
TextView clubName2; 
CircleImageView clubLogoImg; 
TextView onlineTexview; 
FirebaseDatabase mDatabase; 

public ClubNameViewHolder(View itemView) { 
    super(itemView); 
    view = itemView; 
    clubName2 = (TextView) view.findViewById(R.id.clubName_Users); 
    clubLogoImg = (CircleImageView) view.findViewById(R.id.parent_club_logo); 
    onlineTexview = (TextView) view.findViewById(R.id.parent_online_text); 
} 

public void setClubTitle(ExpandableGroup club) { 
    if (club instanceof UserChatGroup) { 
     clubName2.setText(club.getTitle()); 

     Picasso.with(itemView.getContext()).load(((UserChatGroup) club).getClubLogo()).into(clubLogoImg); 
     onlineTexview.setText(((UserChatGroup) club).getOnline()); 
    } 

} 
} 

ChildHolder

public class UsersChatViewHolder extends ChildViewHolder { 

View view; 
TextView usernameUser; 
CircleImageView flagUser; 
CircleImageView profileImageUser; 
ImageView onlineImage; 
FirebaseAuth mAuth; 

public UsersChatViewHolder(View itemView) { 
    super(itemView); 

    view = itemView; 
    usernameUser = (TextView) view.findViewById(R.id.usernameUsers); 
    flagUser = (CircleImageView) view.findViewById(R.id.flagUsers); 
    profileImageUser = (CircleImageView) view.findViewById(R.id.profileUsers); 
    onlineImage = (ImageView) view.findViewById(R.id.onlineStatus); 
    mAuth = FirebaseAuth.getInstance(); 
} 

public void setOnlineImage(){ 
    Log.i("online", String.valueOf(MainPage.myClubName)); 
    Log.i("online", String.valueOf(mAuth.getCurrentUser().getUid())); 
    final DatabaseReference onlineReference = FirebaseDatabase.getInstance().getReference().child("UsersChat").child(MainPage.myClubName).child(mAuth.getCurrentUser().getUid()); 
    onlineReference.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      boolean isOnline = (boolean) dataSnapshot.child("online").getValue(); 
      Log.i("online", String.valueOf(isOnline)); 

      if (isOnline){ 
       onlineImage.setVisibility(View.VISIBLE); 
      } else { 
       onlineImage.setVisibility(View.GONE); 
      } 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 
} 

public void setUsername (String username){ 
    usernameUser.setText(username); 
} 

public void setFlag(Context ctx, String flag){ 

    GenericRequestBuilder<Uri, InputStream, SVG, PictureDrawable> requestBuilder; 

    requestBuilder = Glide 
      .with(ctx) 
      .using(Glide.buildStreamModelLoader(Uri.class, ctx), InputStream.class) 
      .from(Uri.class) 
      .as(SVG.class) 
      .transcode(new SvgDrawableTranscoder(), PictureDrawable.class) 
      .sourceEncoder(new StreamEncoder()) 
      .cacheDecoder(new FileToStreamDecoder<SVG>(new SearchableCountry.SvgDecoder())) 
      .decoder(new SearchableCountry.SvgDecoder()) 
      .animate(android.R.anim.fade_in); 


    Uri uri = Uri.parse(flag); 
    requestBuilder 
      // SVG cannot be serialized so it's not worth to cache it 
      .diskCacheStrategy(DiskCacheStrategy.SOURCE) 
      .load(uri) 
      .into(flagUser); 

} 

public void setProfileImage(Context ctx, String proifleImage){ 
    Picasso.with(ctx).load(proifleImage).into(profileImageUser); 
} 
} 

モデル

UsersModel

public class UserChat { 

String username; 
String flag; 
String profileImage; 

public UserChat(String username, String flag, String profileImage) { 
    this.username = username; 
    this.flag = flag; 
    this.profileImage = profileImage; 
} 

public String getUsername() { 
    return username; 
} 

public void setUsername(String username) { 
    this.username = username; 
} 

public String getFlag() { 
    return flag; 
} 

public void setFlag(String flag) { 
    this.flag = flag; 
} 

public String getProfileImage() { 
    return profileImage; 
} 

public void setProfileImage(String profileImage) { 
    this.profileImage = profileImage; 
} 
} 

グループモデル

public class UserChatGroup extends ExpandableGroup { 
String clubLogo; 
String online; 
public UserChatGroup(String title, List items,String clubLogo,String online) { 
    super(title, items); 

    this.clubLogo = clubLogo; 
    this.online = online; 
} 

public String getClubLogo() { 
    return clubLogo; 
} 

public void setClubLogo(String clubLogo) { 
    this.clubLogo = clubLogo; 
} 

public String getOnline() { 
    return online; 
} 

public void setOnline(String online) { 
    this.online = online; 
} 


} 

アダプタ

public class ClubNameChatAdapter extends ExpandableRecyclerViewAdapter<ClubNameViewHolder, UsersChatViewHolder> { 
Context ctx; 

public ClubNameChatAdapter(List<? extends ExpandableGroup> groups, Context ctx) { 
    super(groups); 
    this.ctx = ctx; 
} 

@Override 
public ClubNameViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.parent_layout_users, parent, false); 

    return new ClubNameViewHolder(view); 
} 

@Override 
public UsersChatViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_layout_users, parent, false); 

    return new UsersChatViewHolder(view); 
} 

@Override 
public void onBindChildViewHolder(UsersChatViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) { 

    UserChat userChat = (UserChat) group.getItems().get(childIndex); 
    holder.setUsername(userChat.getUsername()); 
    holder.setFlag(ctx, userChat.getFlag()); 
    holder.setProfileImage(ctx, userChat.getProfileImage()); 
    holder.setOnlineImage(); 

} 

@Override 
public void onBindGroupViewHolder(ClubNameViewHolder holder, int flatPosition,  ExpandableGroup group) { 
    holder.setClubTitle(group); 
} 
} 

断片

public class FragmentChatUsers extends Fragment { 

RecyclerView userRecylerView; 
ClubNameChatAdapter adapter; 
List<UserChatGroup> clubName; 
DatabaseReference userReference; 
String profileImage; 
String username; 
String flag; 
String clubNameLogo; 
boolean isOnline; 
int numberOnline; 
String online; 


public void getClubName() { 
    userReference = FirebaseDatabase.getInstance().getReference().child("UsersChat"); 

    userReference.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      clubName = new ArrayList<UserChatGroup>(); 
      for (final DataSnapshot snapshot : dataSnapshot.getChildren()){ 


       final String clubNameString = snapshot.getKey().toString(); 

       DatabaseReference chatReference = FirebaseDatabase.getInstance().getReference().child("UsersChat").child(clubNameString); 
       chatReference.addValueEventListener(new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 
         final List<UserChat> userChats = new ArrayList<UserChat>(); 
         numberOnline = 0; 
         for (DataSnapshot snapshot1 : dataSnapshot.getChildren()) { 

          username = String.valueOf(snapshot1.child("username").getValue()); 
          profileImage = String.valueOf(snapshot1.child("profileImage").getValue()); 
          flag = String.valueOf(snapshot1.child("flag").getValue()); 
          clubNameLogo = String.valueOf(snapshot1.child("favoriteClubLogo").getValue()); 
          isOnline = (boolean) snapshot1.child("online").getValue(); 

          if (isOnline){ 
           numberOnline++; 
          } 

          online = String.valueOf(numberOnline); 



          userChats.add(new UserChat(username, flag, profileImage)); 
         } 
          clubName.add(new UserChatGroup(clubNameString, userChats, clubNameLogo, online)); 


         adapter = new ClubNameChatAdapter(clubName, getContext()); 
         userRecylerView.setLayoutManager(new LinearLayoutManager(getContext())); 
         userRecylerView.setAdapter(adapter); 
         adapter.notifyDataSetChanged(); 
        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) { 

        } 
       }); 



      } 

     } 


     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 
} 

@Override 
public void onStart() { 
    super.onStart(); 

    DatabaseReference setOfline = FirebaseDatabase.getInstance().getReference().child("UsersChat").child(MainPage.myClubName).child(MainPage.uid); 
    setOfline.child("online").setValue(true); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 

    DatabaseReference setOfline = FirebaseDatabase.getInstance().getReference().child("UsersChat").child(MainPage.myClubName).child(MainPage.uid); 
    setOfline.child("online").setValue(false); 
} 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_chat_users,container,false); 

    userRecylerView = (RecyclerView) view.findViewById(R.id.chatUserRecView); 
    getClubName(); 

    return view; 
} 
} 

答えて

0

問題解決、addListenerForSingleValueEvent

でaddValueEventListenerを変更
関連する問題