https://github.com/nuptboyzhb/SuperSwipeRefreshLayout

封装

由于此类调用的代码比较多,所以要做一下封装。

定义封装类: RefreshHandler.java

/**
 * 封装刷新和加载更多的操作
 * author:Created by ZhangChen on 2016/8/3 0003.
 * detail:
 */
public class RefreshHandler {
    private SuperSwipeRefreshLayout swipeRefreshLayout;

    // Header View
    private ProgressBar progressBar;
    private TextView textView;
    private ImageView imageView;

    // Footer View
    private ProgressBar footerProgressBar;
    private TextView footerTextView;
    private ImageView footerImageView;


    private OnActionListener onActionListener;


    public RefreshHandler(SuperSwipeRefreshLayout swipeRefreshLayout) {
        this.swipeRefreshLayout = swipeRefreshLayout;
        initSwipeRefreshLayout( this.swipeRefreshLayout);
    }

    private void initSwipeRefreshLayout(SuperSwipeRefreshLayout swipeRefreshLayout) {
        // init SuperSwipeRefreshLayout
        swipeRefreshLayout.setHeaderViewBackgroundColor(Color.parseColor("#ECEFF1"));
        swipeRefreshLayout.setFooterViewBackgroundColor(Color.parseColor("#ECEFF1"));
        swipeRefreshLayout.setHeaderView(createHeaderView());// add headerView
        swipeRefreshLayout.setFooterView(createFooterView());
        swipeRefreshLayout.setTargetScrollWithLayout(true);
        swipeRefreshLayout
                .setOnPullRefreshListener(new SuperSwipeRefreshLayout.OnPullRefreshListener() {

                    @Override
                    public void onRefresh() {
                        textView.setText("正在刷新");
                        imageView.setVisibility(View.GONE);
                        progressBar.setVisibility(View.VISIBLE);
                        if(onActionListener != null){
                            onActionListener.onRefresh();
                        }
                    }

                    @Override
                    public void onPullDistance(int distance) {
                        // pull distance
                    }

                    @Override
                    public void onPullEnable(boolean enable) {
                        textView.setText(enable ? "松开刷新" : "下拉刷新");
                        imageView.setVisibility(View.VISIBLE);
                        imageView.setRotation(enable ? 180 : 0);
                    }
                });

        swipeRefreshLayout
                .setOnPushLoadMoreListener(new SuperSwipeRefreshLayout.OnPushLoadMoreListener() {

                    @Override
                    public void onLoadMore() {
                        footerTextView.setText("正在加载...");
                        footerImageView.setVisibility(View.GONE);
                        footerProgressBar.setVisibility(View.VISIBLE);
                        if(onActionListener != null){
                            onActionListener.onLoadMore();
                        }
                    }

                    @Override
                    public void onPushEnable(boolean enable) {
                        footerTextView.setText(enable ? "松开加载" : "上拉加载");
                        footerImageView.setVisibility(View.VISIBLE);
                        footerImageView.setRotation(enable ? 0 : 180);
                    }

                    @Override
                    public void onPushDistance(int distance) {

                    }

                });
    }



    private View createFooterView() {
        View footerView = LayoutInflater.from(swipeRefreshLayout.getContext())
                .inflate(R.layout.layout_footer, null);
        footerProgressBar = (ProgressBar) footerView
                .findViewById(R.id.footer_pb_view);
        footerImageView = (ImageView) footerView
                .findViewById(R.id.footer_image_view);
        footerTextView = (TextView) footerView
                .findViewById(R.id.footer_text_view);
        footerProgressBar.setVisibility(View.GONE);
        footerImageView.setVisibility(View.VISIBLE);
        footerImageView.setImageResource(R.drawable.down_arrows_footer);
        footerTextView.setText("上拉加载更多...");
        return footerView;
    }

    private View createHeaderView() {
        View headerView = LayoutInflater.from(swipeRefreshLayout.getContext())
                .inflate(R.layout.layout_head, null);
        progressBar = (ProgressBar) headerView.findViewById(R.id.pb_view);
        textView = (TextView) headerView.findViewById(R.id.text_view);
        textView.setText("下拉刷新");
        imageView = (ImageView) headerView.findViewById(R.id.image_view);
        imageView.setVisibility(View.VISIBLE);
        imageView.setImageResource(R.drawable.down_arrows_footer);
        progressBar.setVisibility(View.GONE);
        return headerView;
    }


    public void finishRefresh(){
        swipeRefreshLayout.setRefreshing(false);
        progressBar.setVisibility(View.GONE);
    }

    public void finishLoadMore(){
        footerImageView.setVisibility(View.VISIBLE);
        footerProgressBar.setVisibility(View.GONE);
        swipeRefreshLayout.setLoadMore(false);
    }


    public interface  OnActionListener {
        void onRefresh();
        void onLoadMore();
    }


    public void setOnActionListener(OnActionListener onActionListener){
        this.onActionListener = onActionListener;
    }
}

调用封装类

创建RefreshHandler refreshHandler = new RefreshHandler(swipeRefreshLayout); refreshHandler.setOnActionListener(this); 然后在合适的时候调用 refreshHandler.finishLoadMore(); refreshHandler.finishRefresh();


0 条评论

发表回复

您的电子邮箱地址不会被公开。