| /* |
| * Copyright (C) 2017 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| 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] and a |
| * [GridLayoutManager]. |
| */ |
| class RecyclerViewFragment : Fragment() { |
| |
| private lateinit var currentLayoutManagerType: LayoutManagerType |
| private lateinit var recyclerView: RecyclerView |
| private lateinit var layoutManager: RecyclerView.LayoutManager |
| private lateinit var dataset: Array<String> |
| |
| enum class LayoutManagerType { GRID_LAYOUT_MANAGER, LINEAR_LAYOUT_MANAGER } |
| |
| override fun onCreate(savedInstanceState: Bundle?) { |
| super.onCreate(savedInstanceState) |
| |
| // Initialize dataset, this data would usually come from a local content provider or |
| // remote server. |
| initDataset() |
| } |
| |
| override fun onCreateView(inflater: LayoutInflater, |
| container: ViewGroup?, |
| savedInstanceState: Bundle?): View? { |
| val rootView = inflater.inflate(R.layout.recycler_view_frag, |
| container, false).apply { tag = TAG} |
| |
| recyclerView = rootView.findViewById(R.id.recyclerView) |
| |
| // LinearLayoutManager is used here, this will layout the elements in a similar fashion |
| // to the way ListView would layout elements. The RecyclerView.LayoutManager defines how |
| // elements are laid out. |
| layoutManager = LinearLayoutManager(activity) |
| |
| currentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER |
| |
| if (savedInstanceState != null) { |
| // Restore saved layout manager type. |
| currentLayoutManagerType = savedInstanceState |
| .getSerializable(KEY_LAYOUT_MANAGER) as LayoutManagerType |
| } |
| setRecyclerViewLayoutManager(currentLayoutManagerType) |
| |
| // Set CustomAdapter as the adapter for RecyclerView. |
| recyclerView.adapter = CustomAdapter(dataset) |
| |
| rootView.findViewById<RadioButton>(R.id.linear_layout_rb).setOnClickListener{ |
| setRecyclerViewLayoutManager(LayoutManagerType.LINEAR_LAYOUT_MANAGER) |
| } |
| |
| rootView.findViewById<RadioButton>(R.id.grid_layout_rb).setOnClickListener{ |
| setRecyclerViewLayoutManager(LayoutManagerType.GRID_LAYOUT_MANAGER) |
| } |
| |
| return rootView |
| } |
| |
| /** |
| * Set RecyclerView's LayoutManager to the one given. |
| * |
| * @param layoutManagerType Type of layout manager to switch to. |
| */ |
| private fun setRecyclerViewLayoutManager(layoutManagerType: LayoutManagerType) { |
| var scrollPosition = 0 |
| |
| // If a layout manager has already been set, get current scroll position. |
| if (recyclerView.layoutManager != null) { |
| scrollPosition = (recyclerView.layoutManager as LinearLayoutManager) |
| .findFirstCompletelyVisibleItemPosition() |
| } |
| |
| when (layoutManagerType) { |
| RecyclerViewFragment.LayoutManagerType.GRID_LAYOUT_MANAGER -> { |
| layoutManager = GridLayoutManager(activity, SPAN_COUNT) |
| currentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER |
| } |
| RecyclerViewFragment.LayoutManagerType.LINEAR_LAYOUT_MANAGER -> { |
| layoutManager = LinearLayoutManager(activity) |
| currentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER |
| } |
| } |
| |
| with(recyclerView) { |
| layoutManager = this@RecyclerViewFragment.layoutManager |
| scrollToPosition(scrollPosition) |
| } |
| |
| } |
| |
| override fun onSaveInstanceState(savedInstanceState: Bundle) { |
| |
| // Save currently selected layout manager. |
| savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, currentLayoutManagerType) |
| super.onSaveInstanceState(savedInstanceState) |
| } |
| |
| /** |
| * Generates Strings for RecyclerView's adapter. This data would usually come |
| * from a local content provider or remote server. |
| */ |
| private fun initDataset() { |
| dataset = Array(DATASET_COUNT, {i -> "This is element # $i"}) |
| } |
| |
| companion object { |
| private val TAG = "RecyclerViewFragment" |
| private val KEY_LAYOUT_MANAGER = "layoutManager" |
| private val SPAN_COUNT = 2 |
| private val DATASET_COUNT = 60 |
| } |
| } |