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. 单片机的图形UI

    https://www.st.com/content/st_com/en/stm32-graphic-user-interface.html TouchGFX Designer:如今免费,资源占用10 ...

  2. DRF之权限认证频率组建

    认证组件 很久很久以前,Web站点只是作为浏览服务器资源(数据)和其他资源的工具,甚少有什么用户交互之类的烦人的事情需要处理,所以,Web站点的开发这根本不关心什么人在什么时候访问了什么资源,不需要记 ...

  3. 《大巧不工 web前端设计修炼之道》学习笔记

    前端设计如同一个人的着装与外表,站点的设计总是最先吸引人们的眼球.布局是否合理.风格是否简介.配色是否和谐,流程是否通畅,操作是否便捷,这些前端特效都影响着用户对站点的认可度.随着用户体验,可用性,可 ...

  4. stm32串口学习(一)

    串口在工作中经常用到,今天我们从零开始学习stm32的串口编程(利用库函数). 先从最简单的情况开始,假设我们要实现的功能就是串口发送一个字节,不考虑接收,也不考虑中断. 那么要解决两个问题: 1 串 ...

  5. centos6.3 配置防火墙,开启80端口、3306端口

    vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(允许80端口通过防火 ...

  6. C#遍历匿名对象的所有属性、value

    Object obj = ,pwd=" }; //遍历匿名对象 foreach (System.Reflection.PropertyInfo p in obj.GetType().GetP ...

  7. 在快速自定义的NopCommerce中使用实体框架(EF)代码优先迁移

    我看到很多nopCommerce论坛的用户问他们如何使用Entity Framework(EF)代码优先迁移来自定义nopCommerce,添加新的字段和entites核心.我实际上在做nopComm ...

  8. css透明边框实现

    代码:   css:      .box1{ background: black; padding:20px; } .box2{ background: #fff; border: 10px soli ...

  9. 解决 maven 项目中加入了 lombok 库后依然报错的问题

    平时我们采用 maven 引入第三方库,可以方便的管理第三方 jar 包,然加入 lombok 后启动 eclipse 依然报错,这是由于 lombok 是通过反射在运行时自动生成 getter(). ...

  10. mybatis springmvc批量删除 2最新

    service层: @Override public void batchDeletes(List list) { creditDao.batchDeletes(list); } 控制层control ...