6

I want change all item Views RecyclerView by click Bottom I just change view type variable and call notifyDataSetChanged() to notify adapter but don't change anything

private void ChangeAdapterView(){

    if (viewType==0) {
        StuffAdapter.ViewType=++viewType;
        recyclerView.getAdapter().notifyDataSetChanged();
    }
    else if (viewType==1){
        viewType=0;
        StuffAdapter.ViewType=viewType;
        recyclerView.getAdapter().notifyDataSetChanged();
    }
}

note: I use flexible layout and don't need to change span size I want just change view

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

Answer better use static variable for reference view type to avoid mistake like this 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();
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
amin mahmoudi
  • 630
  • 7
  • 26

3 Answers3

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

And recall adapter.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
DMK
  • 13
  • 5
0

I have made a library for that:

https://github.com/sajadshokri/GhostAdapter

add supported view types dynamically:

public void putViewType(@LayoutRes int layout, Class<? extends RecyclerView.ViewHolder> holder) {
        this.viewTypes.put(layout, holder);
    }

and make related viewholder based on view type:

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

OR

simply use the library.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
0

Answer better use static variable for view type to avoid mistake like this 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();
      }
     }
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
amin mahmoudi
  • 630
  • 7
  • 26