XRecyclerView上下拉使用

1.配置build.gradle文件:

直接添加

       compile 'com.jcodecraeer:xrecyclerview:1.3.2'   即可。
 
  
 
  
    2.在xml文件的使用
 
  
 
  
xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_xreca"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>
    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:layout_width="match_parent"
        android:id="@+id/xre_xrv"
        android:layout_height="match_parent">


    com.jcodecraeer.xrecyclerview.XRecyclerView>

RelativeLayout>
 
在xml中的使用只需要配置它的id,长宽就好了。
 
  
 
  
    
      3.在JAVJ文件中的使用
 
  
public class XrecyclerviewActivity extends AppCompatActivity {
    private XRecyclerView recyclerView;
    private Context context;
    //数据集合
    private Listlist=new ArrayList<>();
    //获取数据的开始
    private int curr;
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xreca);
        getSupportActionBar().hide();
        context=this;
        recyclerView=(XRecyclerView)findViewById(R.id.xre_xrv);


        //LinearLayoutManager是线性布局,setOrientation可以设置他的方向是横向还是纵向。
        LinearLayoutManager layoutManager=new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        //GridLayoutManager是表格布局,GridLayoutManagerContext,表格的个数(根据方向决定横排几个或纵排几个))
//        GridLayoutManager layoutManager1=new GridLayoutManager(context,3);
//        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        recyclerView.setLayoutManager(layoutManager1);


        //StaggeredGridLayoutManager是瀑布流,StaggeredGridLayoutManager(横或纵排个数,方向)
//        StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
//        recyclerView.setLayoutManager(staggeredGridLayoutManager);


        //XRecyclerView的上下拉监听方法
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            //下拉刷新
            public void onRefresh() {
                //当下拉刷新的时候,重新获取数据,所有curr要变回0,并且把集合list清空
                curr=0;
                list.clear();
                getData(curr);
                recyclerView.refreshComplete();
            }

            @Override
            //上拉加载
            public void onLoadMore() {
                //当上拉加载的时候,因为一次获取是10个数据,所也在获取的时候就要在加10的地方开始获取
//                如:第一次0——9//                    第二次10——19//                SystemClock.sleep(1000);
                curr=curr+10;
                getData(curr);
                recyclerView.refreshComplete();
            }
        });

    //第一次获取数据
        curr=0;
        getData(curr);


    }


    

    private void getData(int number){
        for (int i=number;i10;i++){
            list.add("数据是第"+i+"");
        }

        //调用Adapter展示数据,这个判断是为了不重复创建MyAdapter的对象
        if (adapter==null){
            adapter=new MyAdapter(list,context);
            recyclerView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }

    }

    private  class MyAdapter extends RecyclerView.Adapter{
        private Listlist=new ArrayList<>();
        private Context context;

        public MyAdapter(List list, Context context) {
            this.list = list;
            this.context = context;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            //Adapter添加布局,bq把这个view传递给HoldView,让HoldView找到空间
            View view= LayoutInflater.from(context).inflate(R.layout.xrecyc_adapter, parent,false);
            HoldView holdView=new HoldView(view);
            return holdView;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            //positionAdapter的位置,数据从list里面可以拿出来。
            String s=list.get(position);
            ((HoldView)holder).textView.setText(s);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
        private class HoldView extends RecyclerView.ViewHolder{
            private TextView textView;
            public HoldView(View itemView) {
                super(itemView);
                //根据onCreateViewHolderHoldView所添加的xml布局找到空间
                textView= (TextView) itemView.findViewById(R.id.xrecyc_text);
            }
        }
    }
}

你可能感兴趣的:(Andriod开发)