forked from yarolegovich/SlidingRootNav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleActivity.java
124 lines (103 loc) · 4.32 KB
/
SampleActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.yarolegovich.slidingrootnav.sample;
import android.app.Fragment;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import com.yarolegovich.slidingrootnav.SlidingRootNav;
import com.yarolegovich.slidingrootnav.SlidingRootNavBuilder;
import com.yarolegovich.slidingrootnav.sample.menu.DrawerAdapter;
import com.yarolegovich.slidingrootnav.sample.menu.DrawerItem;
import com.yarolegovich.slidingrootnav.sample.menu.SimpleItem;
import com.yarolegovich.slidingrootnav.sample.menu.SpaceItem;
import com.yarolegovich.slidingrootnav.sample.fragment.CenteredTextFragment;
import java.util.Arrays;
/**
* Created by yarolegovich on 25.03.2017.
*/
public class SampleActivity extends AppCompatActivity implements DrawerAdapter.OnItemSelectedListener {
private static final int POS_DASHBOARD = 0;
private static final int POS_ACCOUNT = 1;
private static final int POS_MESSAGES = 2;
private static final int POS_CART = 3;
private static final int POS_LOGOUT = 5;
private String[] screenTitles;
private Drawable[] screenIcons;
private SlidingRootNav slidingRootNav;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
slidingRootNav = new SlidingRootNavBuilder(this)
.withToolbarMenuToggle(toolbar)
.withMenuOpened(false)
.withContentClickableWhenMenuOpened(false)
.withSavedState(savedInstanceState)
.withMenuLayout(R.layout.menu_left_drawer)
.inject();
screenIcons = loadScreenIcons();
screenTitles = loadScreenTitles();
DrawerAdapter adapter = new DrawerAdapter(Arrays.asList(
createItemFor(POS_DASHBOARD).setChecked(true),
createItemFor(POS_ACCOUNT),
createItemFor(POS_MESSAGES),
createItemFor(POS_CART),
new SpaceItem(48),
createItemFor(POS_LOGOUT)));
adapter.setListener(this);
RecyclerView list = findViewById(R.id.list);
list.setNestedScrollingEnabled(false);
list.setLayoutManager(new LinearLayoutManager(this));
list.setAdapter(adapter);
adapter.setSelected(POS_DASHBOARD);
}
@Override
public void onItemSelected(int position) {
if (position == POS_LOGOUT) {
finish();
}
slidingRootNav.closeMenu();
Fragment selectedScreen = CenteredTextFragment.createFor(screenTitles[position]);
showFragment(selectedScreen);
}
private void showFragment(Fragment fragment) {
getFragmentManager().beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
private DrawerItem createItemFor(int position) {
return new SimpleItem(screenIcons[position], screenTitles[position])
.withIconTint(color(R.color.textColorSecondary))
.withTextTint(color(R.color.textColorPrimary))
.withSelectedIconTint(color(R.color.colorAccent))
.withSelectedTextTint(color(R.color.colorAccent));
}
private String[] loadScreenTitles() {
return getResources().getStringArray(R.array.ld_activityScreenTitles);
}
private Drawable[] loadScreenIcons() {
TypedArray ta = getResources().obtainTypedArray(R.array.ld_activityScreenIcons);
Drawable[] icons = new Drawable[ta.length()];
for (int i = 0; i < ta.length(); i++) {
int id = ta.getResourceId(i, 0);
if (id != 0) {
icons[i] = ContextCompat.getDrawable(this, id);
}
}
ta.recycle();
return icons;
}
@ColorInt
private int color(@ColorRes int res) {
return ContextCompat.getColor(this, res);
}
}