RecyclerView嵌套RecyclerView点击事件遇到取值错乱的问题
其实问题根本就是咱们没有处理好被嵌套的那层recycleview的position
那么解决思路也很简单,想办法把被嵌套的那层recycleview的position传出来即可
上一个简单的效果图:
video2gif_20190304_215107.gif
首先
build.gradle文件添加依赖
dependencies {
api fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
api 'com.android.support.constraint:constraint-layout:1.1.3'
api 'com.android.support:appcompat-v7:28.0.0'
//一个是recycleview依赖用的,一个是butterknife大家应该很熟了
api 'com.android.support:design:28.0.0'
api 'com.jakewharton:butterknife:8.5.1'
}
MainActivity布局很简单:
一个RecycleView即可
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/center_reclycleview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
然后是外层的item布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_marginTop="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusable="false"
android:gravity="center"
android:text="xxxx"
android:textColor="@color/GoldenColor"
android:layout_margin="3dp"
android:textSize="14dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
第二层:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<LinearLayout
android:id="@+id/lay_option"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/background_grid_select"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="xxxx"
android:textColor="@color/black"
android:textSize="13dp" />
</LinearLayout>
</LinearLayout>
最主要的来了,外层Adapter:
外层的适配器的话没什么特别的 就是要在里面初始化内层Adapter
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.ViewHolder> {
//新增itemType
public static final int ITEM_TYPE = 100;
private Context mContext;
private List<Bean.DatasBean> mList;
public RvAdapter(Context context, List<Bean.DatasBean> list) {
mContext = context;
mList = list;
}
//重写改方法,设置ItemViewType
@Override
public int getItemViewType(int position) {
//返回值与使用时设置的值需保持一致
return ITEM_TYPE;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_detaillist, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.tv_title.setText(mList.get(position).getTitle());
/*
1.把内部RecyclerView的adapter和集合数据通过holder缓存
2.使内部RecyclerView的adapter提供设置position的方法
*/
holder.list.clear();
holder.list.addAll(mList.get(position).getOptions());
if (holder.mRvAdapter == null) {
//初始化内层adapter时,把对应的position传过去
holder.mRvAdapter = new RvvAdapter(mContext, holder.list, position);
GridLayoutManager layoutManage = new GridLayoutManager(mContext, 2);
holder.rvItemItem.setLayoutManager(layoutManage);
holder.rvItemItem.addItemDecoration(new GridSpacingItemDecoration(2, 20, false));
holder.rvItemItem.setAdapter(holder.mRvAdapter);
} else {
holder.mRvAdapter.setPosition(position);
holder.mRvAdapter.notifyDataSetChanged();
}
}
@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.tv_title)
TextView tv_title;
@BindView(R.id.rv_item)
RecyclerView rvItemItem;
private RvvAdapter mRvAdapter;
private List<Bean.DatasBean.Option> list = new ArrayList<>();
ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
GridSpacingItemDecoration的话是一个 一个动态设置item个数,间距的工具类
/**
* 重写RecyclerView.ItemDecoration方法
* 一个动态设置item个数,间距的工具类
*/
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount; //列数
private int spacing; //间隔
private boolean includeEdge; //是否包含边缘
public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//这里是关键,需要根据你有几列来判断
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
}
}
主要就是内层的Adapter:
这里的话调用了一个MainActivity的公告方法,把对应的tag传出去
public class RvvAdapter extends RecyclerView.Adapter<RvvAdapter.ViewHolder> {
private Context mContext;
private List<Bean.DatasBean.Option> mList;
private int mPosition;
public RvvAdapter(Context context, List<Bean.DatasBean.Option> list, int position) {
mContext = context;
mList = list;
mPosition = position;
}
/**
* 新增方法
*
* @param position 位置
*/
public void setPosition(int position) {
this.mPosition = position;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_detail_option, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.lay_option.setTag(position);
holder.tvOption.setText(mList.get(position).getDatas());
if (mList.get(position).isSelect()) {
holder.lay_option.setBackgroundResource(R.drawable.background_grid_unselect);
} else {
holder.lay_option.setBackgroundResource(R.drawable.background_grid_select);
}
}
@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.lay_option)
LinearLayout lay_option;
@BindView(R.id.tv_option)
TextView tvOption;
ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
//这里设置一个tag,作为被嵌套的recycleview item点击事件的 position
lay_option.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mList.get((int) v.getTag()).setSelect(true);
((MainActivity) mContext).OnClickListener(mPosition, (int) v.getTag());
}
});
}
}
}
最后就是在MainActivity初始化一些数据,调用即可:
public class MainActivity extends AppCompatActivity {
@BindView(R.id.center_reclycleview)
RecyclerView c_RecyclerView;
private RvAdapter mRvAdapter;
private Bean mBean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initdata();
}
//模拟数据
private void initdata() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
c_RecyclerView.setLayoutManager(layoutManager);
c_RecyclerView.setFocusableInTouchMode(false);
mBean = new Bean();
List<Bean.DatasBean> datas = new ArrayList<>();
//模拟一些数据
for (int i = 0; i < 20; i++) {
Bean.DatasBean datasBean = new Bean.DatasBean();
List<Bean.DatasBean.Option> option = new ArrayList<>();
Bean.DatasBean.Option optionBean = new Bean.DatasBean.Option();
optionBean.setDatas("这是选项" + i);
option.add(optionBean);
Bean.DatasBean.Option optionBean1 = new Bean.DatasBean.Option();
optionBean1.setDatas("这是选项" + (i + 1));
option.add(optionBean1);
Bean.DatasBean.Option optionBean2 = new Bean.DatasBean.Option();
optionBean2.setDatas("这是选项" + (i + 2));
option.add(optionBean2);
datasBean.setOptions(option);
datasBean.setTitle("这是标题哦1");
datas.add(datasBean);
}
mBean.setDatas(datas);
mRvAdapter = new RvAdapter(this, mBean.getDatas());
c_RecyclerView.setAdapter(mRvAdapter);
mRvAdapter.notifyDataSetChanged();
}
/**
* 设置一个public方法,供adapter点击事件调用
*
* @param position 为第一层recycleview位置
* @param tag 为第二层recycleview位置
*/
public void OnClickListener(int position, int tag) {
final List<Bean.DatasBean> datasBeans = mBean.getDatas();
for (int i = 0; i < datasBeans.size(); i++) {
if (i == position) {
List<Bean.DatasBean.Option> option = datasBeans.get(i).getOptions();
for (int j = 0; j < option.size(); j++) {
if (j == tag) {
option.get(j).setSelect(true);
} else {
option.get(j).setSelect(false);
}
}
Toast.makeText(MainActivity.this,
datasBeans.get(position).getTitle() + "-" + option.get(tag).getDatas(),
Toast.LENGTH_SHORT).show();
} else {
//这里让之前选中的效果还原成未选中
List<Bean.DatasBean.Option> option = datasBeans.get(i).getOptions();
for (int j = 0; j < option.size(); j++) {
option.get(j).setSelect(false);
}
}
}
mRvAdapter.notifyDataSetChanged();
}
}
基本代码都在了
github代码:(https://github.com/a824676719/RecycleNestDemo)
网友评论