欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SwipeRefreshLayout

程序员文章站 2022-03-14 15:53:08
...

简介

官方下刷组件(无上加)

方法

进度条色(默黑)

  • 参数int colorResIds

    loadRefresh.setColorSchemeResources(R.color.colorPrimary, R.color.warning_color, R.color.success_sign);
  • 参数int colors

    loadRefresh.setColorSchemeColors(ContextCompat.getColor(GdActivity.this,R.color.colorPrimary));

圆背景色(默白)

  • 参数int colorRes

    loadRefresh.setProgressBackgroundColorSchemeResource(R.color.colorPrimary);
  • 参数int color

    loadRefresh.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(GdActivity.this, R.color.colorPrimary));

透明度

所包ListView/ScrollView/RecyclerView及刷新圈透明度。

loadRefresh.setAlpha(xxx);

刷新

loadRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
     @Override
     public void onRefresh() {
         toastShort("刷新");
     }
});

可刷否

loadRefresh.setRefreshing(boolean xxx);

禁/开

loadRefresh.setEnabled(boolean xxx);

定制

下刷上加

package widget;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.ListView;

import com.self.zsp.rd.R;

/**
 * Created on 2018/1/26.
 *
 * @desc load refresh
 */

public class LoadRefresh extends SwipeRefreshLayout implements AbsListView.OnScrollListener {
    private ListView listView;
    private OnLoadListener onLoadListener;
    private View footer;
    /**
     * 滑至最底上拉
     */
    private int touchSlop;
    /**
     * 按下y坐标
     */
    private int yDown;
    /**
     * 抬起y坐标
     */
    private int yUp;
    /**
     * 加状
     */
    public boolean isLoading;
    /**
     * 禁/开
     */
    public boolean enable = true;

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

    public LoadRefresh(Context context, AttributeSet attrs) {
        super(context, attrs);
        touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        footer = LayoutInflater.from(context).inflate(R.layout.lv_footer, null, false);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (listView == null) {
            getListView();
        }
    }

    private void getListView() {
        int childCount = getChildCount();
        if (childCount > 0) {
            View childView = getChildAt(0);
            if (childView instanceof ListView) {
                listView = (ListView) childView;
                listView.setOnScrollListener(this);
            }
        }
    }

    /**
     * (non-Javadoc)
     *
     * @param event
     * @return
     * @see android.view.ViewGroup#dispatchTouchEvent(android.view.MotionEvent)
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        switch (action) {
            // 按下
            case MotionEvent.ACTION_DOWN:
                yDown = (int) event.getRawY();
                break;
            // 移动
            case MotionEvent.ACTION_MOVE:
                yUp = (int) event.getRawY();
                break;
            // 抬起
            case MotionEvent.ACTION_UP:
                if (canLoad()) {
                    loadData();
                }
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    /**
     * 可加 最底 无加 上拉
     *
     * @return
     */
    private boolean canLoad() {
        return enable && isBottom() && !isLoading && isPullUp();
    }

    /**
     * 最底
     */
    private boolean isBottom() {
        if (listView != null && listView.getAdapter() != null) {
            int index = listView.getAdapter().getCount() - 1;
            if (listView.getLastVisiblePosition() == index) {
                View lVIV = listView.getChildAt(index - listView.getFirstVisiblePosition());
                return (lVIV != null) && (lVIV.getBottom() == listView.getHeight());
            }
        }
        return false;
    }

    /**
     * 上拉
     *
     * @return
     */
    private boolean isPullUp() {
        return (yDown - yUp) >= touchSlop;
    }

    private void loadData() {
        if (onLoadListener != null) {
            setLoading(true);
            onLoadListener.onLoad();
        }
    }

    public void setLoading(boolean loading) {
        isLoading = loading;
        if (isLoading) {
            listView.addFooterView(footer);
        } else {
            listView.removeFooterView(footer);
            yDown = 0;
            yUp = 0;
        }
    }

    public void setOnLoadListener(OnLoadListener loadListener) {
        onLoadListener = loadListener;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (canLoad()) {
            loadData();
        }
    }

    public interface OnLoadListener {
        /**
         * 加载
         */
        void onLoad();
    }
}

isBottom()可如下,即View lVIV = listView.getChildAt(index - listView.getFirstVisiblePosition());View lVIV = listView.getChildAt(listView.getChildCount() - 1);。此时底部,listView.getChildCount()全item项(否滑程见item项),故二均可。否只能View lVIV = listView.getChildAt(index - listView.getFirstVisiblePosition());

/**
 * 最底
 */
private boolean isBottom() {
    if (listView != null && listView.getAdapter() != null) {
        int index = listView.getAdapter().getCount() - 1;
        if (listView.getLastVisiblePosition() == index) {
            View lVIV = listView.getChildAt(listView.getChildCount() - 1);
            return (lVIV != null) && (lVIV.getBottom() == listView.getHeight());
        }
    }
    return false;
}

方法

  • 禁/开

    loadRefresh.enable = xxx;
  • 加状

    if (loadRefresh.isLoading) {
        // 执行
    }

使用

布局

<widget.LoadRefresh
    android:id="@+id/loadRefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background"
        android:divider="@null"
        android:overScrollMode="never"
        android:scrollbars="none" />
</widget.LoadRefresh>

主代码

// loadRefresh
loadRefresh.setColorSchemeResources(R.color.colorPrimary, R.color.warning_color, R.color.success_sign);
loadRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        toastShort("刷新");
    }
});
loadRefresh.setOnLoadListener(new LoadRefresh.OnLoadListener() {
    @Override
    public void onLoad() {
        toastShort("加载");
    }
});
相关标签: color