FirebaseRecyclerAdapter with two different database links - negative scroll effect

enter image description here

The simple thing I would like to do (see picture)

Display a view with information coming from two different places in Firebase so that it works professionally by scrolling UP and DOWN

I have a list of films, and for each of them I would like the user to specify a rating and watch it

In the database, I created 2 structures to have a list of films on one side and ratings for each user on the other

Problem Using FirebaseRecyclerAdapter

, , , (), ( ), , () , . FirebaseRecyclerView?

viewHolders recycleView reset populateView() , . , , (. SetOnlistener populateView()

populateView , populateView() ( UP DOWN ).

/ ?

, ? ? , populateView(), ?

, :

  • viewHolders ?
  • RecyclerView? parseSnapshot(), ...
  • , ( , , ).
  • , , firebase ( ) : " ".

FirebaseRecyclerAdapter

 @Override
protected void populateViewHolder(final MovieViewHolder viewHolder, final     Movie movie, final int position) {

    String movieId = this.getRef(position).getKey();

    // Oblidged to show no rating at the beginning because otherwise
    // if a viewHolder is reused it has the values from another movie
    viewHolder.showNoRating();

    //---------------------------------------------
    // Set values in the viewHolder from the model 
    //---------------------------------------------
    viewHolder.movieTitle.setText(movie.getTitle());
    viewHolder.movieDescription.setText(movie.getDescription());

    //-----------------------------------------------------
    // Ratings info are in another DB location... get them
    // but call is asynchronous so PROBLEM when SCROLLING!
    //-----------------------------------------------------
    DatabaseReference ratingMovieRef = mDbRef.child(Constants.FIREBASE_LOCATION_RATINGS).child(currentUserId).child(movieId);
 ratingQuoteRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            RatingMovie ratingMovie = dataSnapshot.getValue(RatingMovie.class);
            Rating rating = Rating.NO_RATING;
            if (ratingMovie != null) {
                rating = Rating.valueOf(ratingMovie.getRating());
            }

            // Set the rating in the viewholder (through anhelper method)
            viewHolder.showActiveRating(rating);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

MovieViewHolder

public class QuoteViewHolder extends RecyclerView.ViewHolder {

public CardView cardView;
public TextView movieTitle;
public TextView movieDescription;
public ImageView ratingOneStar;
public ImageView ratingTwoStar;
public ImageView ratingThreeStar;

public QuoteViewHolder(View itemView) {

    super(itemView);
    movieTitle = (TextView)itemView.findViewById(R.id.movie_title);
    movieDescription = (TextView)itemView.findViewById(R.id.movie_descr);

    // rating
    ratingOneStar = (ImageView)itemView.findViewById(R.id.rating_one);
    ratingTwoStar = (ImageView)itemView.findViewById(R.id.rating_two);
    ratingThreeStar = (ImageView)itemView.findViewById(R.id.rating_three);
}

/**
* Helper to show the color on stars depending on rating value 
*/
public void showActiveRating(Rating rating){

    if (rating.equals(Rating.ONE)) {
        // just set the good color on ratingOneStar and the others
        ...
    }
    else if (rating.equals(Rating.TWO)) {
        // just set the good color
        ...
    } else if (rating.equals(Rating.THREE)) {
       // just set the good color
       ...
   }


/**
 * Initialize the rating icons to unselected.
 * Important because the view holder can be reused and if not initalised values from other moviecan be seen
 */
public void initialiseNoRating(){
 ratingOneStar.setColorFilter(ContextCompat.getColor(itemView.getContext(), R.color.light_grey)); 
    ratingTwoStar.setColorFilter(....
    ratingThreeStar.SetColorFilter(...
}
+4
1

- ChildEventListener. node . , RecyclerAdapter, , , , recyclerview, . , , / FirebaseUI .

-

private MovieRatingConnection ratingConnection;

   // inside onCreate

    ratingConnection = new MovieRatingConnection(userId, new MovieRatingConnection.RatingChangeListener() {
        @Override
        public void onRatingChanged(DataSnapshot dataSnapshot) {
            if (recyclerAdapter != null) {
                if (dataSnapshot != null) {
                    int index = recyclerAdapter.snapshots.getIndexForKey(dataSnapshot.getKey());
                    recyclerAdapter.notifyItemChanged(index);
                }
            }
        }
    });

    Query movieQuery = FirebaseDatabase.getInstance().getReference().child("Movies");
    recyclerAdapter = new FirebaseRecyclerAdapter(movieQuery...) {
        @Override
        public void populateViewHolder(RecyclerView.ViewHolder viewHolder, Object model, int position) {
            //...
            final String key = getRef(position).getKey();
            viewHolder.showActiveRating(ratingConnection.getRating(key));
        }
    };

MovieRatingConnection ,

public class MovieRatingConnection {

    private MovieRatingListener listener;

    public MovieRatingConnection(String userId, RatingChangeListener changeListener) {
        Query query = FirebaseDatabase.getInstance().getReference().child("MovieRatings").child(userId);
        listener = new MovieRatingListener(query, changeListener);
    }

    public Rating getRating(String key) {
        return listener.getRating(key);
    }

    public void cleanup() {
        if (listener != null) {
            listener.unregister();
        }
    }



    public static class MovieRatingListener implements ChildEventListener {

        public interface RatingChangeListener {
            public void onRatingChanged(DataSnapshot snapshot);

        }

        private Query query;
        private HashMap<String, Rating> ratingMap = new HashMap<>();
        private RatingChangeListener changeListener;


        public MovieRatingListener(Query query, RatingChangeListener changeListener) {
            this.query = query;
            this.changeListener = changeListener;
            query.addChildEventListener(this);

        }

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot != null) {
                ratingMap.put(dataSnapshot.getKey(), dataSnapshot.getValue(Rating.class));
                changeListener.onRatingChanged(dataSnapshot);
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot != null) {
                ratingMap.put(dataSnapshot.getKey(), dataSnapshot.getValue(Rating.class));
                changeListener.onRatingChanged(dataSnapshot);
            }
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            ratingMap.remove(dataSnapshot.getKey());
            changeListener.onRatingChanged(null);
        }

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

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

        public Rating getRating(String key) {
            if (ratingMap.get(key) != null) {
                return ratingMap.get(key);
            } else {
                return new Rating(); // default value/null object
            }
        }

        public void unregister() {
            query.removeEventListener(this);
        }
    }
}
+2

Source: https://habr.com/ru/post/1670949/


All Articles