项目中难免遇到需要嵌套RecyclerView的情况,本文主要记录一下自己在工作中遇到的嵌套RecyclerView时滑动冲突的问题,方便以后查看。
页面中使用了CoordinatorLayout控件实现头部状态栏吸顶效果,状态栏下方使用了RV控件嵌套,底部是WebView,在向上滑动时由于RV嵌套出现了滑动冲突,状态栏没有向上滑动,RV控件先于状态栏向上滑动了。解决方法是调用内部嵌套RecyclerView 的setNestedScrollingEnabled(false);或者在xml布局文件的RecyclerView控件中设置android:nestedScrollingEnabled="false"让RecyclerView顺滑滑动。
这里提供一下两层RV嵌套的部分代码供参考,源码请移步githup:https://github.com/zjw0/ShoppingCart
外部RV适配器代码:
package com.zhao.shoppingcart.rvnested;
import android.support.v7.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zhao.shoppingcart.R;
import com.zhao.shoppingcart.bean.GoodsList;
import java.util.List;
public class GoodsKindListAdapter extends BaseQuickAdapter<GoodsList, BaseViewHolder> {
public GoodsKindListAdapter(int layoutResId, List<GoodsList> datas) {
super(layoutResId, datas);
}
@Override
protected void convert(final BaseViewHolder helper, final GoodsList item) {
helper.setText(R.id.tv_food_kind, item.name);
new GoodsListView(0, mContext, (RecyclerView) helper.getView(R.id.rv_kind_list), item.shoppingCartListList);
}
}
新建一个view类(GoodsListView)显示内部RV,方便区分和修改:
package com.zhao.shoppingcart.rvnested;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.scwang.smartrefresh.layout.divider.ListItemDecoration;
import com.zhao.shoppingcart.R;
import com.zhao.shoppingcart.bean.ShoppingCartList;
import java.util.List;
public class GoodsListView {
private RecyclerView rv;
private Context mActivity;
private int type;
private List<ShoppingCartList> datas;
public GoodsListView(int type, Context mActivity, RecyclerView rv, List<ShoppingCartList> datas) {
this.type = type;
this.mActivity = mActivity;
this.rv = rv;
this.datas = datas;
getGoodList();
}
//商品列表
private void getGoodList() {
/(item.price));
helper.setChecked(R.id.rb_select_goods, item.isSelected);
//选中商品
helper.getView(R.id.rb_select_goods).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
item.isSelected = !item.isSelected;
helper.setChecked(R.id.rb_select_goods, item.isSelected);
}
});
//增加商品数量
helper.getView(R.id.iv_jia).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO 增加
MainActivity.editGoodsCount(mContext, item, 1, 1, tvGoodsCount);
}
});
//减少商品数量
helper.getView(R.id.iv_jian).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO 减少
int count = TextUtils.isEmpty(item.num) ? 0 : Integer.parseInt(item.num);
if (count <= 1) {
ToastUtil.showToast(mContext, "宝贝不能在减少了哦!");
}
else {
MainActivity.editGoodsCount(mContext, item, 2, 1, tvGoodsCount);
}
}
});
helper.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mContext.startActivity(new Intent(mContext, MainActivity.class));
}
});
}
}
不足之处日后再完善。。。。。。