欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android側(cè)滑菜單控件DrawerLayout使用方法詳解

 更新時(shí)間:2017年12月02日 10:09:14   作者:戀戀西風(fēng)  
這篇文章主要為大家詳細(xì)介紹了android側(cè)滑菜單控件DrawerLayout的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

drawerLayout是Support Library包中實(shí)現(xiàn)了側(cè)滑菜單效果的控件,可以說(shuō)drawerLayout是因?yàn)榈谌娇丶鏜enuDrawer等的出現(xiàn)之后,google借鑒而出現(xiàn)的產(chǎn)物。drawerLayout分為側(cè)邊菜單和主內(nèi)容區(qū)兩部分,側(cè)邊菜單可以根據(jù)手勢(shì)展開(kāi)與隱藏(drawerLayout自身特性),主內(nèi)容區(qū)的內(nèi)容可以隨著菜單的點(diǎn)擊而變化(這需要使用者自己實(shí)現(xiàn))。

使用步驟:

創(chuàng)建一個(gè)DrawerLayout

為了添加導(dǎo)航抽屜,你需要在你的布局界面中聲明一個(gè)DrawerLayout對(duì)象作為布局的根節(jié)點(diǎn)。同時(shí)在DrawerLayout內(nèi)部添加兩個(gè)view:

  • 添加一個(gè)View,它包含應(yīng)用的主內(nèi)容(當(dāng)抽屜隱藏時(shí)你的主要布局);
  • 添加另一個(gè)View它包含了導(dǎo)航抽屜;

如下面例子所示:該布局使用了DrawerLayout它包含了兩個(gè)子節(jié)點(diǎn):一個(gè)FrameLayout它包含了主要內(nèi)容(在運(yùn)行時(shí)將會(huì)被Fragment替換) 和 一個(gè)ListView作為導(dǎo)航抽屜,上面titlebar 上圖標(biāo),負(fù)責(zé)打開(kāi)、關(guān)閉抽屜;

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 > 
 <LinearLayout 
  android:orientation="horizontal" 
  android:layout_width="match_parent" 
  android:id="@+id/titleBar" 
  android:gravity="center_vertical" 
  android:background="@android:color/darker_gray" 
  android:layout_height="40dp"> 
 
  <ImageView 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:src="@drawable/ic_drawer" 
   android:onClick="onClickDrawerOpened" 
   android:clickable="true" 
   android:id="@+id/imageView" /> 
 </LinearLayout> 
 <android.support.v4.widget.DrawerLayout 
  android:id="@+id/drawer_layout" 
  android:layout_below="@id/titleBar" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
  <!-- The main content view --> 
 
  <FrameLayout 
   android:id="@+id/content_frame" 
 
   android:layout_width="match_parent" 
   android:layout_height="match_parent"></FrameLayout> 
 
  <!-- The navigation drawer --> 
  <ListView 
   android:id="@+id/left_drawer" 
   android:layout_width="240dp" 
   android:layout_height="match_parent" 
   android:layout_gravity="start" 
   android:background="#111" 
   android:choiceMode="singleChoice" 
   android:divider="@android:color/transparent" 
   android:dividerHeight="0dp" /> 
 </android.support.v4.widget.DrawerLayout> 
</RelativeLayout> 

上面這個(gè)例子包含了一些重要的布局技巧:

  • 主內(nèi)容View(FrameLayout在最上層)必須是Drawerlayout的第一個(gè)子節(jié)點(diǎn)因?yàn)閄ML在安排這些界面的時(shí)候是按照Z(yǔ)軸的順序來(lái)安排的 同時(shí) 抽屜必須在主內(nèi)容的頂部。
  • 主內(nèi)容View被設(shè)置成匹配父View的寬和高,因?yàn)楫?dāng)導(dǎo)航抽屜隱藏的時(shí)候它要填充整個(gè)UI。
  • 導(dǎo)航View(ListView)必須被聲明一個(gè)水平的gravity借助屬性android:layout_gravity。為了滿(mǎn)足從右到左的約定,聲明它的值為”start” 代替 “l(fā)eft”(因此這個(gè)抽屜將會(huì)在右面呈現(xiàn)當(dāng)布局是RTL時(shí))
  • 在導(dǎo)航View聲明時(shí):寬度用dp為單位、高度匹配父View。為了保證用戶(hù)無(wú)論怎樣都能看到主內(nèi)容的一部分,導(dǎo)航抽屜的寬度不能超過(guò)320dp

初始化Drawer List

在你的Activity中,要做的第一件事是初始化導(dǎo)航抽屜的列表項(xiàng)。具體該怎么做根據(jù)你APP的內(nèi)容來(lái)定,但是導(dǎo)航抽屜通常包含一個(gè)Listview,所以還需要一個(gè)相匹配的Adapter(比如 ArrayAdapter 或者 SimpleCursorAdapter)
下面的例子,告訴你該如何借助一個(gè)string array 來(lái)初始化一個(gè)導(dǎo)航list

public class MainActivity extends Activity { 
 private DrawerLayout mDrawerLayout; 
 private ListView mDrawerList; 
 
 private String[] mPlanetTitles; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
 
  mPlanetTitles = getResources().getStringArray(R.array.planets_array); 
  mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
  mDrawerList = (ListView) findViewById(R.id.left_drawer); 
 
  // set a custom shadow that overlays the main content when the drawer opens 
  mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 
  // set up the drawer's list view with items and click listener 
  mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
    R.layout.drawer_list_item, mPlanetTitles)); 
  mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 
 } 
//................................ 
} 

處理導(dǎo)航點(diǎn)擊事件

當(dāng)用戶(hù)選擇了抽屜列表里面的一個(gè)Item時(shí), 系統(tǒng)調(diào)用onItemClickListener上的onItemClick(), 給setOnItemClickListener().
你在onItemClick()方法里面做什么, 取決于你的app實(shí)現(xiàn)的結(jié)構(gòu). 在下面的例子中, 選擇每一個(gè)Item都會(huì)在主要內(nèi)容的布局中插入一個(gè)不同的Fragment.

private class DrawerItemClickListener implements ListView.OnItemClickListener { 
 @Override 
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
  selectItem(position); 
 } 
 } 
 
 private void selectItem(int position) { 
 // update the main content by replacing fragments 
 Fragment fragment = new PlanetFragment(); 
 Bundle args = new Bundle(); 
 args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); 
 fragment.setArguments(args); 
 
 FragmentManager fragmentManager = getFragmentManager(); 
 fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 
 
 // update selected item and title, then close the drawer 
 mDrawerList.setItemChecked(position, true); 
 mDrawerLayout.closeDrawer(mDrawerList); 
 } 

 打開(kāi)和關(guān)閉抽屜 

使用onDrawerOpened()和onDrawerClosed () 打開(kāi)和關(guān)閉抽屜:

public void onClickDrawerOpened(View drawerView) { 
 if(!mDrawerLayout.isDrawerOpen(GravityCompat.START))//if not open ,open or close; 
 { 
 mDrawerLayout.openDrawer(mDrawerList); 
 } 
 else 
 { 
 mDrawerLayout.closeDrawer(mDrawerList); 
 } 
} 

效果圖:

Demo 下載

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論