Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import android.os.Looper;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
Expand Down Expand Up @@ -148,7 +149,9 @@ public class ExtendedListFragment extends Fragment implements
private ListFragmentBinding binding;

protected void setRecyclerViewAdapter(RecyclerView.Adapter recyclerViewAdapter) {
mRecyclerView.getRecycledViewPool().clear();
mRecyclerView.setAdapter(recyclerViewAdapter);
mRecyclerView.getAdapter().notifyDataSetChanged();
}

protected RecyclerView getRecyclerView() {
Expand All @@ -161,13 +164,13 @@ public void setLoading(boolean enabled) {

public void switchToGridView() {
if (!isGridEnabled()) {
getRecyclerView().setLayoutManager(new GridLayoutManager(getContext(), getColumnsCount()));
getRecyclerView().setLayoutManager(new WrapContentGridLayoutManager(getContext(), getColumnsCount()));
}
}

public void switchToListView() {
if (isGridEnabled()) {
getRecyclerView().setLayoutManager(new LinearLayoutManager(getContext()));
getRecyclerView().setLayoutManager(new WrapContentLinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
}
}

Expand Down Expand Up @@ -699,3 +702,42 @@ protected void setGridSwitchButton() {
}
}
}
class WrapContentLinearLayoutManager extends LinearLayoutManager {
public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}

@Override
public boolean supportsPredictiveItemAnimations() {
return false;
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
super.onLayoutChildren(recycler, state);
} catch (IndexOutOfBoundsException e) {
Log.e("TAG", "meet a IOOBE in RecyclerView");
}
}
}


class WrapContentGridLayoutManager extends GridLayoutManager {
public WrapContentGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}

@Override
public boolean supportsPredictiveItemAnimations() {
return false;
}

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
super.onLayoutChildren(recycler, state);
} catch (IndexOutOfBoundsException e) {
Log.e("TAG", "meet a IOOBE in RecyclerView");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1436,23 +1436,27 @@ public void switchLayoutManager(boolean grid) {

RecyclerView.LayoutManager layoutManager;
if (grid) {
layoutManager = new GridLayoutManager(getContext(), getColumnsCount());
((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//layoutManager = new GridLayoutManager(getContext(), getColumnsCount());
layoutManager=new WrapContentGridLayoutManager(getContext(),getColumnsCount());
((WrapContentGridLayoutManager) layoutManager).setSpanSizeLookup(new WrapContentGridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (position == getAdapter().getItemCount() - 1 ||
position == 0 && getAdapter().shouldShowHeader()) {
return ((GridLayoutManager) layoutManager).getSpanCount();
return ((WrapContentGridLayoutManager) layoutManager).getSpanCount();
} else {
return 1;
}
}
});

} else {
layoutManager = new LinearLayoutManager(getContext());
//layoutManager = new LinearLayoutManager(getContext());
layoutManager=new WrapContentLinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
}

getRecyclerView().getLayoutManager().removeAllViews();
getRecyclerView().getRecycledViewPool().clear();
getRecyclerView().setLayoutManager(layoutManager);
getRecyclerView().scrollToPosition(position);
getAdapter().setGridView(grid);
Expand Down