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.lastsoft.plog; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.widget.SwipeRefreshLayout; 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 com.lastsoft.plog.adapter.GroupAdapter; import com.lastsoft.plog.db.Player; import com.pluscubed.recyclerfastscroll.RecyclerFastScroller; import net.i2p.android.ext.floatingactionbutton.FloatingActionsMenu; import java.util.List; /** * Demonstrates the use of {@link RecyclerView} with a {@link LinearLayoutManager} and a * {@link GridLayoutManager}. */ public class PlayersFragment_Groups extends Fragment { private static final String TAG = "PlayersFragment"; private static final String KEY_LAYOUT_MANAGER = "layoutManager"; private static final int SPAN_COUNT = 2; private static final int DATASET_COUNT = 60; private float x, y; private enum LayoutManagerType { LINEAR_LAYOUT_MANAGER } protected LayoutManagerType mCurrentLayoutManagerType; protected RecyclerView mRecyclerView; protected GroupAdapter mAdapter; protected RecyclerView.LayoutManager mLayoutManager; protected String[] mDataset; private SwipeRefreshLayout pullToRefreshView; //private OnFragmentInteractionListener mListener; FloatingActionsMenu fabMenu; public static PlayersFragment_Groups newInstance() { PlayersFragment_Groups fragment = new PlayersFragment_Groups(); Bundle args = new Bundle(); //args.putLong("gameGroup", gameGroup); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialize dataset, this data would usually come from a local content provider or // remote server. initDataset(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.fragment_players, container, false); rootView.setTag(TAG); // BEGIN_INCLUDE(initializeRecyclerView) mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView); RecyclerFastScroller fastScroller = (RecyclerFastScroller) rootView.findViewById(R.id.fastscroller); fastScroller.attachRecyclerView(mRecyclerView); //VerticalRecyclerViewFastScroller fastScroller = (VerticalRecyclerViewFastScroller) rootView.findViewById(R.id.fastscroller); // Connect the recycler to the scroller (to let the scroller scroll the list) //fastScroller.setRecyclerView(mRecyclerView, null); // Connect the scroller to the recycler (to let the recycler scroll the scroller's handle) //mRecyclerView.setOnScrollListener(fastScroller.getOnScrollListener()); // 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(mActivity); mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; if (savedInstanceState != null) { // Restore saved layout manager type. mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState.getSerializable(KEY_LAYOUT_MANAGER); } setRecyclerViewLayoutManager(mCurrentLayoutManagerType); mAdapter = new GroupAdapter(mActivity, this, MainActivity.CurrentYear); // Set CustomAdapter as the adapter for RecyclerView. mRecyclerView.setAdapter(mAdapter); return rootView; } @Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save currently selected layout manager. savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, mCurrentLayoutManagerType); super.onSaveInstanceState(savedInstanceState); } Activity mActivity; @Override public void onAttach(Activity activity) { super.onAttach(activity); mActivity = activity; } @Override public void onStart() { super.onStart(); if (mActivity != null) { ((MainActivity) mActivity).setUpActionBar(5); } } @Override public void onDetach() { super.onDetach(); //mListener = null; if (mActivity != null) { ((MainActivity) mActivity).setUpActionBar(6); } } /** * 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 LINEAR_LAYOUT_MANAGER: mLayoutManager = new LinearLayoutManager(mActivity); mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; break; default: mLayoutManager = new LinearLayoutManager(mActivity); mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; } mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.scrollToPosition(scrollPosition); } /** * Generates Strings for RecyclerView's adapter. This data would usually come * from a local content provider or remote server. */ private void initDataset() { List<Player> players = Player.listPlayersAZ(0); if (players != null) { mDataset = new String[players.size()]; int i = 0; for (Player player : players) { mDataset[i] = player.playerName; //Log.d("V1", "player name = " + player.playerName); i++; } } } }