2 Copyright (C) 2014 Samsung Electronics Polska Sp. z o.o.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU AFFERO General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8 You may obtain a copy of the License at
10 http://www.gnu.org/licenses/agpl-3.0.txt
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com.samsung.srpol.ui.drawer;
23 import com.samsung.srpol.R;
24 import com.samsung.srpol.loader.AppListLoader;
26 import android.support.v4.app.Fragment;
27 import android.os.Bundle;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.AdapterView;
32 import android.widget.ListView;
35 public class MenuFragment extends Fragment {
37 private static final String SAVED_POSITION = "saved_position";
39 private NavigationDrawerItemListener mDrawerItemListener;
41 private ListView mMenuListView;
42 private CategoryArrayAdapter mAdapter;
44 private int mSelectedPosition = 0;
46 public MenuFragment() {
50 public void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
52 if (savedInstanceState != null) {
53 mSelectedPosition = savedInstanceState
54 .getInt(SAVED_POSITION);
55 selectItem(mSelectedPosition);
60 public void onActivityCreated(Bundle savedInstanceState) {
61 super.onActivityCreated(savedInstanceState);
62 setHasOptionsMenu(true);
66 public View onCreateView(LayoutInflater inflater, ViewGroup container,
67 Bundle savedInstanceState) {
68 View mRootView = (View) inflater.inflate(
69 R.layout.fragment_navigation_drawer, container, false);
70 mMenuListView = (ListView) mRootView
71 .findViewById(R.id.fragment_listView);
73 .setOnItemClickListener(new AdapterView.OnItemClickListener() {
75 public void onItemClick(AdapterView<?> parent, View view,
76 int position, long id) {
80 mAdapter = new CategoryArrayAdapter(getActivity().getApplicationContext());
81 mMenuListView.setAdapter(mAdapter);
83 mMenuListView.setItemChecked(mSelectedPosition, true);
87 public void refresh() {
88 if (mAdapter != null) {
90 mAdapter.addAll(AppListLoader.getCategories());
91 mAdapter.notifyDataSetChanged();
96 private void selectItem(int position) {
97 mSelectedPosition = position;
98 if (mMenuListView != null) {
99 mMenuListView.setItemChecked(position, true);
102 if (mDrawerItemListener != null) {
103 mDrawerItemListener.onNavigationDrawerItemSelected(position);
108 public void onDetach() {
110 mDrawerItemListener = null;
113 public void setDrawerItemListener(NavigationDrawerItemListener drawerItemListener) {
114 mDrawerItemListener = drawerItemListener;
118 public void onSaveInstanceState(Bundle outState) {
119 super.onSaveInstanceState(outState);
120 outState.putInt(SAVED_POSITION, mSelectedPosition);
123 public void notifyDataSetChanged(){
124 if(mAdapter != null){
125 mAdapter.notifyDataSetChanged();