一種是編寫兩個Adapter,切換Adapter的方式來實現切換列表視圖。另外一種方式是同一個Adapter切換佈局layout的方式來切換視圖java
直接複製如今視圖,控件ID未做變動,這樣能夠更改最少的代碼達到目的ide
private int layoutViewType = LIST_VIEW;
@Override public int getItemViewType(int position) { return layoutViewType; }
public void setLayoutViewType(int layoutViewType){ this.layoutViewType = layoutViewType; } public int getLayoutViewType(){ return this.layoutViewType; }
@NonNull @Override public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.file_manager_item, parent, false); if(viewType == GRID_VIEW) view = LayoutInflater.from(mContext).inflate(R.layout.file_manager_grid_item, parent, false); return new myViewHolder(view, mContext); }
if(fileAdapter.getLayoutViewType() == pub.LIST_VIEW){ fileAdapter.setLayoutViewType(pub.GRID_VIEW); mView.ivFmViewType.setImageResource(R.drawable.list_48); mView.rvFmShow.setLayoutManager(new GridLayoutManager(FileManagerActivity.this, 6)); }else{ fileAdapter.setLayoutViewType(pub.LIST_VIEW); mView.ivFmViewType.setImageResource(R.drawable.grid_view_48); mView.rvFmShow.setLayoutManager(new LinearLayoutManager(FileManagerActivity.this)); } fileAdapter.notifyDataSetChanged();