ListView ViewPager ScrollView 修改边界色

如果觉得android中默认的listview 滑动到上面或者底部的阴影色不好看,可以这么定义

先看效果图


public class ListView extends android.widget.ListView {

    private OnScrollListener mLegacyOnScrollListener;
    private final MulticastOnScrollListener mMulticastOnScrollListener = new MulticastOnScrollListener();

    public ListView(Context context) {
        this(context, null);
    }

    public ListView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.listViewStyle);
    }

    public ListView(Context context, AttributeSet attrs, int defStyle) {
        super(new ContextWrapperEdgeEffect(context), attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        int color = context.getResources().getColor(R.color.default_edgeeffect_color);

        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EdgeEffectView, defStyle, 0);
            color = a.getColor(R.styleable.EdgeEffectView_edgeeffect_color, color);
            a.recycle();
        }
        setEdgeEffectColor(color);
    }

    public void setOnScrollListener(OnScrollListener listener) {
        if (listener != null) {
            checkPrecondition(mLegacyOnScrollListener == null);
            mLegacyOnScrollListener = listener;
            addOnScrollListener(mLegacyOnScrollListener);
        } else if (mLegacyOnScrollListener != null) {
            removeOnScrollListener(mLegacyOnScrollListener);
            mLegacyOnScrollListener = null;
        }
    }

    public void setEdgeEffectColor(int edgeEffectColor) {
        ((ContextWrapperEdgeEffect) getContext()).setEdgeEffectColor(edgeEffectColor);
    }

    public void addOnScrollListener(OnScrollListener listener) {
        mMulticastOnScrollListener.add(listener);
    }

    public void clearOnScrollListeners() {
        mMulticastOnScrollListener.clear();
    }

    public void removeOnScrollListener(OnScrollListener listener) {
        mMulticastOnScrollListener.remove(listener);
    }

    void checkPrecondition(boolean state) {
        if (!state) {
            throw new IllegalStateException();
        }
    }
}
在xml中直接使用就可以

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ListViewActivity">

    <uk.co.androidalliance.edgeeffectoverride.ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:edgeeffect_color="@color/green"/>


</RelativeLayout>
自定义属性文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="EdgeEffectView">
        <attr name="edgeeffect_color" format="color"/>
    </declare-styleable>
</resources>

Viewpager scrollview等类似

参考Demo 下载地址

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值