RecyclerView上啦刷新下拉加载
2016-11-09 17:44
357 查看
package com.example.administrator.recyclerview; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{ private RecyclerView recyclerView; private List<String>mDatas; private SimpleAdapter myAdapter; private SwipeRefreshLayout mRefreshLayout; private LinearLayoutManager mLinearLayoutManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initDatas(); //绑定适配器 myAdapter=new SimpleAdapter(this,mDatas); //为RecyclerView加载Adapter recyclerView.setAdapter(myAdapter); //设置RecyclerView的布局管理 LinearLayoutManager layoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); recyclerView.setLayoutManager(layoutManager); //设置RecyclerView的分割线 recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST)); //监听SwipeRefreshLayout.OnRefreshListener mRefreshLayout.setOnRefreshListener(this); mLinearLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(mLinearLayoutManager); /** * 监听addOnScrollListener这个方法,新建我们的EndLessOnScrollListener * 在onLoadMore方法中去完成上拉加载的操作 * */ recyclerView.addOnScrollListener(new EndLessOnScrollListener(mLinearLayoutManager) { @Override public void onLoadMore(int currentPage) { loadMoreData(); } }); } //初始化界面 private void initViews(){ recyclerView= (RecyclerView) findViewById(R.id.id_recyclerView); mRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.layout_swipe_refresh); //这个是下拉刷新出现的那个圈圈要显示的颜色 mRefreshLayout.setColorSchemeResources( R.color.colorRed, R.color.colorYellow, R.color.colorGreen ); } //初始化一开始加载的数据 private void initDatas(){ mDatas=new ArrayList<String>(); for (int i=0; i<=10; i++){ /*mDatas.add(""+(char)i);*/ mDatas.add("Item"+i); } } //每次上拉加载的时候,就加载十条数据到RecyclerView中 private void loadMoreData(){ for (int i =0; i < 10; i++){ mDatas.add("嘿,我是“上拉加载”生出来"+i); myAdapter.notifyDataSetChanged(); mRefreshLayout.setRefreshing(false); } } /** * 重写SwipeRefreshLayout.OnRefreshListener的OnRefresh方法 * 在这里面去做下拉刷新的操作 */ @Override public void onRefresh() { updateData(); //数据重新加载完成后,提示数据发生改变,并且设置现在不在刷新 myAdapter.notifyDataSetChanged(); mRefreshLayout.setRefreshing(false); } private void updateData(){ //我在List最前面加入一条数据 mDatas.add(0, "嘿,我是“下拉出来的”"); }}package com.example.administrator.recyclerview;/** Copyright (C) 2014 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0 ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Rect;import android.graphics.drawable.Drawable;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.util.Log;import android.view.View;/*** This class is from the v7 samples of the Android SDK. It's not by me!* <p/>分割线----工具类* See the license above for details.*/public class DividerItemDecoration extends RecyclerView.ItemDecoration{private static final int[] ATTRS = new int[] { android.R.attr.listDivider };public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;private Drawable mDivider;private int mOrientation;public DividerItemDecoration(Context context, int orientation){final TypedArray a = context.obtainStyledAttributes(ATTRS);mDivider = a.getDrawable(0);a.recycle();setOrientation(orientation);}public void setOrientation(int orientation){if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST){throw new IllegalArgumentException("invalid orientation");}mOrientation = orientation;}@Overridepublic void onDraw(Canvas c, RecyclerView parent){if (mOrientation == VERTICAL_LIST) {drawVertical(c, parent);} else {drawHorizontal(c, parent);}}public void drawVertical(Canvas c, RecyclerView parent){final int left = parent.getPaddingLeft();final int right = parent.getWidth() - parent.getPaddingRight();final int childCount = parent.getChildCount();for (int i = 0; i < childCount; i++){final View child = parent.getChildAt(i);RecyclerView v = new RecyclerView(parent.getContext());final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();final int top = child.getBottom() + params.bottomMargin;final int bottom = top + mDivider.getIntrinsicHeight();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}public void drawHorizontal(Canvas c, RecyclerView parent){final int top = parent.getPaddingTop();final int bottom = parent.getHeight() - parent.getPaddingBottom();final int childCount = parent.getChildCount();for (int i = 0; i < childCount; i++){final View child = parent.getChildAt(i);final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();final int left = child.getRight() + params.rightMargin;final int right = left + mDivider.getIntrinsicHeight();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}@Overridepublic void getItemOffsets(Rect outRect, int itemPosition,RecyclerView parent){if (mOrientation == VERTICAL_LIST){outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());} else{outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);}}}
package com.example.administrator.recyclerview;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.util.Log;/*** 刷新工具类* Created by Administrator on 2016/11/9 0009.*/public abstract class EndLessOnScrollListener extends RecyclerView.OnScrollListener{//声明一个LinearLayoutManagerprivate LinearLayoutManager mLinearLayoutManager;//当前页,从0开始private int currentPage = 0;//已经加载出来的Item的数量private int totalItemCount;//主要用来存储上一个totalItemCountprivate int previousTotal = 0;//在屏幕上可见的item数量private int visibleItemCount;//在屏幕可见的Item中的第一个private int firstVisibleItem;//是否正在上拉数据private boolean loading = true;public EndLessOnScrollListener(LinearLayoutManager linearLayoutManager) {this.mLinearLayoutManager = linearLayoutManager;}@Overridepublic void onScrolled(RecyclerView recyclerView, int dx, int dy) {super.onScrolled(recyclerView, dx, dy);visibleItemCount = recyclerView.getChildCount();totalItemCount = mLinearLayoutManager.getItemCount();firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();if(loading){Log.d("wnwn", "firstVisibleItem: " + firstVisibleItem);Log.d("wnwn","totalPageCount:" +totalItemCount);Log.d("wnwn", "visibleItemCount:" + visibleItemCount);if(totalItemCount > previousTotal){//说明数据已经加载结束loading = false;previousTotal = totalItemCount;}}//这里需要好好理解if (!loading && totalItemCount-visibleItemCount <= firstVisibleItem){currentPage ++;onLoadMore(currentPage);loading = true;}}/*** 提供一个抽闲方法,在Activity中监听到这个EndLessOnScrollListener* 并且实现这个方法* */public abstract void onLoadMore(int currentPage);}package com.example.administrator.recyclerview;import android.content.Context;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import java.util.ArrayList;import java.util.List;/*** 适配器* Created by Administrator on 2016/11/8 0008.*/public class SimpleAdapter extends RecyclerView.Adapter<MyViewHolder> {private LayoutInflater layoutInflater;private Context context;private List<String>mDatas;SimpleAdapter(Context context, List<String> datas){this.context=context;this.mDatas=datas;layoutInflater=LayoutInflater.from(context);}@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view=layoutInflater.inflate(R.layout.item_main,parent,false);MyViewHolder myViewHolder=new MyViewHolder(view);return myViewHolder;}@Overridepublic void onBindViewHolder(MyViewHolder holder, int position) {holder.tv.setText(mDatas.get(position));}@Overridepublic int getItemCount() {return mDatas==null?0:mDatas.size();}}class MyViewHolder extends RecyclerView.ViewHolder{TextView tv;public MyViewHolder(View itemView) {super(itemView);tv= (TextView) itemView.findViewById(R.id.text);}}
相关文章推荐
- RecyclerView上拉刷新,下拉加载更多
- RecyclerView上拉刷新,下拉加载
- RecyclerView上拉刷新与下拉加载封装
- PullToRefreshRecyclerView——带上拉刷新下拉加载功能的RecyclerView
- MVP模式的OKhttp请求网络数据,xrecyclerview上拉刷新,下拉加载
- Android开发之RecyclerView的上拉刷新和下拉加载
- RecyclerView实现上啦刷新下拉加载
- Android XRecyclerView简单实现上拉刷新,下拉加载
- okhhtp + recyclerview + 上拉刷新下拉加载 + 点击事件
- Android RecyclerView下拉刷新 & 上拉加载更多
- 支持下拉加载刷新的RecyclerView
- RecyclerView的上拉刷新与下拉加载
- XRecyclerView实现RecyclerView下拉刷新上来加载 自己做了部分修改,使代码更简洁易用
- recyclerview上拉刷新下拉加载
- RecyclerView+SwipeRefreshLayout实 4000 现上拉刷新,下拉加载更多的功能
- PullToRefreshRecyclerView——带上拉刷新下拉加载功能的RecyclerView
- RecyclerView上拉刷新下拉加载
- Android之 RecyclerView,CardView 详解和相对应的上拉刷新下拉加载
- Xrecyclerview的使用(上拉刷新,下拉加载)
- RecyclerView上拉刷新,下拉加载