我想使用网格视图和列表视图切换recyclerview。
但是两个视图应该具有不同的布局设计。
如果设置了LayoutManager并通知其切换工作。
但是如何在不重新创建适配器的情况下基于Recyclerview的LayoutManager更新UI。
if(isListView){
item.setIcon(getActivity().getResources().getDrawable(R.drawable.ic_list_view));
GridLayoutManager lm = new GridLayoutManager(getActivity(),2);
lm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (channelDetailAdapter != null) {
switch (channelDetailAdapter.getItemViewType(position)) {
case 0:
return 2; //number of columns of the grid
default:
return 1;
}
} else {
return -1;
}
}
});
rvChannelDetail.setLayoutManager(lm);
isListView = false;
}else{
item.setIcon(getActivity().getResources().getDrawable(R.drawable.ic_grid_view));
LinearLayoutManager lm = new LinearLayoutManager(getActivity());
rvChannelDetail.setLayoutManager(lm);
isListView = true;
}
channelDetailAdapter.notifyDataSetChanged();
如果我通知适配器,则LayoutManager的开关工作正常,但是如果添加一些布局更改,则该更改不会反映出来。
适配器内的代码是
if(layout_type.getLayoutManager() instanceof GridLayoutManager){
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.content_media_grid_items, null);
Media_Grid_ImageHolder viewHolder = new Media_Grid_ImageHolder(view);
return viewHolder;
}else{
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.content_media_list_items, null);
Media_List_ImageHolder viewHolder = new Media_List_ImageHolder(view);
return viewHolder;
}
答案 0 :(得分:1)
我认为您必须在视图持有者类中处理此问题。
return YourHolder(your view,viewType);
只写一个视图持有人:
public class YourHolder extends RecyclerView.ViewHolder
{
...
public YourHolder(View itemView,int type)
{
super(itemView);
if(type==GRID_VIEW){
// grid view initial
} if(type==LIST_VIEW){
// list view initial
}
我希望这对您有用:)