随着RecyclerView的出现,Listview、GridView的使用率相对有些减少,废话不多说,先看一下效果:

代码如下:

1.自定义的RecyclerView(根据自己的需要)

  1. public class CallbkRecyclerView extends RecyclerView {
    private OnReachBottomListener onReachBottomListener;
    private boolean isInTheBottom = false;
    private int reachBottomRow = 1;
    private List<OnScrollListener> mScrollListeners;
    public CallbkRecyclerView(Context context) {
    super(context);
    }
    public CallbkRecyclerView(Context context, AttributeSet attrs) {
    super(context, attrs);
    }
  2.  
  3. public CallbkRecyclerView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    }
    public void onScrolled(int dx, int dy) {
    if (onReachBottomListener != null) {
    LayoutManager layoutManager = getLayoutManager();
    if (layoutManager == null) { //it maybe unnecessary
    throw new RuntimeException("LayoutManager is null,Please check it!");
    }
    Adapter adapter = getAdapter();
    if (adapter == null) { //it maybe unnecessary
    throw new RuntimeException("Adapter is null,Please check it!");
    }
    boolean isReachBottom = false;
    //is GridLayoutManager
    if (layoutManager instanceof GridLayoutManager) {
    GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
    int rowCount = adapter.getItemCount() / gridLayoutManager.getSpanCount();
    int lastVisibleRowPosition = gridLayoutManager.findLastVisibleItemPosition() / gridLayoutManager.getSpanCount();
    isReachBottom = (lastVisibleRowPosition >= rowCount - reachBottomRow);
    }
    //is LinearLayoutManager
    else if (layoutManager instanceof LinearLayoutManager) {
    int lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
    int rowCount = adapter.getItemCount();
    if (reachBottomRow > rowCount)
    reachBottomRow = 1;
    isReachBottom = (lastVisibleItemPosition >= rowCount - reachBottomRow);
    }
    //is StaggeredGridLayoutManager
    else if (layoutManager instanceof StaggeredGridLayoutManager) {
    StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
    int spanCount = staggeredGridLayoutManager.getSpanCount();
    int[] into = new int[spanCount];
    int[] eachSpanListVisibleItemPosition = staggeredGridLayoutManager.findLastVisibleItemPositions(into);
    for (int i = 0; i < spanCount; i++) {
    if (eachSpanListVisibleItemPosition[i] > adapter.getItemCount() - reachBottomRow * spanCount) {
    isReachBottom = true;
    break;
    }
    }
    }
    if (!isReachBottom) {
    isInTheBottom = false;
    } else if (!isInTheBottom) {
    onReachBottomListener.onReachBottom();
    isInTheBottom = true;
    Log.d("RBCallbkRecyclerView", "onReachBottom");
    }
    }
    }
    public void setReachBottomRow(int reachBottomRow) {
    if (reachBottomRow < 1)
    reachBottomRow = 1;
    this.reachBottomRow = reachBottomRow;
    }
    public interface OnReachBottomListener {
    void onReachBottom();
    }
  4.  
  5. public void setOnReachBottomListener(OnReachBottomListener onReachBottomListener) {
    this.onReachBottomListener = onReachBottomListener;
    }
    //public void addOnScrollListener(OnScrollListener listener){
    // if(mScrollListeners==null){
    // mScrollListeners=new ArrayList<OnScrollListener>();
    // }
    // mScrollListeners.add(listener);
    //}
    }
    2.自定义GridLayoutManager(同样根据需要自定义)
  1. public class FullyGridLayoutManager extends GridLayoutManager {
    private int mwidth = 0;
    private int mheight = 0;
  2.  
  3. public FullyGridLayoutManager(Context context, int spanCount) {
    super(context, spanCount);
    }
  4.  
  5. public FullyGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
    super(context, spanCount, orientation, reverseLayout);
    }
  6.  
  7. private int[] mMeasuredDimension = new int[2];
  8.  
  9. public int getMwidth() {
    return mwidth;
    }
  10.  
  11. public void setMwidth(int mwidth) {
    this.mwidth = mwidth;
    }
  12.  
  13. public int getMheight() {
    return mheight;
    }
  14.  
  15. public void setMheight(int mheight) {
    this.mheight = mheight;
    }
  16.  
  17. @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
    final int widthMode = View.MeasureSpec.getMode(widthSpec);
    final int heightMode = View.MeasureSpec.getMode(heightSpec);
    final int widthSize = View.MeasureSpec.getSize(widthSpec);
    final int heightSize = View.MeasureSpec.getSize(heightSpec);
  18.  
  19. int width = 0;
    int height = 0;
    int count = getItemCount();
    int span = getSpanCount();
    for (int i = 0; i < count; i++) {
    measureScrapChild(recycler, i,
    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
    mMeasuredDimension);
  20.  
  21. if (getOrientation() == HORIZONTAL) {
    if (i % span == 0) {
    width = width + mMeasuredDimension[0];
    }
    if (i == 0) {
    height = mMeasuredDimension[1];
    }
    } else {
    if (i % span == 0) {
    height = height + mMeasuredDimension[1];
    }
    if (i == 0) {
    width = mMeasuredDimension[0];
    }
    }
    } switch (widthMode) {
    case View.MeasureSpec.EXACTLY:
    width = widthSize;
    case View.MeasureSpec.AT_MOST:
    case View.MeasureSpec.UNSPECIFIED:
    }
  22.  
  23. switch (heightMode) {
    case View.MeasureSpec.EXACTLY:
    height = heightSize;
    case View.MeasureSpec.AT_MOST:
    case View.MeasureSpec.UNSPECIFIED:
    }
    setMheight(height);
    setMwidth(width);
    setMeasuredDimension(width, height);
    }
  24.  
  25. private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
    int heightSpec, int[] measuredDimension) {
    if (position < getItemCount()) {
    try {
    View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException
    if (view != null) {
    RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
    int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
    getPaddingLeft() + getPaddingRight(), p.width);
    int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
    getPaddingTop() + getPaddingBottom(), p.height);
    view.measure(childWidthSpec, childHeightSpec);
    measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
    measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
    recycler.recycleView(view);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }
    3.
  1. public class DividerItemDecoration extends RecyclerView.ItemDecoration{
  2.  
  3. private static final int[] ATTRS = new int[] { android.R.attr.listDivider };
    private Drawable mDivider;
  4.  
  5. public DividerItemDecoration(Context context)
    {
    final TypedArray a = context.obtainStyledAttributes(ATTRS);
    mDivider = a.getDrawable(0);
    a.recycle();
    }
  6.  
  7. @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)
    {
  8.  
  9. drawHorizontal(c, parent);
    drawVertical(c, parent);
  10.  
  11. }
    private int getSpanCount(RecyclerView parent)
    {
    // 列数
    int spanCount = -1;
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager)
    {
  12.  
  13. spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
    } else if (layoutManager instanceof StaggeredGridLayoutManager)
    {
    spanCount = ((StaggeredGridLayoutManager) layoutManager)
    .getSpanCount();
    }
    return spanCount;
    }
  14.  
  15. public void drawHorizontal(Canvas c, RecyclerView parent)
    {
    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.getLeft() - params.leftMargin;
    final int right = child.getRight() + params.rightMargin
    + mDivider.getIntrinsicWidth();
    final int top = child.getBottom() + params.bottomMargin;
    final int bottom = top + mDivider.getIntrinsicHeight();
    mDivider.setBounds(left, top, right, bottom);
    mDivider.draw(c);
    }
    }
    public void drawVertical(Canvas c, RecyclerView parent)
    {
    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++)
    {
    final View child = parent.getChildAt(i);
  16.  
  17. final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
    .getLayoutParams();
    final int top = child.getTop() - params.topMargin;
    final int bottom = child.getBottom() + params.bottomMargin;
    final int left = child.getRight() + params.rightMargin;
    final int right = left + mDivider.getIntrinsicWidth();
  18.  
  19. mDivider.setBounds(left, top, right, bottom);
    mDivider.draw(c);
    }
    }
  20.  
  21. private boolean isLastColum(RecyclerView parent, int pos, int spanCount,
    int childCount)
    {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager)
    {
    if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
    {
    return true;
    }
    } else if (layoutManager instanceof StaggeredGridLayoutManager)
    {
    int orientation = ((StaggeredGridLayoutManager) layoutManager)
    .getOrientation();
    if (orientation == StaggeredGridLayoutManager.VERTICAL)
    {
    if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
    {
    return true;
    }
    } else
    {
    childCount = childCount - childCount % spanCount;
    if (pos >= childCount)// 如果是最后一列,则不需要绘制右边
    return true;
    }
    }
    return false;
    }
    private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
    int childCount)
    {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager)
    {
    childCount = childCount - childCount % spanCount;
    if (pos >= childCount)// 如果是最后一行,则不需要绘制底部
    return true;
    } else if (layoutManager instanceof StaggeredGridLayoutManager)
    {
    int orientation = ((StaggeredGridLayoutManager) layoutManager)
    .getOrientation();
    // StaggeredGridLayoutManager 且纵向滚动
    if (orientation == StaggeredGridLayoutManager.VERTICAL)
    {
    childCount = childCount - childCount % spanCount;
    // 如果是最后一行,则不需要绘制底部
    if (pos >= childCount)
    return true;
    } else
    // StaggeredGridLayoutManager 且横向滚动
    {
    // 如果是最后一行,则不需要绘制底部
    if ((pos + 1) % spanCount == 0)
    {
    return true;
    }
    }
    }
    return false;
    }
  22.  
  23. public void getItemOffsets(Rect outRect, int itemPosition,
    RecyclerView parent)
    {
    int spanCount = getSpanCount(parent);
    int childCount = parent.getAdapter().getItemCount();
    if (isLastRaw(parent, itemPosition, spanCount, childCount))// 如果是最后一行,则不需要绘制底部
    {
    outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
    } else if (isLastColum(parent, itemPosition, spanCount, childCount))// 如果是最后一列,则不需要绘制右边
    {
    outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    } else
    {
    outRect.set(0, 0, mDivider.getIntrinsicWidth(),
    mDivider.getIntrinsicHeight());
    }
    }
    }
    4.主页面
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. private CallbkRecyclerView recyclerView;
    private List<String> list;
    private HomeAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  4.  
  5. recyclerView = (CallbkRecyclerView) findViewById(R.id.id_recyclerview);
    list=new ArrayList<>();
    recyclerView.setLayoutManager(new FullyGridLayoutManager(this, 2));
    recyclerView.addItemDecoration(new DividerItemDecoration(this));
  6.  
  7. for(int i=0;i<20;i++){
    list.add("item"+i);
    }
    adapter=new HomeAdapter(this,list);
    recyclerView.setAdapter(adapter);
    adapter.setLoadCallback(new HomeAdapter.ILoadCallback() {
    @Override
    public void onLoad() {
    indateMore();
    }
    });
    }
    private void indateMore() {
    List<String> listDte=new ArrayList<>();
    for(int i=0;i<10;i++){
    list.add("NewItem"+i);
    }
    adapter.addData(listDte);
    list.addAll(listDte);
    }
    }

RecyclerView-------之GridView模式加载更多的更多相关文章

  1. Android5.0新特性:RecyclerView实现上拉加载更多

    RecyclerView是Android5.0以后推出的新控件,相比于ListView可定制性更大,大有取代ListView之势.下面这篇博客主要来实现RecyclerView的上拉加载更多功能. 基 ...

  2. Recyclerview 实现上拉加载更多

    LinearLayoutManager layoutManager; layoutManager = new LinearLayoutManager(getActivity()); layoutMan ...

  3. RecyclerView下拉刷新上拉加载更多

    现在Android里都建议用RecyclerView代替ListView和GridView,所以下拉刷新和上拉加载更多也需要实现.下拉刷新可以用SwipeRefreshLayout 包裹Recycle ...

  4. 实现上拉加载更多的SwipeRefreshLayout

    转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/49992269 本文出自:[江清清的博客] (一).前言: [好消息] ...

  5. RecyclerView实例-实现可下拉刷新上拉加载更多并可切换线性流和瀑布流模式(1)

    摘要 最近项目有个列表页需要实现线性列表和瀑布流展示的切换,首先我想到的就是上 [RecyclerView],他本身已经很好的提供了三种布局方式,只是简单做个切换应该是很简单的事情,如果要用Recyc ...

  6. ScrollView嵌套ListView嵌套GridView的上下拉以及加载更多

    ScrollView 效果 ScrollView 说明 一个ScrollView 嵌套ListView 嵌套GridView的上拉加载更多,下拉刷新的demo. 主要是重写了GridView和Lsit ...

  7. Android RecyclerView 瀑布流滑动到最后自动加载更多

    mRecycleView.setOnScrollListener(new RecyclerView.OnScrollListener(){ //用来标记是否正在向最后一个滑动,既是否向下滑动 bool ...

  8. RecyclerView 判断滑到底部 顶部 预加载 更多 分页 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  9. 实现RecyclerView下拉刷新和上拉加载更多以及RecyclerView线性、网格、瀑布流效果演示

    实现RecyclerView下拉刷新和上拉加载更多以及RecyclerView线性.网格.瀑布流效果演示 效果预览 实例APP 小米应用商店 使用方法 build.gradle文件 dependenc ...

随机推荐

  1. 转自52 梦回凉亭的她 Java常见问题,面试题

    收集整理分享# 相关概念## 面向对象的三个特征封装,继承,多态.这个应该是人人皆知.有时候也会加上抽象.## 多态的好处允许不同类对象对同一消息做出响应,即同一消息可以根据发送对象的不同而采用多种不 ...

  2. STL语法——映射:map 反片语(Ananagrams,UVa 156)

    Description Most crossword puzzle fans are used to anagrams--groups of words with the same letters i ...

  3. PHP中的面向对象OOP中的魔术方法

    一.什么是魔术方法: PHP为我们提供了一系列用__开头的函数,这些函数无需自己手动调用,会在合适的时机自动调用,这类函数称为魔术函数.例如: function __construct(){} 在ne ...

  4. thphp5.0学习笔记(一)

    1.目录结构: 其中thinkphp子目录是框架核心目录 thinkphp结构: 2.入口文件 默认自带的入口文件位于public/index.php 应用目录为application,其结构: in ...

  5. Unexpected end of input 和 Unexpected token var 和 Unexpected token ;

    在写jsp的时候使用的一段代码一直调试,出现Unexpected token ; 错误. 所以最后把代码各种精简,得到了如下的测试示例代码 <% String aaa="123&quo ...

  6. ssh代理上网

    背景: 公司开发机没有外网,但可以通过ssh连接到另一台可以上公网的机器,所以想通过ssh代理的方式上网,简单又方便,而且需要的时候上,不需要的时候也可以不上 配置: 超级简单 在开发机上建立ssh隧 ...

  7. 基于SSE实现的极速的矩形核腐蚀和膨胀(最大值和最小值)算法。

    因未测试其他作者的算法时间和效率,本文不敢自称是最快的,但是速度也可以肯定说是相当快的,在一台I5机器上占用单核的资源处理 3000 * 2000的灰度数据用时约 20ms,并且算法和核心的大小是无关 ...

  8. JS 事件派发器EventDispatcher

    在Java和AS中经常用到EventDispatcher,写了一个JS版本的. addListener :添加事件监听器 removeListener:移除事件监听器 dispatchEvent:派发 ...

  9. 10亿美元融资腾讯跟头,Grail要用基因测序做癌症早期筛查

    癌症超早期筛查:"在干草堆中寻找缝衣针"癌症是人类的噩梦,尤其是中晚期癌症,但很多时候,当患者感觉到身体不适而去医院检查时,病情都已经到了中晚期,很难治愈.而有研究表明,早期癌症患 ...

  10. php添加pcntl扩展(Linux)

    pcntl扩展可以支持php的多线程操作(仅限linux)原本需要重新编译PHP的后面configrue提示加上--enable-pcntl 由于我的php是采用yum安装的,所以不能采用上面的方式下 ...