|
19 | 19 | import android.app.Activity; |
20 | 20 | import android.content.Context; |
21 | 21 | import android.content.Intent; |
22 | | -import android.os.AsyncTask; |
23 | 22 | import android.os.Bundle; |
24 | 23 | import android.os.Handler; |
25 | 24 | import android.os.Looper; |
@@ -1704,39 +1703,46 @@ public void switchToGridView() { |
1704 | 1703 |
|
1705 | 1704 | @SuppressLint("NotifyDataSetChanged") |
1706 | 1705 | public void switchLayoutManager(boolean grid) { |
| 1706 | + final var recyclerView = getRecyclerView(); |
| 1707 | + final var adapter = getAdapter(); |
| 1708 | + final var context = getContext(); |
| 1709 | + |
| 1710 | + if (context == null || adapter == null || recyclerView == null) { |
| 1711 | + Log_OC.e(TAG, "cannot switch layout, arguments are null"); |
| 1712 | + return; |
| 1713 | + } |
| 1714 | + |
1707 | 1715 | int position = 0; |
1708 | 1716 |
|
1709 | | - if (getRecyclerView() != null && getRecyclerView().getLayoutManager() != null) { |
1710 | | - position = ((LinearLayoutManager) getRecyclerView().getLayoutManager()) |
1711 | | - .findFirstCompletelyVisibleItemPosition(); |
| 1717 | + if (recyclerView.getLayoutManager() instanceof LinearLayoutManager linearLayoutManager) { |
| 1718 | + position = linearLayoutManager.findFirstCompletelyVisibleItemPosition(); |
1712 | 1719 | } |
1713 | 1720 |
|
1714 | 1721 | RecyclerView.LayoutManager layoutManager; |
1715 | 1722 | if (grid) { |
1716 | | - layoutManager = new GridLayoutManager(getContext(), getColumnsCount()); |
1717 | | - ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { |
| 1723 | + layoutManager = new GridLayoutManager(context, getColumnsCount()); |
| 1724 | + GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager; |
| 1725 | + gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { |
1718 | 1726 | @Override |
1719 | 1727 | public int getSpanSize(int position) { |
1720 | 1728 | if (position == getAdapter().getItemCount() - 1 || |
1721 | 1729 | position == 0 && getAdapter().shouldShowHeader()) { |
1722 | | - return ((GridLayoutManager) layoutManager).getSpanCount(); |
| 1730 | + return gridLayoutManager.getSpanCount(); |
1723 | 1731 | } else { |
1724 | 1732 | return 1; |
1725 | 1733 | } |
1726 | 1734 | } |
1727 | 1735 | }); |
1728 | | - |
1729 | 1736 | } else { |
1730 | | - layoutManager = new LinearLayoutManager(getContext()); |
| 1737 | + layoutManager = new LinearLayoutManager(context); |
1731 | 1738 | } |
1732 | 1739 |
|
1733 | | - if (getRecyclerView() != null) { |
1734 | | - getRecyclerView().setLayoutManager(layoutManager); |
1735 | | - getRecyclerView().scrollToPosition(position); |
1736 | | - getAdapter().setGridView(grid); |
1737 | | - getRecyclerView().setAdapter(getAdapter()); |
1738 | | - getAdapter().notifyDataSetChanged(); |
1739 | | - } |
| 1740 | + recyclerView.setLayoutManager(layoutManager); |
| 1741 | + recyclerView.scrollToPosition(position); |
| 1742 | + adapter.setGridView(grid); |
| 1743 | + recyclerView.setAdapter(adapter); |
| 1744 | + adapter.notifyDataSetChanged(); |
| 1745 | + onRefresh(); |
1740 | 1746 | } |
1741 | 1747 |
|
1742 | 1748 | public CommonOCFileListAdapterInterface getCommonAdapter() { |
|
0 commit comments