NestedScrollView和RecyclerView使用,并设置间距:

效果图如下:



1.NestedScrollView 和RecyclerView嵌套问题(类似ScrollView 和listView)\

需重写 RecyclerView 的 GridLayoutManager(还有另外2种,随便搜下就有)

public class FullyGridLayoutManager extends GridLayoutManager {
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]; @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:
} 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();
}
}
}
}

重写NestedScrollView,实际上是NestedScrollView禁止滑动

public class MyNestedScrollView extends NestedScrollView {
private int downX;
private int downY;
private int mTouchSlop; public MyNestedScrollView(Context context) {
super(context);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
} public MyNestedScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
} public MyNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
} @Override
public boolean onInterceptTouchEvent(MotionEvent e) {
int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downX = (int) e.getRawX();
downY = (int) e.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int moveY = (int) e.getRawY();
if (Math.abs(moveY - downY) > mTouchSlop) {
return true;
}
}
return super.onInterceptTouchEvent(e);
}
}

让recyclerView滑动

“`java

recyclerView.setNestedScrollingEnabled(false);

“`

给recyclerView创建Adapter

 public class DemoAdapter extends RecyclerView.Adapter<DemoViewHolder> {
private ArrayList<String> list;
public Context mContext; public DemoAdapter(Context mContext) {
this.mContext=mContext;
}
@Override
public DemoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(mContext).inflate(R.layout.item_viewholder, parent, false);
DemoViewHolder mViewHolder = new DemoViewHolder(mView);
return mViewHolder;
} @Override
public void onBindViewHolder(DemoViewHolder holder, int position) {
Glide.with(mContext).load("http://img.nongshanghang.cn/allimg/160906/22435210b_1.jpg").into( holder.imageView);
} @Override
public int getItemCount() {
return 9;
}
}

Viewholder 部分

 public class DemoViewHolder extends RecyclerView.ViewHolder {

    public DemoViewHolder(View itemView) {
super(itemView);
imageView=(ImageView)itemView.findViewById(R.id.imageview);
}
public ImageView imageView;
}

设置RecyclerView 的item间距

 public class SpacesItemDecoration extends RecyclerView.ItemDecoration  {

    private int left;
private int right;
private int top;
private int bottom;
public SpacesItemDecoration(int space) {
this.left=space;
this.right=space;
this.top=space;
this.bottom=space; } public SpacesItemDecoration(int left,int right,int top,int bottom) {
this.left=left;
this.right=right;
this.top=top;
this.bottom=bottom; } @Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.left=left;
outRect.right=right;
outRect.bottom=bottom;
outRect.top=top; }
}

最后设置

   recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setNestedScrollingEnabled(false);
layoutManager=new FullyGridLayoutManager(this,3,GridLayoutManager.VERTICAL,false);
adpater=new DemoAdapter(this);
recyclerView.setAdapter(adpater);
recyclerView.setLayoutManager(layoutManager);
SpacesItemDecoration decoration;
// if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 19) {
// decoration = new SpacesItemDecoration(ScreenUtils.dipToPx(getActivity(), 4), ScreenUtils.dipToPx(getActivity(), 4), ScreenUtils.dipToPx(getActivity(), 0), ScreenUtils.dipToPx(getActivity(), 8));
// }else{
decoration = new SpacesItemDecoration(4, 4, 4,4);
// }
recyclerView.addItemDecoration(decoration);
adpater.notifyDataSetChanged();

代码并不难,希望通过我这个例子,一是巩固自己的学习和理解,二是希望更多的人更好的学习,我会再接再厉,写更多的博文。

源码下载

csdn博客

NestedScrollView和RecyclerView使用,并设置间距的更多相关文章

  1. 使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突

    使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种冲突 如果你还在为处理滑动冲突而发愁,那么你需要静 ...

  2. NestedScrollView嵌套RecyclerView

    天气渐寒,然学习不可懈怠,记录一下使用NestedScrollView嵌套RecyclerView的两个问题,以后遇到可以来这里温故. 应该说在MD中,RecyclerView代替了ListView, ...

  3. Android NestedScrollView与RecyclerView嵌套,以及NestedScrollView不会滚动到屏幕顶部解决

    ①NestedScrollView与RecyclerView嵌套,导致滚动惯性消失 解决:mRecyclerView.setNestedScrollingEnabled(false); ②Nested ...

  4. Android 使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突

    如果你还在为处理滑动冲突而发愁,那么你需要静下心来看看这边文章,如果你能彻底理解这篇文章中使用的技术,那么,一切滑动冲突的问题解决起来就轻而易举了: 先扔一个最终实现的效果图 先分析下效果图中实现的功 ...

  5. UICollectionViewCell的设置间距

    UICollectionViewCell的设置间距 #pragma mark - UICollectionView 大小(宽高,平均一行三个) - (CGSize)collectionView:(UI ...

  6. Android 开发 RecyclerView设置间距

    实现步骤 首先要创建一个类继承RecyclerView.ItemDecoration 然后重新这个类的getItemOffsets方法,删除方法里的super.getItemOffsets(outRe ...

  7. Recyclerview设置间距

    首先自定义一个RecyclerViewDivider 继承 RecyclerView.ItemDecoration,实现自定义. public class RecyclerViewDivider ex ...

  8. Android 解决NestedScrollView 嵌套 RecyclerView出现的卡顿,上拉刷新无效

    解决卡顿的方法最简单的就是设置RecyclerView的android:nestedScrollingEnabled="false",放弃自己的滑动,交给外部的NestedScro ...

  9. 动态为TextView控件设置drawableLeft图标,并设置间距

    效果图: 重要属性: textView.setCompoundDrawablePadding(4);//设置图片和text之间的间距 textView.setPadding(-5, 0, 0, 0); ...

随机推荐

  1. Android NDK开发 Jni中打日志LOG(二)

    HelloJni.c文件中,加入头文件和函数声明.最终文件如下: #include <jni.h> #include <string.h> #include<androi ...

  2. python处理时间和日期

    时间和日期  (图中错误修正:dt_obj.strftime(format))     import time, datetime 1. datetime obj 1) datetime dateti ...

  3. python学习之可变不可变

    在python的数据类型中,整数.字符串.元组是不可变的:而列表.字典是可变的.所以不用C的思维来修改一个整数,它相当于重新定义了一个整数(原来的被覆盖掉了),名字和原来一样,但与前面的同名变量没有一 ...

  4. 牛客网练习赛28A

    题目链接:https://www.nowcoder.com/acm/contest/200/A 链接:https://www.nowcoder.com/acm/contest/200/A来源:牛客网 ...

  5. my___sublime Text配置

    sublime text 备份 插件下载 http://www.cnblogs.com/457220157-FTD/p/5546545.html https://www.jianshu.com/p/3 ...

  6. vue-cli生成的重要代码详解

    安装好vue-cli之后,我们可以在package.json中看到下面所示: { // 项目名称 "name": "myvue", // 项目版本 " ...

  7. 微服务(Micro Service Architecture)浅析

    最近一段时间,微服务的概念很火,可能是跟Docker技术的快速发展和壮大有一定的关系,同时借助于Uber.Netflix.Groupon等公司的实践.宣传和推广,使得MSA渐渐地成为企业或者架构师讨论 ...

  8. java Redis工具类

    redis就是一个nosql数据库,做存储做缓存的,java代码中就是嵌入了一个客户端,读取与存储数据而已. 先来一个简单的工具类: package com.ming.redis; import re ...

  9. React.js 小书 Lesson5 - React.js 基本环境安装

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson5 转载请注明出处,保留原文链接和作者信息. 安装 React.js React.js 单独使 ...

  10. PrintPreviewControl

    用于按文档打印时的外观显示PrintDocument对象.此控件没有按钮或其他用户界面元素,因此通常只有在希望编写自己的打印预览用户界面时才会使用PrintpreviewControl 属性 Docu ...