最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android DrawerLayout帶有側(cè)滑功能的布局類(lèi)(1)

 更新時(shí)間:2016年07月15日 09:47:49   作者:金洪光  
這篇文章主要為大家詳細(xì)介紹了Android DrawerLayout帶有側(cè)滑功能的布局類(lèi),感興趣的小伙伴們可以參考一下

DrawerLayout顧名思義就是一個(gè)管理布局的。使用方式可以與其它的布局類(lèi)類(lèi)似。
DrawerLayout帶有滑動(dòng)的功能。只要按照drawerLayout的規(guī)定布局方式寫(xiě)完布局,就能有側(cè)滑的效果。
直接將DrawerLayout作為根布局,然后其內(nèi)部 

  第一個(gè)View為內(nèi)容區(qū)域

  第二個(gè)View為左側(cè)菜單   

       第三個(gè)View為右側(cè)側(cè)滑菜單 

當(dāng)前第三個(gè)是可選的。
使用的包如下: 
import android.support.v4.widget.DrawerLayout; 

使用這些包的時(shí)候有時(shí)有的會(huì)報(bào)錯(cuò)。這時(shí)候確保android.support.v4是不是最新的版本。
 可以更新一下support包,文件存放在sdk/extres/support中。
 然后可以通過(guò)eclipse>project right click>Android Tools>Add Support library…
 或者可以直接把文件復(fù)制到Project中l(wèi)ibs文件夾中。 

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

Drawer positioning and layout is controlled using the android:layout_gravity attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right.
 (Or start/end on platform versions that support layout direction.)
 也就是說(shuō)
  android:layout_gravity="start" 相當(dāng)于左側(cè)的MENU向右滑動(dòng)即顯示菜單,LEFT/START(RIGHT/END)
那么從布局文件中可知:
 FrameLayout是內(nèi)容區(qū), ListView是左側(cè)菜單。
我們需做一個(gè)Fragment來(lái)裝載內(nèi)容: 

public class PageFragment extends Fragment {
  public final static String ITEM_POSITION_NUMBER = "item_position_num";
  public PageFragment(){}
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
   View convertView = inflater.inflate(R.layout.page_fragment_layout, null);
   TextView tv = (TextView) convertView.findViewById(R.id.textView);
   int num = getArguments().getInt(ITEM_POSITION_NUMBER);
   //從res/array中獲取list數(shù)據(jù)
   String[] dynastyList = getResources().getStringArray(R.array.list_item);
   tv.setText(dynastyList[num]);
   return convertView;
  }
 }

代碼中可以看出當(dāng)我們?cè)谧髠?cè)菜單中選擇SelectItem時(shí)會(huì)把對(duì)應(yīng)的值顯示到內(nèi)容區(qū)。
代碼中的page_fragment_layout.xml僅是FrameLayout內(nèi)加一個(gè)TextView所以就不貼代碼了。
接下來(lái)我們需要把listView進(jìn)行填充數(shù)據(jù)。 

private ListView menuList;
private String[] mMenuTitles;
private String[] historyTitles;
private String[] musicTitles;
private String[] movieTitles;
private String[] listTitles;
     // 歷史欄
  historyTitles = getResources().getStringArray(R.array.history);
  // 音樂(lè)欄
  musicTitles = getResources().getStringArray(R.array.music);
  // 電影欄
  movieTitles = getResources().getStringArray(R.array.movie);
  // 標(biāo)題數(shù)組
  mMenuTitles = getResources().getStringArray(R.array.title);
  // 每一項(xiàng)的標(biāo)題
  listTitles = getResources().getStringArray(R.array.list_item);

  drawLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  menuList = (ListView) findViewById(R.id.left_menu);

  // 設(shè)置菜單陰影效果
  // drawLayout.setDrawerShadow(R.drawable.drawer_shadow,
  // GravityCompat.START);
  List<Item> list = new ArrayList<Item>();
  // 菜單加入歷史標(biāo)題和歷史項(xiàng)
  HeaderItem historyHeader = new HeaderItem(mMenuTitles[0]);
  list.add(historyHeader);
  for (int i = 0; i < historyTitles.length; i++) {
   EventItem historyitem = new EventItem(historyTitles[i]);
   list.add(historyitem);
  }

  // 菜單加入音樂(lè)標(biāo)題和音樂(lè)項(xiàng)
  HeaderItem musicHeader = new HeaderItem(mMenuTitles[1]);
  list.add(musicHeader);
  for (int i = 0; i < musicTitles.length; i++) {
   EventItem musicItem = new EventItem(musicTitles[i]);
   list.add(musicItem);
  }

  // 菜單加入電影標(biāo)題和電影項(xiàng)
  HeaderItem movieHeader = new HeaderItem(mMenuTitles[2]);
  list.add(movieHeader);
  for (int i = 0; i < movieTitles.length; i++) {
   EventItem movieItem = new EventItem(movieTitles[i]);
   list.add(movieItem);
  }

  MyListAdapter adapter = new MyListAdapter(this, list);
  menuList.setAdapter(adapter);

這個(gè)數(shù)據(jù)填充有點(diǎn)麻煩。自定義ListAdapter然后進(jìn)行適配。
 數(shù)據(jù)在res/values/arrays.xml中 

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string-array name="history">
  <item >三國(guó)</item>
  <item >楚漢</item>
  <item >春秋</item>
  <item >戰(zhàn)國(guó)</item>
 </string-array>
  <string-array name="music">
  <item >爵士</item>
  <item >古典</item>
  <item >現(xiàn)代</item>
  <item >民謠</item>
 </string-array>
  <string-array name="movie">
  <item >懸疑</item>
  <item >愛(ài)情</item>
  <item >歷史</item>
  <item >恐怖</item>
 </string-array>
 <string-array name="title">
  <item >歷史</item>
  <item >音樂(lè)</item>
  <item >電影</item>
 </string-array>
 <string-array name="list_item">
  <item >歷史</item>
  <item >三國(guó)</item>
  <item >楚漢</item>
  <item >春秋</item>
  <item >戰(zhàn)國(guó)</item>
  <item >音樂(lè)</item>
  <item >爵士</item>
  <item >古典</item>
  <item >現(xiàn)代</item>
  <item >民謠</item>
  <item >電影</item>
  <item >懸疑</item>
  <item >愛(ài)情</item>
  <item >歷史</item>
  <item >恐怖</item>
 </string-array>
</resources>

然后就是listView的監(jiān)聽(tīng): 

  private void initListener() {
  // 菜單單擊事件監(jiān)聽(tīng)器
  menuList.setOnItemClickListener(new DrawerItemClickListener());
 }

 /* The click listner for ListView in the navigation drawer */
 private class DrawerItemClickListener implements
   ListView.OnItemClickListener {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
   Log.i("Light", "position:" + position);
   selectItem(position);
  }
 }

 private void selectItem(int position) {
  // update the main content by replacing fragments
  PageFragment fragment = new PageFragment();
  // 將當(dāng)前選擇的項(xiàng)傳遞到Fragment
  Bundle args = new Bundle();
  args.putInt(PageFragment.ITEM_POSITION_NUMBER, position);
  fragment.setArguments(args);

  FragmentTransaction ft = MainActivity.this.getSupportFragmentManager()
    .beginTransaction();
  ft.replace(R.id.content_frame, fragment).commit();

  drawLayout.closeDrawer(menuList);
  // update selected item and title, then close the drawer
  menuList.setItemChecked(position, true);
  // 注意這里改變的是ActionBar的標(biāo)題
  getActionBar().setTitle(listTitles[position]);
 }

我們關(guān)心的是當(dāng)某一個(gè)Item被點(diǎn)擊時(shí)會(huì)發(fā)生什么即代碼: 

private void selectItem(int position) {
  // update the main content by replacing fragments
  PageFragment fragment = new PageFragment();
  // 將當(dāng)前選擇的項(xiàng)傳遞到Fragment
  Bundle args = new Bundle();
  args.putInt(PageFragment.ITEM_POSITION_NUMBER, position);
  fragment.setArguments(args);

  FragmentTransaction ft = MainActivity.this.getSupportFragmentManager()
    .beginTransaction();
  ft.replace(R.id.content_frame, fragment).commit();

  drawLayout.closeDrawer(menuList);
  // update selected item and title, then close the drawer
  menuList.setItemChecked(position, true);
  // 注意這里改變的是ActionBar的標(biāo)題
  getActionBar().setTitle(listTitles[position]);
 }

從代碼中可以看出 
1. 首先我們先通過(guò)new PageFragment();獲取內(nèi)容區(qū)。 
2. 通過(guò)Bundle把數(shù)據(jù)打包起來(lái)然后注入fragment.setArguments(args);中這樣fragment就能獲取到此數(shù)據(jù)。 
    在fragment類(lèi)中通過(guò)getArguments().getInt(ITEM_POSITION_NUMBER);可以獲取傳遞的值。 
3. 然后通過(guò)ft.replace(R.id.content_frame, fragment).commit();把內(nèi)容替換成之前定義的PageFragment  
4. 關(guān)閉菜單通過(guò)drawLayout.closeDrawer(menuList); 整個(gè)代碼中我們僅用DrawLayout這一個(gè)函數(shù) 
5. 同時(shí)把ActionBar的標(biāo)題改為selectedItem對(duì)應(yīng)的值。 
*這時(shí)有人會(huì)問(wèn)我們?cè)趺礇](méi)有ListView與DrawerLayout進(jìn)行綁定的操作。我們?cè)谥耙舱f(shuō)過(guò)DrawerLayout中的第二個(gè)開(kāi)始即是菜單View,內(nèi)部已經(jīng)綁定好了。 
就這些內(nèi)容可以實(shí)現(xiàn)左右側(cè)滑動(dòng)菜單效果了。

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

相關(guān)文章

  • Android開(kāi)發(fā)之Activity全透明漸變切換方法

    Android開(kāi)發(fā)之Activity全透明漸變切換方法

    下面小編就為大家分享一篇Android開(kāi)發(fā)之Activity全透明漸變切換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Android實(shí)現(xiàn)Camera2預(yù)覽和拍照效果

    Android實(shí)現(xiàn)Camera2預(yù)覽和拍照效果

    這篇文章主要為大家詳細(xì)介紹了Android開(kāi)發(fā)之一個(gè)類(lèi)實(shí)現(xiàn)Camera2預(yù)覽和拍照效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android實(shí)現(xiàn)下載工具的簡(jiǎn)單代碼

    Android實(shí)現(xiàn)下載工具的簡(jiǎn)單代碼

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)下載工具的簡(jiǎn)單代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android中的廣播(BroadCast)詳細(xì)介紹

    Android中的廣播(BroadCast)詳細(xì)介紹

    這篇文章主要介紹了Android中的廣播(BroadCast)詳細(xì)介紹,本文講解了什么是廣播、廣播有什么用、實(shí)現(xiàn)廣播、動(dòng)態(tài)注冊(cè)方式、配置文件方式等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • 最常見(jiàn)的猜拳小游戲Android代碼實(shí)現(xiàn)

    最常見(jiàn)的猜拳小游戲Android代碼實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了最常見(jiàn)的猜拳小游戲Android代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android編程中Handler原理及用法實(shí)例分析

    Android編程中Handler原理及用法實(shí)例分析

    這篇文章主要介紹了Android編程中Handler用法,結(jié)合實(shí)例形式分析了Handler的功能,原理及使用技巧,需要的朋友可以參考下
    2016-01-01
  • Android Studio連接SQLite數(shù)據(jù)庫(kù)的登錄注冊(cè)實(shí)現(xiàn)

    Android Studio連接SQLite數(shù)據(jù)庫(kù)的登錄注冊(cè)實(shí)現(xiàn)

    這篇文章主要介紹了Android Studio連接SQLite數(shù)據(jù)庫(kù)的登錄注冊(cè)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • android自定義ProgressDialog加載效果

    android自定義ProgressDialog加載效果

    這篇文章主要為大家詳細(xì)介紹了android自定義ProgressDialog加載效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Android滾輪選擇時(shí)間控件使用詳解

    Android滾輪選擇時(shí)間控件使用詳解

    這篇文章主要為大家詳細(xì)介紹了Android滾輪選擇時(shí)間控件使用,滾輪選擇選擇數(shù)值、選擇字符串,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • 探究Android客戶(hù)端網(wǎng)絡(luò)預(yù)連接優(yōu)化機(jī)制

    探究Android客戶(hù)端網(wǎng)絡(luò)預(yù)連接優(yōu)化機(jī)制

    一般情況下,我們都是用一些封裝好的網(wǎng)絡(luò)框架去請(qǐng)求網(wǎng)絡(luò),對(duì)底層實(shí)現(xiàn)不甚關(guān)注,而大部分情況下也不需要特別關(guān)注處理。了解底層的一些實(shí)現(xiàn),有益于我們對(duì)網(wǎng)絡(luò)加載進(jìn)行優(yōu)化。本文就是關(guān)于根據(jù)http的連接復(fù)用機(jī)制來(lái)優(yōu)化網(wǎng)絡(luò)加載速度的原理與細(xì)節(jié)
    2021-06-06

最新評(píng)論

静乐县| 彩票| 绥阳县| 徐水县| 林周县| 罗城| 清河县| 库车县| 政和县| 马龙县| 墨竹工卡县| 新野县| 沙雅县| 平武县| 丘北县| 丁青县| 日照市| 武安市| 那曲县| 滁州市| 靖安县| 德州市| 新营市| 剑川县| 新泰市| 临江市| 佛冈县| 大连市| 闽清县| 甘肃省| 姜堰市| 新绛县| 辰溪县| 东阿县| 始兴县| 长岛县| 永年县| 长岛县| 郑州市| 谷城县| 滦南县|