首页 > 代码库 > ListView ViewPager ScrollView 修改边界色

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 下载地址

ListView ViewPager ScrollView 修改边界色