我想在我的应用程序中像这个视图一样实现,如图所示。
当我滚动任何列表时,必须滚动另一个列表,它应该看起来像单个滚动视图。
public class ListFragment extends Fragment {
private RelativeLayout mContainerLayout;
private Activity mContext;
private RecyclerView mVerticalRV;
private RecyclerView mHorizontalRV;
private ArrayList<Integer> mImageResourceList;
private MenuListAdapter mVerticalListAdapter;
private MenuListAdapter mHorizontalListAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, null);
initFragment(view);
mContext.onWindowFocusChanged(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
mVerticalRV.setLayoutManager(mLayoutManager);
mVerticalRV.setItemAnimator(new DefaultItemAnimator());
mVerticalRV.setAdapter(mVerticalListAdapter);
mVerticalListAdapter.notifyDataSetChanged();
RecyclerView.LayoutManager mLayoutManager1 = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
mHorizontalRV.setLayoutManager(mLayoutManager1);
mHorizontalRV.setItemAnimator(new DefaultItemAnimator());
mHorizontalRV.setAdapter(mHorizontalListAdapter);
mHorizontalListAdapter.notifyDataSetChanged();
mVerticalRV.addOnScrollListener(firstOSL);
mHorizontalRV.addOnScrollListener(secondOSL);
return view;
}
private void initFragment( View view ) {
mContext = getActivity();
mContainerLayout = (RelativeLayout) view.findViewById(R.id.layout_container);
mVerticalRV = (RecyclerView) view.findViewById(R.id.rv_vertical);
mHorizontalRV = (RecyclerView) view.findViewById(R.id.rv_horizontal);
mImageResourceList = new ArrayList<>();
mImageResourceList.add(R.drawable.icon_1);
mImageResourceList.add(R.drawable.icon_2);
mImageResourceList.add(R.drawable.icon_3);
mImageResourceList.add(R.drawable.icon_4);
mImageResourceList.add(R.drawable.icon_5);
mImageResourceList.add(R.drawable.icon_6);
mImageResourceList.add(R.drawable.icon_7);
mImageResourceList.add(R.drawable.icon_8);
mImageResourceList.add(R.drawable.icon_9);
mImageResourceList.add(R.drawable.icon_10);
mImageResourceList.add(R.drawable.icon_11);
mImageResourceList.add(R.drawable.icon_12);
mImageResourceList.add(R.drawable.icon_13);
mImageResourceList.add(R.drawable.icon_14);
mVerticalListAdapter = new MenuListAdapter(mImageResourceList);
mHorizontalListAdapter = new MenuListAdapter(mImageResourceList);
}
int vdx;
int vdy;
int hdx;
int hdy;
OnScrollListener firstOSL= new OnScrollListener() {
@Override
public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) {
super.onScrolled(recyclerView, dx, dy);
vdx = dx;
vdy = dy;
mHorizontalRV.smoothScrollToPosition(hdx+dy);
}
};
OnScrollListener secondOSL= new OnScrollListener() {
@Override
public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) {
super.onScrolled(recyclerView, dx, dy);
hdx = dx;
hdy = dy;
}
};
}
public class MenuListAdapter extends RecyclerView.Adapter<MenuListAdapter.MyViewHolder> {
private List<Integer> mMenuList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView ivMenu;
public MyViewHolder(View view) {
super(view);
ivMenu = (ImageView) view.findViewById(R.id.iv_menu);
}
}
public MenuListAdapter(List<Integer> menuList) {
this.mMenuList = menuList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_row_menu, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.ivMenu.setImageResource(mMenuList.get(position));
}
@Override
public int getItemCount() {
return mMenuList.size();
}
}
另外,我试过这个link