进入进阶篇,相信大家通过之前的入门篇已经有了一定的认识了,接下来就是我们通过RecyclerView实现一些自己的功能
目录
- 点击事件
- 添加、删除数据
- 更丰富的条目展示(多条目展示)
- 添加header 、footer
- 下拉刷新、上拉加载更多
进阶一:点击事件、长按事件(实现adapter和Activity之间的通信)
RecyclerView要实现点击事件和长按事件可以在适配器中定义接口事件回调
1、定义对象
private OnItemClickListener mOnItemClickListener;
2、写接口,并定义方法
/**
* 通过在adapter定义接口实现回调
*/
public interface OnItemClickListener {
//点击事件
void onItemClick(View view, int position);
//长按点击事件
void onItemLongClick(View view, int position);
}
3、
public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
this.mOnItemClickListener = mOnItemClickListener;
}
4、在adapter需要的地方调用事件
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(((ContentViewHolder) holder).llLayout, position - 1);
}
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemLongClick(((ContentViewHolder) holder).llLayout, position - 1);
}
5、Activity调用Adapter中的事件
recyclerViewAdapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(RecycleActivity.this, "点击了第" + position + "条", Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(RecycleActivity.this, "长按了第" + position + "条", Toast.LENGTH_SHORT).show();
}
});
进阶二:添加数据、删除某条数据
这里需要了解到刷新界面的多种方式
notifyDataSetChanged();//刷新整体布局
notifyItemChanged(position);//刷新指定position,会重新调用onBindViewHolder来进行刷新,如果当前position在屏幕之外,则不会调用,当重新滚动到屏幕内,会重新调用
notifyItemRangeChanged(position, 10);//刷新从position开始之后10条数据
notifyItemInserted(position);//在第position处插入数据,这个插入时会有动画
notifyItemMoved(position,5);//将当前position移动到position为5的地方,并且刷新
notifyItemRangeInserted(3,10);//批量增加
notifyItemRemoved(position);//删除第position条数据
notifyItemRangeRemoved(position, 10);//批量删除
/**
* 删除某条数据
*
* @param position
*/
public void removeData(int position) {
mList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mList.size());
}
/**
* 在指定position添加数据
*
* @param position
*/
public void addData(int position, String msg) {
if (mList == null) {
mList = new ArrayList<>();
}
if (position < mList.size()) {
//说明这里是第一条
mList.add(position, msg);
} else {
mList.add(msg);
position = mList.size() - 1;
}
notifyItemInserted(position);
notifyItemRangeChanged(position, mList.size());
}
问题一:长按事件、点击事件同时发生
解决思路
在public boolean onLongClick(View v) {return true}
需要消费掉这个长按事件 返回true
进阶三:添加header、footer
思路
1、首先要区分头布局、中间布局、尾布局三种不同的type,通过设置一个int值来判断不同的布局
2、通过getItemViewType来返回type
3、在onCreateViewHolder中根据不同的viewType显示不同的布局
4、在onBindViewHolder根据不同的holder加载不同的数据
注意
1、getItemCount这里需要返回的集合的数量+header数量+footer数量
2、当content布局中加载数据是通过position获取数据时需要将
position-1
1、添加三种不同的type来区分不同的布局
public static final int ITEM_TYPE_CONTENT = 0;
public static final int ITEM_TYPE_FOOTER = 1;
public static final int ITEM_TYPE_HEADER = 2;
2、添加头、尾布局数量
private int footerCount = 1;
private int headerCount = 1;
3、根据position获取Item的类型
@Override
public int getItemViewType(int position) {
if (isHeaderView(position)) {
return ITEM_TYPE_HEADER;
} else if (isFooterView(position)) {
return ITEM_TYPE_FOOTER;
} else {
return ITEM_TYPE_CONTENT;
}
}
4、根据不同的viewType显示不同的布局
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
/**
* 主要负责把加载子项的布局
* 将xml定义的布局实例化为view对象
*/
if (viewType == ITEM_TYPE_FOOTER) {
View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_footer_item, viewGroup, false);
return new FooterViewHolder(view);
} else if (viewType == ITEM_TYPE_HEADER) {
View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_header_item, viewGroup, false);
return new HeaderViewHolder(view);
} else if (viewType == ITEM_TYPE_CONTENT) {
View view = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item, viewGroup, false);
return new ContentViewHolder(view);
}
return null;
}
5、根据不同的ViewHolder加载不同的数据(注意content中通过position加载数据position需要-1)
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
//主要负责绑定数据
if (holder instanceof HeaderViewHolder) {
} else if (holder instanceof FooterViewHolder) {
} else if (holder instanceof ContentViewHolder) {
((ContentViewHolder) holder).tvItem.setText(mList.get(position - 1));
((ContentViewHolder) holder).llLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(((ContentViewHolder) holder).llLayout, position - 1);
}
}
});
((ContentViewHolder) holder).llLayout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemLongClick(((ContentViewHolder) holder).llLayout, position - 1);
}
return true;
}
});
}
}
6、返回的集合数量(务必加上header、footer)
@Override
public int getItemCount() {
//返回集合的数量
return mList.size() + headerCount + footerCount;
}
7、判断当前 item 是否是 FooterView
/**
* 判断当前 item 是否是 FooterView
*/
public boolean isFooterView(int position) {
return footerCount != 0 && position >= (footerCount + mList.size());
}
8、判断当前 item 是否是 HeaderView
/**
* 判断当前 item 是否是 HeaderView
*/
public boolean isHeaderView(int position) {
return headerCount != 0 && position < headerCount;
}
需加
class HeaderViewHolder extends RecyclerView.ViewHolder {
public HeaderViewHolder(@NonNull View itemView) {
super(itemView);
}
}
class FooterViewHolder extends RecyclerView.ViewHolder {
public FooterViewHolder(@NonNull View itemView) {
super(itemView);
}
}
问题一
当我们为recyclerView设置表格布局时,设置footer、header,如果不做任何处理就会出现下方错误
解决思路
1、通过setSpanSizeLookup重新设置显示的列数
2、通过判断当前view是footer、header,若是footer、header则占取整列,否则占取自己列
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if (manager instanceof GridLayoutManager) {
//通过强转将manager转换成GridLayoutManager类型
final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
//通过setSpanSizeLookup重新设置显示的列数
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
//通过判断当前view是否是footer、header
//若是footer、header则占取整列,否则占取自己列
return (isHeaderView(position) || isFooterView(position)) ? gridLayoutManager.getSpanCount() : 1;
}
});
}
}
进阶四:下拉刷新、上拉加载更多
下拉刷新:在RecyclerView布局外层添加SwipeRefreshLayout
上拉加载更多:通过RecyclerView的addOnScrollListener来判断房前滚动的状态是否是最后一个Item,来进行加载更多
1、布局
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
2、通过swipeRefreshLayout.setOnRefreshListener来进行下拉刷新
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
/**
*要执行的操作
*/
recyclerViewAdapter.addData(0, "我是刷新出来的数据");
swipeRefreshLayout.setRefreshing(false);
Toast.makeText(RecycleActivity.this, "下拉刷新成功", Toast.LENGTH_SHORT).show();
}
}, 1500);//3秒后执行Runnable中的run方法
}
});
3、通过调用addOnScrollListener来进行加载更多
onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState)//滚动状态变化时的回调
onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy)//滚动时回调
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
/**
*
* @param recyclerView 当前滚动的recyclerView
* @param newState 当前滚动的状态
*
* public static final int SCROLL_STATE_IDLE = 0;静止没有滚动
* public static final int SCROLL_STATE_DRAGGING = 1;正在被外部拖拽,即用户滑动
* public static final int SCROLL_STATE_SETTLING = 2;//自动滚动
*
*
*/
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
//滚动状态变化时的回调
super.onScrollStateChanged(recyclerView, newState);
//先判断当前状态是否已停止
//判断最后一条数据+1是否是集合的数据
//判断是否滑动到最底部
if (newState == RecyclerView.SCROLL_STATE_IDLE && (lastVisibleItem + 1 == recyclerViewAdapter.getItemCount())) {
//判断总数>0 说明可以加载更多
if (total > 0) {
//判断total > count 说明还可以加载数据
if (total > count) {
Toast.makeText(RecycleActivity.this, "上拉加载", Toast.LENGTH_SHORT).show();
count += 10;//每次加载10条
getData();//加载的数据
recyclerViewAdapter.notifyDataSetChanged();//刷新适配器
} else {
Toast.makeText(RecycleActivity.this, "没有数据加载了", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(RecycleActivity.this, "没有数据加载了", Toast.LENGTH_SHORT).show();
}
}
}
/**
*
* @param recyclerView 当前滚动的recyclerView
* @param dx 水平滚动距离
* dx > 0 时为手指向左滚动,列表滚动显示右面的内容
* dx < 0 时为手指向右滚动,列表滚动显示左面的内容
* @param dy 垂直滚动距离
* dy > 0 时为手指向上滚动,列表滚动显示下面的内容
* dy < 0 时为手指向下滚动,列表滚动显示上面的内容
*/
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//滚动时回调
//最后一个可见Item
lastVisibleItem = mLinearLayoutManager.findLastVisibleItemPosition();
}
});
整体代码
为了更好的方便查,我把Activity和Adapter放在一起写,开发中大家一定要分开逻辑来操作
package com.example.androidhightproject.module;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.*;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.androidhightproject.R;
import java.util.ArrayList;
import java.util.List;
/**
* @author dongbo
*/
public class RecycleActivity extends AppCompatActivity {
private List<String> mList = new ArrayList<>();
private RecyclerView mRecyclerView;
private SwipeRefreshLayout swipeRefreshLayout;
private int lastVisibleItem;
private int total = 30;
private int count = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle);
mRecyclerView = (RecyclerView) findViewById(R.id.recycleview);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
/**
* 设置布局管理器
* 默认垂直排布 LinearLayoutManager.VERTICAL垂直 LinearLayoutManager.HORIZONTAL水平
* boolean reverseLayout 默认是false 是否反转
*
*/
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
//设置纵向默认排列
// mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
//设置横向默认排列,这里需要注意改成横向列表,分割线需要改成横向
// mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
//设置表格布局,这里需要注意的是,有很多需求是根据返回的数据显示表格的数量,所以分割线需要自己重新定义
// mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
//设置瀑布流显示
// mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
//默认的分割线
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
//设置自定义分割线
// mRecyclerView.addItemDecoration(new SpaceItemDecoation(3));
//设置删除时的动画
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(getData(), this);
mRecyclerView.setAdapter(recyclerViewAdapter);
swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
/**
*要执行的操作
*/
recyclerViewAdapter.addData(0, "我是刷新出来的数据");
swipeRefreshLayout.setRefreshing(false);
Toast.makeText(RecycleActivity.this, "下拉刷新成功", Toast.LENGTH_SHORT).show();
}
}, 1500);//3秒后执行Runnable中的run方法
}
});
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
/**
*
* @param recyclerView 当前滚动的recyclerView
* @param newState 当前滚动的状态
*
* public static final int SCROLL_STATE_IDLE = 0;静止没有滚动
* public static final int SCROLL_STATE_DRAGGING = 1;正在被外部拖拽,即用户滑动
* public static final int SCROLL_STATE_SETTLING = 2;//自动滚动
*
*
*/
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
//滚动状态变化时的回调
super.onScrollStateChanged(recyclerView, newState);
//先判断当前状态是否已停止
//判断最后一条数据+1是否是集合的数据
//判断是否滑动到最底部
Log.e("newState", newState + "");
if (newState == RecyclerView.SCROLL_STATE_IDLE && (lastVisibleItem + 1 == recyclerViewAdapter.getItemCount())) {
//判断总数>0 说明可以加载更多
if (total > 0) {
//判断total > count 说明还可以加载数据
if (total > count) {
Toast.makeText(RecycleActivity.this, "上拉加载", Toast.LENGTH_SHORT).show();
count += 10;//每次加载10条
getData();//加载的数据
recyclerViewAdapter.notifyDataSetChanged();//刷新适配器
} else {
Toast.makeText(RecycleActivity.this, "没有数据加载了", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(RecycleActivity.this, "没有数据加载了", Toast.LENGTH_SHORT).show();
}
}
}
/**
*
* @param recyclerView 当前滚动的recyclerView
* @param dx 水平滚动距离
* dx > 0 时为手指向左滚动,列表滚动显示右面的内容
* dx < 0 时为手指向右滚动,列表滚动显示左面的内容
* @param dy 垂直滚动距离
* dy > 0 时为手指向上滚动,列表滚动显示下面的内容
* dy < 0 时为手指向下滚动,列表滚动显示上面的内容
*/
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//滚动时回调
//最后一个可见Item
Log.e("dx", dx + "");
lastVisibleItem = mLinearLayoutManager.findLastVisibleItemPosition();
}
});
recyclerViewAdapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(RecycleActivity.this, "点击了第" + position + "条", Toast.LENGTH_SHORT).show();
recyclerViewAdapter.addData(position, "我是添加的数据");
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(RecycleActivity.this, "长按了第" + position + "条", Toast.LENGTH_SHORT).show();
recyclerViewAdapter.removeData(position);
}
});
}
private List<String> getData() {
for (int i = 0; i < 10; i++) {
mList.add("测试数据" + i);
// mList.add("照片地址");
}
return mList;
}
}
class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
/**
* 首先要区分头布局、中间布局、尾布局三种不同的type,通过设置一个int值来判断不同的布局
* 通过getItemViewType来返回type
* 在onCreateViewHolder中根据不同的viewType显示不同的布局
* 在onBindViewHolder根据不同的holder加载不同的数据
* 注意:
* 1、getItemCount这里需要返回的集合的数量+header数量+footer数量
* 2、当content布局中加载数据是通过position获取数据时需要将position-1
*/
private List<String> mList;
private Context mContext;
private OnItemClickListener mOnItemClickListener;
public static final int ITEM_TYPE_CONTENT = 0;
public static final int ITEM_TYPE_FOOTER = 1;
public static final int ITEM_TYPE_HEADER = 2;
private int footerCount = 1;
private int headerCount = 1;
public RecyclerViewAdapter(List<String> mList, Context mContext) {
//大部分只有数据与上下文
this.mList = mList;
this.mContext = mContext;
}
public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
this.mOnItemClickListener = mOnItemClickListener;
}
/**
* 删除某条数据
*
* @param position
*/
public void removeData(int position) {
notifyItemRemoved(position);
mList.remove(position);
notifyItemRangeChanged(position, mList.size());
}
/**
* 在指定position添加数据
*
* @param position
*/
public void addData(int position, String msg) {
notifyItemInserted(position);
if (mList == null) {
mList = new ArrayList<>();
}
if (position < mList.size()) {
//说明这里是第一条
mList.add(position, msg);
} else {
mList.add(msg);
position = mList.size() - 1;
}
notifyItemRangeChanged(position, mList.size());
}
@Override
public int getItemViewType(int position) {
if (isHeaderView(position)) {
return ITEM_TYPE_HEADER;
} else if (isFooterView(position)) {
return ITEM_TYPE_FOOTER;
} else {
return ITEM_TYPE_CONTENT;
}
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if (manager instanceof GridLayoutManager) {
//通过强转将manager转换成GridLayoutManager类型
final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
//通过setSpanSizeLookup重新设置显示的列数
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
//通过判断当前view是否是footer、header
//若是footer、header则占取整列,否则占取自己列
return (isHeaderView(position) || isFooterView(position)) ? gridLayoutManager.getSpanCount() : 1;
}
});
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
/**
* 主要负责把加载子项的布局
* 将xml定义的布局实例化为view对象
*/
if (viewType == ITEM_TYPE_FOOTER) {
View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_footer_item, viewGroup, false);
return new FooterViewHolder(view);
} else if (viewType == ITEM_TYPE_HEADER) {
View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_header_item, viewGroup, false);
return new HeaderViewHolder(view);
} else if (viewType == ITEM_TYPE_CONTENT) {
View view = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item, viewGroup, false);
return new ContentViewHolder(view);
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
//主要负责绑定数据
if (holder instanceof HeaderViewHolder) {
} else if (holder instanceof FooterViewHolder) {
} else if (holder instanceof ContentViewHolder) {
// Glide.with(mContext).load(mList.get(position-1)).into(((ContentViewHolder) holder).ivImage);
((ContentViewHolder) holder).tvItem.setText(mList.get(position - 1));
((ContentViewHolder) holder).llLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(((ContentViewHolder) holder).llLayout, position - 1);
}
}
});
((ContentViewHolder) holder).llLayout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemLongClick(((ContentViewHolder) holder).llLayout, position - 1);
}
return true;
}
});
}
}
@Override
public int getItemCount() {
//返回集合的数量
return mList.size() + headerCount + footerCount;
}
class HeaderViewHolder extends RecyclerView.ViewHolder {
public HeaderViewHolder(@NonNull View itemView) {
super(itemView);
}
}
class FooterViewHolder extends RecyclerView.ViewHolder {
public FooterViewHolder(@NonNull View itemView) {
super(itemView);
}
}
class ContentViewHolder extends RecyclerView.ViewHolder {
// private final ImageView ivImage;
private final TextView tvItem;
private final LinearLayout llLayout;
public ContentViewHolder(@NonNull View itemView) {
super(itemView);
tvItem = (TextView) itemView.findViewById(R.id.tv_item);
llLayout = (LinearLayout) itemView.findViewById(R.id.ll_layout);
// ivImage = (ImageView) itemView.findViewById(R.id.iv_image);
}
}
/**
* 通过在adapter定义接口实现回调
*/
public interface OnItemClickListener {
//点击事件
void onItemClick(View view, int position);
//长按点击事件
void onItemLongClick(View view, int position);
}
/**
* 判断当前 item 是否是 FooterView
*/
public boolean isFooterView(int position) {
return footerCount != 0 && position >= (footerCount + mList.size());
}
/**
* 判断当前 item 是否是 HeaderView
*/
public boolean isHeaderView(int position) {
return headerCount != 0 && position < headerCount;
}
}
class SpaceItemDecoation extends RecyclerView.ItemDecoration {
private int space;
public SpaceItemDecoation(int space) {
this.space = space;
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//如果不是第一个盒子设置一个居左和居下的边距
outRect.left = space;
outRect.bottom = space;
//大部分的需求都是3个 因为第一个都是你设置个数的倍数,所以通过取余来将左边距设置为0
if (parent.getChildLayoutPosition(view) % 3 == 0) {
outRect.left = 0;
}
}
}
最后
到此对于RecyclerView你已经可以实现项目中的使用,还有很多需要我们来探索,比如回收复用机制、缓存机制等等,我也正在学习~
喜欢记得点赞或者关注我哦, 有意见或者建议评论区见~