Drawer Navigation with textview and listview

2019-08-08 11:13发布

问题:

I'm trying to include a TextView to the standard Drawer Navigation for android. But i really don't know how to use it. This is what i have thus far.

<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">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 

    <LinearLayout
        android:layout_width="240dp"
        android:layout_height="match_parent" 
        android:gravity="left"
        >

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="Algemeen"
            android:background="@drawable/title_menu"
            android:textColor="#94A1A1"
            android:textAppearance="?android:attr/textAppearanceListItemSmall"
            android:minHeight="?android:attr/listPreferredItemHeightSmall"  
            android:layout_weight="1"  
            android:gravity="start"     
        />  

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#323232"
            android:layout_weight="1"
            android:gravity="start"
        />

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

I get an error: No drawer view found with absolute gravity LEFT. Could someone help me?

回答1:

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.



回答2:

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){
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(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().

public class DrawerItemClickListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
// TODO Auto-generated method stub
selectItem(position);
}
}
private void selectItem(int position){
Fragment fragment=new ColorsFragment();
Bundle bundle=new Bundle();
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.

public class ColorsFragment extends Fragment{
private int[] colors;
private int position;
@Override
public 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()

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(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