Java tutorial
/** Copyright (c) 2015 Jordi Lpez <@wuyingren> * * MIT License: * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ package cat.wuyingren.rorhelper.fragments; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.Map; import cat.wuyingren.rorhelper.R; import cat.wuyingren.rorhelper.activities.MainActivity; import cat.wuyingren.rorhelper.profiles.CardDecks; import cat.wuyingren.rorhelper.profiles.Senator; import cat.wuyingren.rorhelper.profiles.Statesman; import cat.wuyingren.rorhelper.utils.CardsAdapter; import cat.wuyingren.rorhelper.utils.FactionAdapter; /** * Created by jordilb on 21/04/15. */ public class CardListFragment extends Fragment { public static final String ARG_SCENARIO_ID = "scenario_id"; public static final String ARG_STATEMEN_ENABLED = "statemen_enabled"; private Context context; private int scenarioID; private boolean statemen; private RecyclerView mRecyclerView; private CardsAdapter mAdapter; private Map<String, Senator> senatorMap; private Map<String, Statesman> statemanMap; public CardListFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = MainActivity.getContext(); scenarioID = getArguments().getInt(ARG_SCENARIO_ID); statemen = getArguments().getBoolean(ARG_STATEMEN_ENABLED); Log.w("TAG", "Scenario list " + scenarioID); if (statemen) { statemanMap = CardDecks.getScenarioStatesmanDeckMap(scenarioID); } else { senatorMap = CardDecks.getScenarioSenatorDeckMap(scenarioID); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_cards, container, false); mRecyclerView = (RecyclerView) rootView.findViewById(R.id.list); return rootView; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mRecyclerView.setLayoutManager(new LinearLayoutManager(context)); mRecyclerView.setItemAnimator(new DefaultItemAnimator()); if (statemen) { mAdapter = new CardsAdapter(null, statemanMap, context); } else { mAdapter = new CardsAdapter(senatorMap, null, context); } mRecyclerView.setAdapter(mAdapter); } }