SwipeRefreshLayout控件介绍

1. 简介


SwipeRefreshLayout是一种官方提供的下拉刷新的效果,在support v4包中,android.support.v4.widget.SwipeRefreshLayout,Support Library必须是19.1以上,没有上拉加载更多功能,里边只能包裹一个子控件,可以是任意子控件,ListView、RecyclerView、ScrollView都是可以的,swipeRefresh.setOnRefreshListener这个是监听下拉刷新的回调,至于加载更多可以监听是否滚动到底部,监听它滚动到底部自动加载

2. 监听是否滚动到顶部


/**
     * @return Whether it is possible for the child view of this layout to
     *         scroll up. Override this if the child view is a custom view.
     *
     *  可以判断ListView、RecyclerView、ScrollView
     *         这个方法是SwipeRefreshLayout中的源码:用于判断该View是否滚动到了最顶部,还能不能向上滚动
     */
    public boolean canChildScrollUp() {
        if (android.os.Build.VERSION.SDK_INT < 14) {
            if (mDragListView instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) mDragListView;
                return absListView.getChildCount() > 0
                        && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
                        .getTop() < absListView.getPaddingTop());
            } else {
                return ViewCompat.canScrollVertically(mDragListView, -1) || mDragListView.getScrollY() > 0;
            }
        } else {
            return ViewCompat.canScrollVertically(mDragListView, -1);
        }
    }

3.监听是否滚动到底部

/**
     * @return Whether it is possible for the child view of this layout to
     *         scroll up. Override this if the child view is a custom view.
     *
     *  可以判断ListView、RecyclerView、ScrollView
     *         这个方法是SwipeRefreshLayout中的源码:用于判断该View是否滚动到了最顶部,还能不能向上滚动
     */
    public boolean canChildScrollUp() {
        if (android.os.Build.VERSION.SDK_INT < 14) {
            if (mDragListView instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) mDragListView;
                return absListView.getChildCount() > 0
                        && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
                        .getTop() < absListView.getPaddingTop());
            } else {
                return ViewCompat.canScrollVertically(mDragListView, -1) || mDragListView.getScrollY() > 0;
            }
        } else {
            return ViewCompat.canScrollVertically(mDragListView, -1);
        }
    }
  1. 示例代码如下:
/**
 * @email :  [email protected]
 * @date :2017/12/23
 * @author : Jack-Chen
 * @Description: SwipeRefreshLayout
 *
 */

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{

    private SwipeRefreshLayout swipeLayout;
    private ListView listview;

    private boolean isRefresh = false ; //默认是不刷新的
    private List datas;
    private LVAdapter mLvAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView() ;
        initData() ;

    }

    private void initData() {
        datas = new ArrayList();
        for (int i = 0; i < 10; i++) {
            datas.add("这个第" + i + "个数据") ;
        }

        mLvAdapter = new LVAdapter(this , datas);
        listview.setAdapter(mLvAdapter);

    }

    public void initView() {
        swipeLayout = (SwipeRefreshLayout)  findViewById(R.id.swipeLayout);
        listview = (ListView) findViewById(R.id.listview);

        //设置进度条的颜色主题,最多能设置四种,加载颜色是循环播放的,只要没完成刷新就会一直循环
        swipeLayout.setColorSchemeColors(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);


        //上边方法已经废弃,但是用这个方法的话,进度条的圆圈是没有颜色的
        swipeLayout.setColorSchemeColors(Color.BLUE,
                Color.GREEN,
                Color.YELLOW,
                Color.RED);

        // 设置手指在屏幕下边 下拉多少距离会触发下拉刷新
        swipeLayout.setDistanceToTriggerSync(350);
        // 设置下拉圆圈的背景
        swipeLayout.setProgressBackgroundColorSchemeColor(Color.RED);
        // 设置下拉圆圈的大小
        swipeLayout.setSize(SwipeRefreshLayout.LARGE);

        // 设置下拉刷新的监听
        swipeLayout.setOnRefreshListener(this);

    }

    @Override
    public void onRefresh() {
        if (isRefresh){   //表示没有正在刷新
            return;
        }else{
            isRefresh = true ; //表示正在刷新
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    swipeLayout.setRefreshing(false);

                    //刷新时候清除数据,然后重新请求所有数据 并添加到集合
                    //下边是自己修改的
                    datas.clear();
                    for (int i = 1; i < 10; i++) {
                        datas.add("这是第" + i + "个数据");
                    }

                    mLvAdapter.notifyDataSetChanged();
                    isRefresh = false ;
                }
            } , 3000) ;
        }
    }
}

LVAdapter代码如下:

/**
 * @email :  [email protected]
 * @date :2017/12/23
 * @author : Jack-Chen
 * @Description: SwipeRefreshLayout
 *
 */
public class LVAdapter extends BaseAdapter {

    private Context mContext ;
    private List mDatas ;

    public LVAdapter(Context context , List datas){
        this.mContext = context ;
        this.mDatas = datas ;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv , null) ;

            holder = new ViewHolder() ;

            holder.text = (TextView)convertView.findViewById(R.id.text) ;

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(mDatas.get(position));
        return convertView;
    }

    public class ViewHolder{
        TextView text ;
    }
}

item_lv布局文件如下:





    

        

        
    

具体代码已上传至github
https://github.com/shuai999/SwipeRefreshLayoutDemo.git

你可能感兴趣的:(SwipeRefreshLayout控件介绍)