Skip to content

Commit 2ac831d

Browse files
committed
Custom adapter for navigation drawer
Signed-off-by: Umair Khan <[email protected]>
1 parent e4f6792 commit 2ac831d

File tree

2 files changed

+86
-8
lines changed

2 files changed

+86
-8
lines changed

App/src/main/java/in/ac/dtu/subtlenews/NavigationDrawerFragment.java

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package in.ac.dtu.subtlenews;
22

3-
import android.support.v7.app.ActionBarActivity;;
3+
import android.content.Context;
4+
import android.graphics.Color;
5+
import android.support.v7.app.ActionBarActivity;
46
import android.app.Activity;
57
import android.support.v7.app.ActionBar;
68
import android.support.v4.app.Fragment;
@@ -18,9 +20,12 @@
1820
import android.view.View;
1921
import android.view.ViewGroup;
2022
import android.widget.AdapterView;
21-
import android.widget.ArrayAdapter;
23+
import android.widget.BaseAdapter;
24+
import android.widget.ImageView;
2225
import android.widget.ListView;
23-
import android.widget.Toast;
26+
import android.widget.TextView;
27+
28+
import java.lang.reflect.Array;
2429

2530
/**
2631
* Fragment used for managing interactions for and presentation of a navigation drawer.
@@ -97,11 +102,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
97102
selectItem(position);
98103
}
99104
});
100-
mDrawerListView.setAdapter(new ArrayAdapter<String>(
101-
getActionBar().getThemedContext(),
102-
android.R.layout.simple_list_item_1,
103-
android.R.id.text1,
104-
Utils.categoryMap));
105+
mDrawerListView.setAdapter(new NavigationDrawerAdapter(Utils.categoryMap));
105106
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
106107
return mDrawerListView;
107108
}
@@ -270,4 +271,54 @@ public static interface NavigationDrawerCallbacks {
270271
*/
271272
void onNavigationDrawerItemSelected(int position);
272273
}
274+
275+
private class NavigationDrawerAdapter extends BaseAdapter {
276+
277+
String[] mCategoryMap;
278+
279+
public NavigationDrawerAdapter( String[] mCategoryMap ){
280+
281+
this.mCategoryMap = mCategoryMap;
282+
}
283+
284+
@Override
285+
public int getCount() {
286+
// TODO Auto-generated method stub
287+
return Array.getLength(mCategoryMap);
288+
}
289+
290+
@Override
291+
public String getItem(int pos) {
292+
// TODO Auto-generated method stub
293+
return mCategoryMap[pos];
294+
}
295+
296+
@Override
297+
public long getItemId(int pos) {
298+
// TODO Auto-generated method stub
299+
return pos;
300+
}
301+
302+
@Override
303+
public View getView(int pos, View convertView, ViewGroup parent) {
304+
305+
View rowView;
306+
307+
if(convertView == null){
308+
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
309+
rowView = inflater.inflate(R.layout.row_navigation_drawer, null);
310+
} else {
311+
rowView = convertView;
312+
}
313+
314+
ImageView navigationIcon = (ImageView) rowView.findViewById(R.id.icon_navigation);
315+
TextView navigationTitle = (TextView) rowView.findViewById(R.id.title_navigation);
316+
317+
navigationTitle.setText(mCategoryMap[pos]);
318+
navigationTitle.setTextColor(Color.WHITE);
319+
navigationIcon.setBackgroundResource(R.drawable.ic_launcher);
320+
321+
return rowView;
322+
}
323+
}
273324
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="60dp"
5+
android:paddingTop="5dp"
6+
android:paddingBottom="5dp" >
7+
8+
<ImageView
9+
android:id="@+id/icon_navigation"
10+
android:layout_width="35dp"
11+
android:layout_height="40dp"
12+
android:paddingTop="5dp"
13+
android:paddingBottom="5dp"
14+
android:layout_alignParentLeft="true"
15+
android:layout_marginLeft="12dp"
16+
android:layout_marginRight="12dp"
17+
android:layout_centerVertical="true" />
18+
19+
<TextView
20+
android:id="@+id/title_navigation"
21+
android:layout_width="wrap_content"
22+
android:layout_height="match_parent"
23+
android:layout_toRightOf="@id/icon_navigation"
24+
android:layout_centerVertical="true"
25+
android:paddingRight="40dp"/>
26+
27+
</RelativeLayout>

0 commit comments

Comments
 (0)