Android 动态更改视图类型RecylerView
我想通过单击底部更改所有项目视图RecyclerView 我只需更改视图类型变量并调用Android 动态更改视图类型RecylerView,android,Android,我想通过单击底部更改所有项目视图RecyclerView 我只需更改视图类型变量并调用notifyDataSetChanged() 通知适配器但不更改任何内容 private void ChangeAdapterView(){ if (viewType==0) { StuffAdapter.ViewType=++viewType; recyclerView.getAdapter().notifyDataSetChanged(); } el
notifyDataSetChanged()
通知适配器但不更改任何内容
private void ChangeAdapterView(){
if (viewType==0) {
StuffAdapter.ViewType=++viewType;
recyclerView.getAdapter().notifyDataSetChanged();
}
else if (viewType==1){
viewType=0;
StuffAdapter.ViewType=viewType;
recyclerView.getAdapter().notifyDataSetChanged();
}
}
注意:我使用灵活的布局,不需要更改跨度大小
我只想换个角度
public class StuffAdapter extends RecyclerView.Adapter< StuffAdapter.ViewHolder> {
private final Realm realm;
private final String base;
public static int ViewType=0;
public static void setViewType(int viewType) {
ViewType = viewType;
}
Picasso picasso;
Context context;
StoreView communicator;
SessionManager sessionManager;
List<StuffPOJO> data;
int sellerId;
public StuffAdapter(@Nullable List<StuffPOJO> data,
Picasso picasso, Context context,StoreView communicator) {
this.context=context;
this.picasso=picasso;
this.data=data;
base=context.getResources().getString(R.string.base_url);
this.communicator=communicator;
sessionManager=new SessionManager(context);
sellerId = sessionManager.getSellerId();
}
@NonNull
@Override
public StuffAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType==0)
return new StuffAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_stuff1, parent, false));
else
return new StuffAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_stuff2, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
StuffPOJO obj = getItem(position);
holder.bindView(obj);
}
@Override
public int getItemCount() {
return data.size();
}
@Nullable
public StuffPOJO getItem(int index) {
return data.get(index);
}
public void updateData(@Nullable List<StuffPOJO> list) {
Timber.w("updateData :" + list.size());
int size=this.data.size()+1;
data.addAll(list);
notifyItemRangeInserted(size,list.size());
}
public void reSetData(@Nullable List<StuffPOJO> list){
data.clear();
data.addAll(list);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return ViewType;
}
}
public StuffAdapter(@Nullable List data,毕加索-毕加索,上下文,StoreView communicator,int-viewType){
this.context=context;
这个。毕加索=毕加索;
这个。数据=数据;
***this.ViewType=ViewType;//传递int**
base=context.getResources().getString(R.string.base\u url);
this.communicator=communicator;
sessionManager=新的sessionManager(上下文);
塞勒里德
=sessionManager.getSellerId();
}
然后调用适配器。我为它创建了一个库: 动态添加支持的视图类型:
public void putViewType(@LayoutRes int layout, Class<? extends RecyclerView.ViewHolder> holder) {
this.viewTypes.put(layout, holder);
}
或
只需使用库即可。答案最好对视图类型使用静态变量,以避免类似的错误
为什么要使用静态变量?@amin,请参考这个,而不是在静态方法中传递viewType,在构造函数中传递它,我想它会的work@NoBody对于上次选择的视图,也将更改设置为其他片段decoration@NiravBhavsar它的工作是什么
public StuffAdapter(@Nullable List<StuffPOJO> data,Picasso picasso, Context context,StoreView communicator, int viewType) {
this.context=context;
this.picasso=picasso;
this.data=data;
***this.ViewType = viewtype; // pass int***
base=context.getResources().getString(R.string.base_url);
this.communicator=communicator;
sessionManager=new SessionManager(context);
sellerId
= sessionManager.getSellerId();
}
public void putViewType(@LayoutRes int layout, Class<? extends RecyclerView.ViewHolder> holder) {
this.viewTypes.put(layout, holder);
}
viewTypes.get(viewType).getConstructor(View.class).newInstance(view)
private void ChangeAdapterView(){
if (viewType==0) {
viewType++;
stuffAdapter.setViewType(StuffAdapter.LAYOUT_ITEM_LANDSCAPE);
stuffAdapter.notifyDataSetChanged();
}
else if (viewType==1){
viewType=0;
stuffAdapter.setViewType(StuffAdapter.LAYOUT_ITEM_PORTRAIN);
stuffAdapter.notifyDataSetChanged();
}
}