[关闭]
@TryLoveCatch 2022-11-02T09:20:41.000000Z 字数 2855 阅读 435

Android知识体系之RecyclerView顶部增加渐变效果

Android知识体系 RecyclerView


需求

RecyclerView在往上滑动的时候,期望Item有一个渐变的效果

方案

  1. <androidx.recyclerview.widget.RecyclerView
  2. android:id="@+id/home_recycler_view"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_marginTop="@dimen/home_recycle_margin"
  6. android:layout_marginLeft="@dimen/home_recycle_margin"
  7. android:layout_marginRight="@dimen/home_recycle_margin"
  8. android:scrollbars="none"
  9. android:overScrollMode="never"
  10. android:fadingEdgeLength="@dimen/home_recycle_fading_edge_height"
  11. android:requiresFadingEdge="vertical"
  12. />

主要是fadingEdgeLength 和 requiresFadingEdge两个属性

问题

  1. <FrameLayout
  2. android:id="@+id/home_recycler_root"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_marginTop="@dimen/home_recycle_margin"
  6. android:layout_marginLeft="@dimen/home_recycle_margin"
  7. android:layout_marginRight="@dimen/home_recycle_margin"
  8. android:visibility="gone">
  9. <androidx.recyclerview.widget.RecyclerView
  10. android:id="@+id/home_recycler_view"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:scrollbars="none"
  14. android:overScrollMode="never"
  15. />
  16. <ImageView
  17. android:id="@+id/home_img_edge"
  18. android:layout_width="match_parent"
  19. android:layout_height="32dp"
  20. android:background="#ff0000"
  21. android:visibility="gone" />
  22. </FrameLayout>
  1. mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
  2. @Override
  3. public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
  4. super.onScrollStateChanged(recyclerView, newState);
  5. boolean isNotTop = recyclerView.canScrollVertically(-1);
  6. mImgEdge.setVisibility(isNotTop ? View.VISIBLE : View.GONE);
  7. }
  8. });

增加一个ImageView放在RecyclerView的顶部,然后在onScrollStateChanged的时候控制ImageView的显示和隐藏。

问题

在第一次更新RecycleView的时候,onScrollStateChanged并没有执行,导致ImageView没有显示出来,后续滑动才会显示出来

参考:https://blog.csdn.net/linyukun6422/article/details/52516022

使用ItemDecoration来实现

  1. private class MyItemDecoration extends RecyclerView.ItemDecoration {
  2. private Paint mPaint;
  3. private int mHeight;
  4. private int mColorStart;
  5. private int mColorEnd;
  6. public MyItemDecoration() {
  7. mPaint = new Paint();
  8. mHeight = getResources().getDimensionPixelSize(R.dimen.home_recycle_fading_edge_height);
  9. mColorStart = ContextCompat.getColor(getContext(), R.color.home_recycle_fading_edge_start_color);
  10. mColorEnd = ContextCompat.getColor(getContext(), R.color.home_recycle_fading_edge_end_color);
  11. }
  12. @Override
  13. public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
  14. super.onDrawOver(c, parent, state);
  15. boolean isNotTop = parent.canScrollVertically(-1);
  16. if (isNotTop) {
  17. LinearGradient gradient = new LinearGradient(0, 0, 0, mHeight,
  18. new int[] {mColorStart, mColorEnd},
  19. new float[]{0f, 1.0f},
  20. Shader.TileMode.CLAMP);
  21. mPaint.setShader(gradient);
  22. c.drawRect(0, 0, parent.getWidth(), mHeight, mPaint);
  23. }
  24. }
  25. }

参考

自定义控件三部曲视图篇(五)——RecyclerView系列之二ItemDecoration
ItemDecoration介绍以及应用

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注