RecyclerView的下拉刷新,上拉加载更多

导入依赖

compile 'com.android.support:recyclerview-v7:26.1.0'

MainActivity.xml的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.recyclerview.MainActivity">


    <android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/refreshLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/Recycler_View"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


    </android.support.v7.widget.RecyclerView>


</android.support.v4.widget.SwipeRefreshLayout>


    <TextView
        android:id="@+id/text_view"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"/>


</RelativeLayout>

item_main xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="50dp" />
    </LinearLayout>


</LinearLayout>

MainActivity的完整代码

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{


    private RecyclerView recyclerView;
    private TestAdpater testAdpater;
    private SwipeRefreshLayout refreshLayout;
    private TextView text_view;
    private LinearLayoutManager linearLayoutManager;


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


        initView();
        initData();
    }


    private void initView() {


        text_view = findViewById(R.id.text_view);


        recyclerView = findViewById(R.id.Recycler_View);
        //下拉刷新
        refreshLayout = findViewById(R.id.refreshLayout);

        refreshLayout.setColorSchemeColors(Color.RED,Color.BLUE,Color.GRAY);


        refreshLayout.dispatchNestedPreFling(100,600);


        refreshLayout.onStopNestedScroll(text_view);
        refreshLayout.setOnRefreshListener(this);
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);


        //上拉加载更多
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {


            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);


                int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                int itemCount = linearLayoutManager.getItemCount();
                //可见的条目数
                int childCount = recyclerView.getChildCount();


                if(firstVisibleItemPosition+childCount==itemCount)

                }

 }

        });
    }
     private void initData() {

        //适配器
        testAdpater = new TestAdpater(this);

        recyclerView.setAdapter(testAdpater);

        //给它一个方法

       loadDataFromText();

    }
   private void loadDataFromText() {
    ArrayList<String> list=new ArrayList<>();
           for (int i=0;i<15;i++){
            list.add("======"+i);
            testAdpater.setData(list);
         }
    }


    /**
     * Called when a swipe gesture triggers a refresh.
     */
    @Override
    public void onRefresh() {
     new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
         refreshLayout.setRefreshing(false);
           }
        },2000);
 }

}

//适配器

public class TestAdpater extends RecyclerView.Adapter{


    private Context mcontext;
    private ArrayList<String> mdata=new ArrayList<String>();
     public TestAdpater(Context context) {
        this.mcontext=context;
    }

 @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View inflate = LayoutInflater.from(mcontext).inflate(R.layout.item_main, null);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


        MyViewHolder myViewHolder =(MyViewHolder) holder;
        myViewHolder.name.setText(mdata.get(position));
     }
 @Override
    public int getItemCount() {
        return mdata.size();
    }


    public void setData(ArrayList<String> data) {
        mdata.addAll(data);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{


        private  TextView name;


        public MyViewHolder(View itemView) {
            super(itemView);


            name = itemView.findViewById(R.id.name);


        }
    }
}

猜你喜欢

转载自blog.csdn.net/LiShaoJie_521/article/details/80277057