RecyclerView blinking after notifyDatasetChanged()

前端 未结 18 1667
悲&欢浪女
悲&欢浪女 2020-12-07 16:20

I have a RecyclerView which loads some data from API, includes an image url and some data, and I use networkImageView to lazy load image.

@Override
public vo         


        
相关标签:
18条回答
  • 2020-12-07 16:46

    Try using stable IDs in your RecyclerView.Adapter

    setHasStableIds(true) and override getItemId(int position).

    Without stable IDs, after notifyDataSetChanged(), ViewHolders usually assigned to not to same positions. That was the reason of blinking in my case.

    You can find a good explanation here.

    0 讨论(0)
  • 2020-12-07 16:47

    Assuming mItems is the collection that backs your Adapter, why are you removing everything and re-adding? You are basically telling it that everything has changed, so RecyclerView rebinds all views than I assume the Image library does not handle it properly where it still resets the View even though it is the same image url. Maybe they had some baked in solution for AdapterView so that it works fine in GridView.

    Instead of calling notifyDataSetChanged which will cause re-binding all views, call granular notify events (notify added/removed/moved/updated) so that RecyclerView will rebind only necessary views and nothing will flicker.

    0 讨论(0)
  • 2020-12-07 16:47

    In my case there was a much simpler problem, but it can look/feel very much like the problem above. I had converted an ExpandableListView to a RecylerView with Groupie (using Groupie's ExpandableGroup feature). My initial layout had a section like this:

    <androidx.recyclerview.widget.RecyclerView
      android:id="@+id/hint_list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@android:color/white" />
    

    With layout_height set to "wrap_content" the animation from expanded group to collapsed group felt like it would flash, but it was really just animating from the "wrong" position (even after trying most of the recommendations in this thread).

    Anyway, simply changing layout_height to match_parent like this fixed the problem.

    <androidx.recyclerview.widget.RecyclerView
      android:id="@+id/hint_list"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@android:color/white" />
    
    0 讨论(0)
  • 2020-12-07 16:48

    I had similar issue and this worked for me You can call this method to set size for image cache

    private int getCacheSize(Context context) {
    
        final DisplayMetrics displayMetrics = context.getResources().
                getDisplayMetrics();
        final int screenWidth = displayMetrics.widthPixels;
        final int screenHeight = displayMetrics.heightPixels;
        // 4 bytes per pixel
        final int screenBytes = screenWidth * screenHeight * 4;
    
        return screenBytes * 3;
    }
    
    0 讨论(0)
  • 2020-12-07 16:49

    for me recyclerView.setHasFixedSize(true); worked

    0 讨论(0)
  • 2020-12-07 16:52

    try this to disable the default animation

    ItemAnimator animator = recyclerView.getItemAnimator();
    
    if (animator instanceof SimpleItemAnimator) {
      ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
    }
    

    this the new way to disable the animation since android support 23

    this old way will work for older version of the support library

    recyclerView.getItemAnimator().setSupportsChangeAnimations(false)
    
    0 讨论(0)
提交回复
热议问题