Android recycleView頂部添加透明漸變

一、前言:

在做直播的時(shí)候,聊天頁(yè)面的頂部,要recyclerView的頂部增加半透明的效果,遇到了很多坑,記錄下來。
效果如下:


image.png

二、基礎(chǔ)知識(shí):

1. 基礎(chǔ)使用

requiresFadingEdge:屬性用來設(shè)置拉滾動(dòng)條時(shí) ,邊框漸變的方向。

  • none(邊框顏色不變),
  • horizontal(水平方向顏色變淡),
  • vertical(垂直方向顏色變淡)。

fadingEdgeLength:用來設(shè)置邊框漸變的長(zhǎng)度

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:requiresFadingEdge="vertical"
        android:fadingEdgeLength="40dp"/>
aaa.gif

我們可以看到頭部和尾部,都有半透明效果。

2. 只要頭部有半透明效果

重寫recycleView的類,重新設(shè)置返回值。

  • 1. getTopFadingEdgeStrength(); 重寫這個(gè)方法,設(shè)置返回值是0,去掉頂部陰影;
  • 2. getBottomFadingEdgeStrength(); 重寫這個(gè)方法,設(shè)置返回值是0,去底頂部陰影
  • 3. getLeftFadingEdgeStrength(); 重寫這個(gè)方法,設(shè)置返回值是0,去掉左部陰影
  • 4. getRightFadingEdgeStrength(); 重寫這個(gè)方法,設(shè)置返回值是0,去掉右部陰影
public class RecycleViewCustomer extends RecyclerView {
    public RecycleViewCustomer(@NonNull Context context) {
        super(context);
    }
    public RecycleViewCustomer(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    public RecycleViewCustomer(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    /**
     * 重寫這個(gè)方法,返回值是0,去掉頂部陰影
     *
     * @return
     */
    @Override
    protected float getTopFadingEdgeStrength() {
        return super.getTopFadingEdgeStrength();
    }
    /**
     * 重寫這個(gè)方法,返回值是0,去底頂部陰影
     *
     * @return
     */
    @Override
    protected float getBottomFadingEdgeStrength() {
        //return super.getBottomFadingEdgeStrength();
        return 0;
    }

    /**
     * 重寫這個(gè)方法,返回值是0,去左頂部陰影
     *
     * @return
     */
    @Override
    protected float getLeftFadingEdgeStrength() {
        return super.getLeftFadingEdgeStrength();
    }

    /**
     * 重寫這個(gè)方法,返回值是0,去底右部陰影
     *
     * @return
     */
    @Override
    protected float getRightFadingEdgeStrength() {
        return super.getRightFadingEdgeStrength();
    }
}

參考鏈接:http://m.itdecent.cn/p/78bf0f2e44d1

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容