創建一個抽屜佈局
要添加導航抽屜,聲明與aDrawerLayout對象作爲佈局的根視圖的用戶界面。在theDrawerLayout中,添加一個視圖,其中包含屏幕的主要內容(隱藏抽屜時的主要佈局)和另一個包含導航抽屜內容的視圖。
<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>
這種佈局演示了一些重要的佈局特點: 主要內容視圖(上述的FrameLayout)必須在DrawerLayout的第一個孩子,因爲XML訂單意味着Z排序和抽屜必須在頂部內容。 主內容視圖設置爲與父視圖的寬度和高度相匹配,因爲它表示導航抽屜隱藏時的整個UI。 抽屜視圖(ListView)必須用android:layout_gravity屬性指定其水平重力。要支持從右向左(RTL)語言,請使用「start」而不是「left」指定值(因此,當佈局爲RTL時,抽屜出現在右側)。 抽屜視圖以dp爲單位指定寬度,高度與父視圖匹配。抽屜寬度不應超過320dp,以便用戶始終可以看到主要內容的一部分。 初始化抽屜列表&抽屜Latyout
首先,您必須初始化抽屜列表和抽屜佈局。然後必須使用適配器設置列表視圖的值。
mColors = getResources().getStringArray(R.array.colors_array);
mDrawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList=(ListView)findViewById(R.id.left_drawer);
監聽打開和關閉事件
要監聽抽屜打開和關閉事件,您DrawerLayout調用setDrawerListener(),並通過它DrawerLayout.DrawerListener的實現。此接口爲抽屜事件提供回調,如onDrawerOpened()和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()
}
};
處理導航點擊活動
當用戶選擇在抽屜裏的列表中的項目,系統調用onItemClick()上theOnItemClickListener給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);
}
在選擇信息
(),你必須調用相關片段的給position.Here我創建簡單的片段和改變背景顏色和文字在依賴於給輸入。
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;
}
}
當使用ActionBarDrawerToggle,你必須在 onPostCreate()和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);
}
叫它更多參考,請訪問:http://velmuruganandroidcoding.blogspot.in/2014/09/navigation-drawer-in-android.html
http://www.androiddevelopersolution.com /2013/04/facebook-like-slide-in-and-slide-out.html –