summaryrefslogtreecommitdiffstats
path: root/samples/browseable/RecyclerView/src/com.example.android.recyclerview/RecyclerViewFragment.java
diff options
context:
space:
mode:
Diffstat (limited to 'samples/browseable/RecyclerView/src/com.example.android.recyclerview/RecyclerViewFragment.java')
-rw-r--r--samples/browseable/RecyclerView/src/com.example.android.recyclerview/RecyclerViewFragment.java89
1 files changed, 84 insertions, 5 deletions
diff --git a/samples/browseable/RecyclerView/src/com.example.android.recyclerview/RecyclerViewFragment.java b/samples/browseable/RecyclerView/src/com.example.android.recyclerview/RecyclerViewFragment.java
index 4f4a5966a..019657ffb 100644
--- a/samples/browseable/RecyclerView/src/com.example.android.recyclerview/RecyclerViewFragment.java
+++ b/samples/browseable/RecyclerView/src/com.example.android.recyclerview/RecyclerViewFragment.java
@@ -18,21 +18,37 @@ package com.example.android.recyclerview;
import android.os.Bundle;
import android.support.v4.app.Fragment;
+import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
+import android.widget.RadioButton;
/**
- * Demonstrates the use of RecyclerView with a LinearLayoutManager.
+ * Demonstrates the use of {@link RecyclerView} with a {@link LinearLayoutManager} and a
+ * {@link GridLayoutManager}.
*/
public class RecyclerViewFragment extends Fragment {
private static final String TAG = "RecyclerViewFragment";
+ private static final String KEY_LAYOUT_MANAGER = "layoutManager";
+ private static final int SPAN_COUNT = 2;
+ private static final int DATASET_COUNT = 60;
+
+ private enum LayoutManagerType {
+ GRID_LAYOUT_MANAGER,
+ LINEAR_LAYOUT_MANAGER
+ }
+
+ protected LayoutManagerType mCurrentLayoutManagerType;
+
+ protected RadioButton mLinearLayoutRadioButton;
+ protected RadioButton mGridLayoutRadioButton;
protected RecyclerView mRecyclerView;
- protected RecyclerView.Adapter mAdapter;
+ protected CustomAdapter mAdapter;
protected RecyclerView.LayoutManager mLayoutManager;
protected String[] mDataset;
@@ -58,23 +74,86 @@ public class RecyclerViewFragment extends Fragment {
// to the way ListView would layout elements. The RecyclerView.LayoutManager defines how
// elements are laid out.
mLayoutManager = new LinearLayoutManager(getActivity());
- mRecyclerView.setLayoutManager(mLayoutManager);
+
+ mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
+
+ if (savedInstanceState != null) {
+ // Restore saved layout manager type.
+ mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState
+ .getSerializable(LAYOUT_MANAGER_KEY);
+ }
+ setRecyclerViewLayoutManager(mCurrentLayoutManagerType);
mAdapter = new CustomAdapter(mDataset);
// Set CustomAdapter as the adapter for RecyclerView.
mRecyclerView.setAdapter(mAdapter);
// END_INCLUDE(initializeRecyclerView)
+ mLinearLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.linear_layout_rb);
+ mLinearLayoutRadioButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ setRecyclerViewLayoutManager(LayoutManagerType.LINEAR_LAYOUT_MANAGER);
+ }
+ });
+
+ mGridLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.grid_layout_rb);
+ mGridLayoutRadioButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ setRecyclerViewLayoutManager(LayoutManagerType.GRID_LAYOUT_MANAGER);
+ }
+ });
+
return rootView;
}
/**
+ * Set RecyclerView's LayoutManager to the one given.
+ *
+ * @param layoutManagerType Type of layout manager to switch to.
+ */
+ public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) {
+ int scrollPosition = 0;
+
+ // If a layout manager has already been set, get current scroll position.
+ if (mRecyclerView.getLayoutManager() != null) {
+ scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager())
+ .findFirstCompletelyVisibleItemPosition();
+ }
+
+ switch (layoutManagerType) {
+ case GRID_LAYOUT_MANAGER:
+ mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT);
+ mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
+ break;
+ case LINEAR_LAYOUT_MANAGER:
+ mLayoutManager = new LinearLayoutManager(getActivity());
+ mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
+ break;
+ default:
+ mLayoutManager = new LinearLayoutManager(getActivity());
+ mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
+ }
+
+ mRecyclerView.setLayoutManager(mLayoutManager);
+ mRecyclerView.scrollToPosition(scrollPosition);
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle savedInstanceState) {
+ // Save currently selected layout manager.
+ savedInstanceState.putSerializable(LAYOUT_MANAGER_KEY, mCurrentLayoutManagerType);
+ super.onSaveInstanceState(savedInstanceState);
+ }
+
+ /**
* Generates Strings for RecyclerView's adapter. This data would usually come
* from a local content provider or remote server.
*/
private void initDataset() {
- mDataset = new String[60];
- for (int i=0; i < 60; i++) {
+ mDataset = new String[DATASET_COUNT];
+ for (int i = 0; i < DATASET_COUNT; i++) {
mDataset[i] = "This is element #" + i;
}
}