我想使用RecyclerView来动态切换视图,一个是类似ListView,另一个类似GridView。但两个加载的布局也不同。
效果类似于 文件管理器的 视图切换。
我试了下每次先setLayoutManager,再设置setAdapter,写了两个不同Adapter来加载不同视图。但是有问题,请大家帮忙看下。
case R.id.menu_item_change_view: if (isInGridView) { mPhotoRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); updateUI(); } else { mPhotoRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3)); updateGridUI(); }
private void updateUI() { List<Photo> photos = getPhotos(); if (mAdapter == null) { mAdapter = new PhotoAdapter(photos); mPhotoRecyclerView.setAdapter(mAdapter); } else { mAdapter.setPhotos(photos); mAdapter.notifyDataSetChanged(); } }
private void updateGridUI() { List<Photo> photos = getPhotos(); if (mGridPhotoAdapter == null) { mGridPhotoAdapter = new GridPhotoAdapter(photos); mPhotoRecyclerView.setAdapter(mGridPhotoAdapter); } else { mGridPhotoAdapter.setPhotos(photos); mGridPhotoAdapter.notifyDataSetChanged(); } }
慕粉15255556666
慕粉15255556666
相关分类