Force RecyclerView to call onCreateViewHolder

I have a recyclerview that can display items as a list, small grids or a large grid, and this can be changed at runtime. Depending on what style the user chooses, I inflate a different layout in onCreateViewHolder.

I also use layoutManger.setSpanSizeLookUp()to switch between styles. My code is as follows:

layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if(showType == ProductAdapter.SHOW_TYPE_SMALL_GRID)
                return 1;
            else
                return columnCount; //show one item per row
        }
    });

@Override
public void onClick(View v) {
    if(showType == ProductAdapter.SHOW_TYPE_SMALL_GRID)
        showType = ProductAdapter.SHOW_TYPE_LARGE_GRID;
    else
        showType = ProductAdapter.SHOW_TYPE_SMALL_GRID;


    int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
    adapter = new ProductAdapter(getActivity(), productList, showType);
    recyclerView.setAdapter(adapter);
    layoutManager.scrollToPosition(firstVisibleItem);
}

The problem is getting the onCreateViewHolder to be called. I create a new object every time the user changes the style. Is there any other way ?! to get onBindViewHolder () to be called, I just use adapter.notifyDataSetChanged()How can I get something like this for onCreateViewHolder?

Any solution that does not use multiple adapters is good enough!

+4
3

:

  • Adapter:

  • Views, Adapter:

private static final int LARGE_GRID_ITEM = -1;
private static final int SMALL_GRID_ITEM = -2;

@Override
public int getItemViewType (int position) {
    return mCurrentType;
}
  • createViewHolder viewType, , ViewHolder .

public final RecyclerView.ViewHolder createViewHolder (ViewGroup parent, int viewType){
    if (viewType == LARGE_GRID_ITEM) {
        //return large grid view holder
    } else {
        //return small grid view holder
    }
}
  • , :

public void toggleItemViewType () {
    if (mCurrentType == LARGE_GRID_ITEM){
        mCurrentType = SMALL_GRID_ITEM;
    } else {
        mCurrentType = LARGE_GRID_ITEM;
    }
}

public boolean displaysLargeGrid(){
    return mCurrentType == LARGE_GRID_ITEM;
}
  1. , :

layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if (adapter.displaysLargeGrid()) {
                return 1;
            } else {
                return columnCount; //show one item per row
            }
        }
    });

@Override
public void onClick(View v) {
    adapter.toggleItemViewType();
    adapter.notifyDataSetChanged();
}
+8

, Adapter, onCreateViewHolder(). , - .

0

, :

RecyclerView . , (, ), attach detach :

FragmentClass fragment = (FragmentClass) getSupportFragmentManager().getFragments().get(position); // the fragment position

getSupportFragmentManager().beginTransaction()
                .detach(fragment)
                .attach(fragment)
                .commit();

& , RecyclerView. :

, . FragmentManager RecyclerView, . IllegalStateException, .

-1

Source: https://habr.com/ru/post/1613161/


All Articles