Method for shielding RecyclerView unilateral sliding to head shadow of fadingEdge

  • 2021-09-05 00:54:40
  • OfStack

Preface

RecyclerView is an enhanced version of ListView, which not only achieves the same effect as ListView, but also optimizes various shortcomings in ListView

ResyslerView can achieve horizontal scrolling, which ListView can't achieve

At present, RecyclerView is more recommended by the government.

Scenario:

Because under some product requirements, you want RecyclerView to show a head shadow when it slides to the bottom, but you don't want fadingEdge at the top because it is a drop-down refresh control at the top.

Practice:

By reading the source code implementation of RecyclerView, we found that there is no exposed method that can be called or overloaded, so we use reflection to implement it.

The code is as follows:


 mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
   @Override
   public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    EdgeEffectCompat mTopGlow = null;
    try {
     Field topGlow = mRecyclerView.getClass().getDeclaredField("mTopGlow");

     if (topGlow != null) {
      topGlow.setAccessible(true);
      mTopGlow = (EdgeEffectCompat) topGlow.get(mRecyclerView);
     }

    } catch (Exception e) {
     e.printStackTrace();
    }

    if (mTopGlow != null) {
     mTopGlow.setSize(0, 0);
     mTopGlow.finish();
    }
   }
  });

If there are other better solutions, please discuss them.

Summarize


Related articles: