Java tutorial
/* * Copyright (C) 2014 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.quuzz.tbg.recyclerview; import android.database.sqlite.SQLiteDatabase; 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.ProgressBar; import android.widget.RadioButton; import com.quuzz.tbg.common.activities.Updater; import com.quuzz.tbg.common.logger.Log; /** * 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 = 3; public enum LayoutManagerType { GRID_LAYOUT_MANAGER, LINEAR_LAYOUT_MANAGER } protected LayoutManagerType mCurrentLayoutManagerType; protected RadioButton mLinearLayoutRadioButton; protected RadioButton mGridLayoutRadioButton; protected RecyclerView mRecyclerView; protected CustomAdapter mAdapter; protected RecyclerView.LayoutManager mLayoutManager; private String appUUID; public String getAppUUID() { return appUUID; } public void setAppUUID(String appUUID) { this.appUUID = appUUID; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialize dataset, this data would usually come from a local content provider or // remote server. // Singleton si = Singleton.getInstance(); // while(!updater.isReadyStatus()) { // try { // synchronized (si) { // si.wait(); // } // } catch (InterruptedException e) { // e.printStackTrace(); // } } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Updater updater = initDataset(); // Singleton si = Singleton.getInstance(); // Updater updater = initDataset(); // while(!updater.isReadyStatus()) { // try { // synchronized (si) { // si.wait(100); // } // } catch (InterruptedException e) { // e.printStackTrace(); // } // } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.recycler_view_frag, container, false); rootView.setTag(TAG); // BEGIN_INCLUDE(initializeRecyclerView) mRecyclerView = (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. mLayoutManager = new LinearLayoutManager(getActivity()); mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER; if (savedInstanceState != null) { // Restore saved layout manager type. mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState.getSerializable(KEY_LAYOUT_MANAGER); } setRecyclerViewLayoutManager(mCurrentLayoutManagerType); mAdapter = new CustomAdapter(getActivity()); mRecyclerView.setAdapter(mAdapter); // 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(KEY_LAYOUT_MANAGER, mCurrentLayoutManagerType); super.onSaveInstanceState(savedInstanceState); } /** * Generates Strings for RecyclerView's adapter. This data would usually come * from a local content provider or remote server. */ private Updater initDataset() { // init DB // Gets the data repository in write mode TbDbHelper tbDbHelper = new TbDbHelper(getActivity()); SQLiteDatabase db = tbDbHelper.getWritableDatabase(); // TODO read tb data from server Updater updater = new Updater(this, mRecyclerView, db); updater.execute(); return updater; } }