动态更改 RecyclerView 的视图类型

6

我希望通过点击底部按钮来更改RecyclerView中的所有item视图

我只需更改视图类型变量并调用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();
    }
}

注意: 我使用弹性布局,不需要改变span的大小,我只想改变视图。

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;
    }
}

答案更好地使用静态变量作为引用视图类型,以避免像这样的错误链接

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();
    }
}

你为什么使用了静态变量? - No Body
1
@amin,请参考此SO链接 - Nirav Bhavsar
不要在静态方法中传递 viewType,而是在构造函数中传递,我认为这样会起作用。 - ramkrishna kushwaha
@NoBody 为最后选择的视图装饰设置更改,也适用于其他片段。 - amin mahmoudi
@NiravBhavsar 谢谢你,Nirav,它可以工作了。 - amin mahmoudi
@aminmahmoudi,欢迎加入 :) - Nirav Bhavsar
3个回答

0
       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();
        }

并且回收适配器。


0

我已经为此制作了一个库:

https://github.com/sajadshokri/GhostAdapter

动态添加支持的视图类型:
public void putViewType(@LayoutRes int layout, Class<? extends RecyclerView.ViewHolder> holder) {
        this.viewTypes.put(layout, holder);
    }

根据视图类型创建相关的 viewholder

viewTypes.get(viewType).getConstructor(View.class).newInstance(view)

或者

直接使用库。


0

建议使用静态变量来表示视图类型,以避免出现像这样的错误 link

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();
      }
     }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接