我在我的项目中使用RecyclerView并设置layoutManager Vertical。 此recyclerview中有几种视图类型(标记:ARecyclerView),大多数viewTypes包含带有水平布局管理器的recyclerview(标记:BRecyclerView)。
但如果我在BRecyclerView中滚动水平项目然后滚动ARecyclerView的垂直项目,那么由于回收者,BRecyclerView始终是第一项。
这是我的代码:
public class MainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private static HashMap<Integer, Integer> indexmap = new HashMap<>();
public MainAdapter(Context context) {
this.mContext = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
RecyclerView.ViewHolder viewHolder = null;
view = LayoutInflater.from(mContext).inflate(R.layout.item_main, parent, false);
viewHolder = new AViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
AViewHolder aViewHolder = (AViewHolder) holder;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400);
aViewHolder.rootView.setLayoutParams(lp);
aViewHolder.recyclerView.setHasFixedSize(true);
aViewHolder.recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
aViewHolder.recyclerView.setAdapter(new SubAdapter(mContext));
aViewHolder.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int i = recyclerView.computeHorizontalScrollOffset();
}
});
}
@Override
public int getItemCount() {
return 20;
}
private static class AViewHolder extends RecyclerView.ViewHolder {
public View rootView;
public RecyclerView recyclerView;
public AViewHolder(View itemView) {
super(itemView);
rootView = itemView.findViewById(R.id.root_view);
recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerview);
}
}
}
public class SubAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
public SubAdapter(Context context) {
this.mContext = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
RecyclerView.ViewHolder viewHolder = null;
view = LayoutInflater.from(mContext).inflate(R.layout.item_sub, parent, false);
viewHolder = new SubViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
SubViewHolder subViewHolder = (SubViewHolder) holder;
subViewHolder.textView.setText(position + "");
}
@Override
public int getItemCount() {
return 20;
}
private static class SubViewHolder extends RecyclerView.ViewHolder{
private TextView textView;
public SubViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text);
}
}
}
有什么方法可以解决这个问题吗?