SwipeRefreshLayout+RecyclerView 上拉下刷

XLM布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#ffffff"
              android:orientation="vertical">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

MainActivity:

package ;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kczd.design_android.R;
import com.kczd.design_android.data.EvaluateData;
import com.kczd.design_android.view.adapter.OrderEvaluateAdapter;

/**
 * 
 * Created by Administrator on 2017/9/7.
 */

public class OrderEvaluateFragment extends Fragment implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;
    private OrderEvaluateAdapter adapter;
    private static final int TOTAL_COUNTER = 18;
    private static final int PAGE_SIZE = 6;
    private int delayMillis = 1000;
    private int mCurrentCounter = 0;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;
    private View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.order_evaluate_fragment, null);
        initView();
        return view;
    }

    private void initView() {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_list);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeLayout);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        //设置下拉出现小圆圈是否是缩放出现,出现的位置,最大的下拉位置
        mSwipeRefreshLayout.setProgressViewOffset(true, 50, 200);

        //设置下拉圆圈的大小,两个值 LARGE, DEFAULT
        mSwipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);

        // 设置下拉圆圈上的颜色,蓝色、绿色、橙色、红色
        mSwipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        // 设定下拉圆圈的背景
        mSwipeRefreshLayout.setProgressBackgroundColor(R.color.white);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        initAdapter();
    }

    private void initAdapter() {
        adapter = new OrderEvaluateAdapter();
        adapter.setOnLoadMoreListener(this, mRecyclerView);
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        //        pullToRefreshAdapter.setPreLoadNumber(3);
        mRecyclerView.setAdapter(adapter);
        mCurrentCounter = adapter.getData().size();
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
                Toast.makeText(getContext(), Integer.toString(position), Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void onLoadMoreRequested() {
        mSwipeRefreshLayout.setEnabled(false);
        if (adapter.getData().size() < PAGE_SIZE) {
            adapter.loadMoreEnd(true);
        } else {
            if (mCurrentCounter >= TOTAL_COUNTER) {
                //                    pullToRefreshAdapter.loadMoreEnd();//default visible
                adapter.loadMoreEnd(mLoadMoreEndGone);//true is gone,false is visible
            } else {
                if (isErr) {
                    adapter.addData(EvaluateData.getSampleData(PAGE_SIZE));
                    mCurrentCounter = adapter.getData().size();
                    adapter.loadMoreComplete();
                } else {
                    isErr = true;
                    Toast.makeText(getContext(), "点击更多", Toast.LENGTH_LONG).show();
                    adapter.loadMoreFail();

                }
            }
            mSwipeRefreshLayout.setEnabled(true);
        }
    }

    @Override
    public void onRefresh() {
        adapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                adapter.setNewData(EvaluateData.getSampleData(PAGE_SIZE));
                isErr = false;
                mCurrentCounter = PAGE_SIZE;
                mSwipeRefreshLayout.setRefreshing(false);
                adapter.setEnableLoadMore(true);
            }
        }, delayMillis);
    }
}

Adapter:

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kczd.design_android.R;
import com.kczd.design_android.bean.EvaluateBean;
import com.kczd.design_android.data.EvaluateData;

/**
 * Created by Administrator on 2017/9/11.
 */

public class OrderEvaluateAdapter extends BaseQuickAdapter<EvaluateBean,BaseViewHolder> {
    public OrderEvaluateAdapter() {
        super(R.layout.item_evaluate, EvaluateData.getSampleData(10));
    }

    @Override
    protected void convert(BaseViewHolder helper, EvaluateBean item) {
        helper.setText(R.id.tv_title, item.getTitle()).setText(R.id.tv_weight, item.getWeight())
                .setText(R.id.tv_price, item.getPrice()).setText(R.id.tv_order_time, item.getOrder_tiem())
                .setText(R.id.tv_individual, item.getIndividual()).setText(R.id.tv_total, item.getTotal());
    }
}

item布局:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
                                    xmlns:card_view="http://schemas.android.com/apk/res-auto"
                                    android:id="@+id/card_view"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center"
                                    android:layout_marginLeft="5dp"
                                    android:layout_marginRight="5dp"
                                    android:foreground="?android:attr/selectableItemBackground"
                                    card_view:cardBackgroundColor="#FFFFFF"
                                    card_view:cardCornerRadius="4dp"
                                    card_view:cardUseCompatPadding="true">


    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@color/alpha_05_black"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:orientation="vertical">

            <TextView

                android:id="@+id/tv_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="标题"
                android:textColor="#000"
                android:textSize="19sp"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="重量: "/>

                <TextView
                    android:id="@+id/tv_weight"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="10Kg"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="价格: "/>

                <TextView
                    android:id="@+id/tv_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0.40"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="下单时间: "/>

                <TextView
                    android:id="@+id/tv_order_time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2017-9-9"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_lower"
        android:layout_width="match_parent"
        android:layout_height="40dp"

        android:layout_marginLeft="10dp"
        android:layout_marginTop="110dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="共"/>

        <TextView
            android:id="@+id/tv_individual"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="5"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="个"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="30dp"
            android:text="合计:"
            android:textColor="#000"/>

        <TextView
            android:id="@+id/tv_total"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="100.00"/>

        <TextView
            android:layout_width="70dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginLeft="30dp"
            android:background="@drawable/logistics_shape"
            android:gravity="center"
            android:text="取消订单"/>

        <TextView
            android:layout_width="70dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginLeft="30dp"
            android:background="@drawable/deliver_shape"
            android:gravity="center"
            android:text="我要支付"
            android:textColor="#ea783f"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

EvaluateData:

public class EvaluateData {
    public static List<EvaluateBean> getSampleData(int lenth){
        List<EvaluateBean> list=new ArrayList<>();
        for (int i = 0; i <lenth ; i++) {
            EvaluateBean  bean =new EvaluateBean();
            bean.setIv_image("1");
            bean.setTitle("标题");
            bean.setWeight("10kg");
            bean.setPrice("100");
            bean.setOrder_tiem("2017-9-9");
            bean.setIndividual("3");
            bean.setTotal("150");
            list.add(bean);
        }
        return list;
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值