Drawer Navigation With Textview And Listview
Solution 1:
Create a Drawer Layout
To add a navigation drawer, declare your user interface with aDrawerLayout object as the root view of your layout. Inside theDrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/drawer_layout"android:layout_width="match_parent"android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"android:layout_width="match_parent"android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"android:layout_width="240dp"android:layout_height="match_parent"android:layout_gravity="start"android:choiceMode="singleChoice"android:divider="@android:color/transparent"android:dividerHeight="0dp"android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
This layout demonstrates some important layout characteristics: The main content view (the FrameLayout above) must be the first child in the DrawerLayout because the XML order implies z-ordering and the drawer must be on top of the content. The main content view is set to match the parent view's width and height, because it represents the entire UI when the navigation drawer is hidden. The drawer view (the ListView) must specify its horizontal gravity with the android:layout_gravityattribute. To support right-to-left (RTL) languages, specify the value with "start" instead of "left" (so the drawer appears on the right when the layout is RTL). The drawer view specifies its width in dp units and the height matches the parent view. The drawer width should be no more than 320dp so the user can always see a portion of the main content. Initialize the Drawer List & Drawer Latyout
First,you have to initialize the drawer list and the drawer layout.Then have to set the values for the listview using adapter.
mColors = getResources().getStringArray(R.array.colors_array);mDrawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);mDrawerList=(ListView)findViewById(R.id.left_drawer);
Listen for Open and Close Events
To listen for drawer open and close events, call setDrawerListener() on your DrawerLayout and pass it an implementation of DrawerLayout.DrawerListener. This interface provides callbacks for drawer events such as onDrawerOpened() and onDrawerClosed().
mDrawerToggle=new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close){
publicvoidonDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
publicvoidonDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
Handle Navigation Click Events
When the user selects an item in the drawer's list, the system calls onItemClick() on theOnItemClickListener given to setOnItemClickListener().
publicclassDrawerItemClickListenerimplementsOnItemClickListener {
@OverridepublicvoidonItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
// TODO Auto-generated method stub
selectItem(position);
}
}
privatevoidselectItem(int position){
Fragment fragment=newColorsFragment();
Bundle bundle=newBundle();
bundle.putInt("Position", position);
fragment.setArguments(bundle);
FragmentManager fragmentManager=getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
Toast.makeText(this, position+"", Toast.LENGTH_SHORT).show();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
in selectItem() you have to call the related fragment to the give position.Here I have create simple fragment and change the background color and text in depends on the give input.
publicclassColorsFragmentextendsFragment{
privateint[] colors;
privateint position;
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview=inflater.inflate(R.layout.fragment_layout, container,false);
position=getArguments().getInt("Position");
RelativeLayout layout=(RelativeLayout)rootview.findViewById(R.id.layout);
TextView textView=(TextView)rootview.findViewById(R.id.textview);
colors=getActivity().getResources().getIntArray(R.array.colors);
textView.setText(getResources().getStringArray(R.array.colors_array)[position]);
layout.setBackgroundColor(colors[position]);
return rootview;
}
}
When using the ActionBarDrawerToggle, you must call it during onPostCreate() and onConfigurationChanged()
@OverrideprotectedvoidonPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@OverridepublicvoidonConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
For more reference visit:http://velmuruganandroidcoding.blogspot.in/2014/09/navigation-drawer-in-android.html
Solution 2:
I think the problem lies with android:gravity="left" in linear layout.Docs recommend to use layout_gravity="start" and that should fulfill your requirement.
Post a Comment for "Drawer Navigation With Textview And Listview"