Android Studio第二十九期 - RecycleView的表格形式

本来说好了,最后一篇了,结果又来了一个新需求,就再写一篇吧~

效果图是这样的:

思路是这样:

demo效果:

其实要注意的地方是:

1.RecycleView在ScrollView中不滚动的设置;

2.RecycleView在ScrollView中滚动不灵敏;

3.RecycleView表格显示列数的填充算法;

不滚动的写法:

FullyLinearLayoutManager:

package com.example.p010_recycleviewall.recycleviewbiaoge;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by shining on 2017/1/10 0010.
 */

public class FullyLinearLayoutManager extends LinearLayoutManager {

    private static final String TAG = FullyLinearLayoutManager.class.getSimpleName();

    public FullyLinearLayoutManager(Context context) {
        super(context);
    }

    public FullyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    private int[] mMeasuredDimension = new int[2];

    @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);

        Log.i(TAG, "onMeasure called. \nwidthMode " + widthMode
                + " \nheightMode " + heightSpec
                + " \nwidthSize " + widthSize
                + " \nheightSize " + heightSize
                + " \ngetItemCount() " + getItemCount());

        int width = 0;
        int height = 0;
        for (int i = 0; i < getItemCount(); i++) {
            measureScrapChild(recycler, i,
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    mMeasuredDimension);

            if (getOrientation() == HORIZONTAL) {
                width = width + mMeasuredDimension[0];
                if (i == 0) {
                    height = mMeasuredDimension[1];
                }
            } else {
                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:
        }

        switch (heightMode) {
            case View.MeasureSpec.EXACTLY:
                height = heightSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

        setMeasuredDimension(width, height);
    }

    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                   int heightSpec, int[] measuredDimension) {
        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();
        } finally {
        }
    }
}

FullyGridLayoutManager:

package com.example.p010_recycleviewall.recycleviewbiaoge;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by shining on 2017/1/10 0010.
 */
public class FullyGridLayoutManager extends GridLayoutManager {

    private int mwidth = 0;
    private int mheight = 0;

    public FullyGridLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
    }

    public FullyGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
        super(context, spanCount, orientation, reverseLayout);
    }

    private int[] mMeasuredDimension = new int[2];

    public int getMwidth() {
        return mwidth;
    }

    public void setMwidth(int mwidth) {
        this.mwidth = mwidth;
    }

    public int getMheight() {
        return mheight;
    }

    public void setMheight(int mheight) {
        this.mheight = mheight;
    }

    @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);

        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);

            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:
        }

        switch (heightMode) {
            case View.MeasureSpec.EXACTLY:
                height = heightSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }
        setMheight(height);
        setMwidth(width);
        setMeasuredDimension(width, height);
    }

    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();
            }
        }
    }
}

滚动不灵敏:

1.可以设置自定义的NoScrollView:

package com.example.p010_recycleviewall.widget.NoScrollView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ScrollView;

public class NoScrollView extends ScrollView {

   private int mTouchSlop;

   public NoScrollView(Context context) {
      super(context);
      init(context);
   }

   public NoScrollView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      init(context);
   }

   public NoScrollView(Context context, AttributeSet attrs) {
      super(context, attrs);
      init(context);
   }

   private void init(Context context) {
      mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
   }

   float downX;
   float downY;

   @Override
   public boolean onInterceptTouchEvent(MotionEvent ev) {
      int action = ev.getAction();

      switch (action) {
      case MotionEvent.ACTION_DOWN:
         downX = ev.getX();
         downY = ev.getY();
         break;
      case MotionEvent.ACTION_MOVE:
         if (Math.abs(ev.getX() - downX) > mTouchSlop) {
            return false;
         }
         break;
      default:
         break;
      }
      return super.onInterceptTouchEvent(ev);
   }
   
}

2.设置RecycleView:

recyclerView1.setNestedScrollingEnabled(false);

剩下的就是算法:

//判断条件如果
if ((mList1.size()-1)%3==0){
    //1 4 7 add 2
    mList1.add(new Biaoge_listBean(""));
    mList1.add(new Biaoge_listBean(""));
}
if ((mList1.size()-1)%3==1){
    //2 5 8 add 1
    mList1.add(new Biaoge_listBean(""));
}

唉~能不能不加班看~都过年了~能不能休息一下~麻蛋~就这吧~

地址:https://github.com/geeklx/MyApplication/tree/master/p010_recycleviewall/src/main/java/com/example/p010_recycleviewall/recycleviewbiaoge

时间: 2024-10-10 06:53:07

Android Studio第二十九期 - RecycleView的表格形式的相关文章

Android Studio第二十五期 - 自定义键盘+支付输入框

代码已经整理好,效果如下图: 地址:https://github.com/geeklx/MyApplication/tree/master/p017_keyboard

Android Studio第二十六期 - 自定义Activity中Fragment之间的传值

代码已经整理好,效果如下图:(附:copy loader的~剪刀手~) 地址:https://github.com/geeklx/MyApplication/tree/master/p018_activity_fragmenta_b

Android Studio第十九期 - Glin架构

这次写的是loader的封装后的网络架构,在此基础上,把API提供给大家,另外添加一些自定义的控件,方便开发~持续更新~谢谢~ 地址:https://github.com/geeklx/MakeJarAAR API: 1.GlideUtil:图片缓存框架,支持圆角 //1 GlideUtil.display(MainActivity.this, iv1, "http://img0.bdstatic.com/img/image/touxiang01.jpg", GlideOptionsF

Android Studio第二十四期 - Gson封装Utils

代码已经整理好,效果如下: Json.Utils: import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.

Android Studio第二十期 - 识别Recorder声纹显示水波泡泡效果

代码已经整理好,效果如下图: 总结:此版本控制在6.0以下,以上的自己手动改targetSdkVersion,另外要想想怎么做到语音识别的时候并且播放音乐和此通用一个Record,以后要写一写~ 地址:http://down.51cto.com/data/2260152

Android Studio第二十一期 - TabLayout+Fragment+RecycleView组合所有用法

代码已经整理好,效果如下图: 图1:(对应包名:fragmentviewpager) 图2: 地址:        https://github.com/geeklx/MyApplication/tree/master/p010_recycleviewall/src/main/java/com/example/p010_recycleviewall/tablayout 未完待续~

OSChina 技术周刊第二十九期 —— HTTP 有时候比 HTTPS 好?

每周技术抢先看,总有你想要的! 移动开发 [软件]iOS 图表控件 ios-charts [软件]跨平台应用开发框架 Bridge.NET [博客]为什么不能往 Android 的 Application 对象里存储数据 [博客]Android apk 自我保护技术-修改 Dalvik 指令 前端开发 [软件]AngularJS 指令绑定 Angular-xeditable [软件]J2EE 快速开发平台 EOVA [软件]轻量级 JavaScript 库 JSLite [软件]jQuery 就

Android Studio第十五期 - 友盟统计集成

首先是官网的地址:http://dev.umeng.com/analytics/android-doc/integration#2_5 然后是注意实例化到Application中的代码:  private void setUMEngAnalsys() {   //开启Debug模式 上线可关闭   MobclickAgent.setDebugMode(true);   // SDK在统计Fragment时,需要关闭Activity自带的页面统计,   // 然后在每个页面中重新集成页面统计的代码

Android Studio第十八期 - Snaphelper

代码已经整理好,效果如下图: GravitySnapHelper: /*  * Copyright (C) 2016 The Android Open Source Project  * Copyright (C) 2016 Rúben Sousa  *  * Licensed under the Apache License, Version 2.0 (the "License");  * you may not use this file except in compliance