Android开源代码——MenuDrawer(三)
【MenuDrawer的灵活用法】
1、前面的例子已经能够大体的讲MenuDrawer的使用方法表述清楚了,接下来,我们的例子是对这个MenuDrawer的使用加以熟悉,并加上一些其他的值得使用的东西
2、我们知道在菜单视图中我们一般使用的是一个ListView,但是我们完全可以不使用ListView,而且同样能够使用那个“指针提示标志”
【实例】
效果如下:

左边的菜单视图使用的是一个ScrollView,其中添加了多个TextView,形成一个个的菜单项,(这样的话,提示我们能够随意的设计菜单视图的样式,发挥想象力吧!)右边的内容视图也是TextView
主布局文件:
1 package net.simonvt.menudrawer.samples; 2 3 import net.simonvt.menudrawer.MenuDrawer; 4 import android.app.Activity; 5 import android.os.Build; 6 import android.os.Bundle; 7 import android.view.MenuItem; 8 import android.view.View; 9 import android.widget.TextView; 10 11 public class WindowSample extends Activity implements View.OnClickListener { 12 13 private MenuDrawer mMenuDrawer; 14 private TextView mContentTextView; 15 16 @Override 17 public void onCreate(Bundle inState) { 18 super.onCreate(inState); 19 20 mMenuDrawer = MenuDrawer.attach(this, MenuDrawer.MENU_DRAG_WINDOW); 21 mMenuDrawer.setContentView(R.layout.activity_windowsample); 22 mMenuDrawer.setMenuView(R.layout.menu_scrollview); 23 24 MenuScrollView msv = (MenuScrollView) mMenuDrawer.getMenuView(); 25 msv.setOnScrollChangedListener(new MenuScrollView.OnScrollChangedListener() { 26 @Override 27 public void onScrollChanged() { 28 mMenuDrawer.invalidate();//为的是“指针标志”能够随着滑动而滑动 29 } 30 /* 31 * 注意:在MenuScrollView中的成员方法 32 * onScrollChanged(int l, int t, int oldl, int oldt)中直接调用invalidate()是不能够达到上面的效果的 33 * **/ 34 }); 35 36 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 37 getActionBar().setDisplayHomeAsUpEnabled(true); 38 } 39 40 mContentTextView = (TextView) findViewById(R.id.contentText); 41 42 findViewById(R.id.item1).setOnClickListener(this); 43 findViewById(R.id.item2).setOnClickListener(this); 44 findViewById(R.id.item3).setOnClickListener(this); 45 findViewById(R.id.item4).setOnClickListener(this); 46 findViewById(R.id.item5).setOnClickListener(this); 47 findViewById(R.id.item6).setOnClickListener(this); 48 49 50 // This will animate the drawer open and closed until the user manually drags it. Usually this would only be 51 // called on first launch. 52 mMenuDrawer.peekDrawer(); 53 } 54 55 @Override 56 public boolean onOptionsItemSelected(MenuItem item) { 57 switch (item.getItemId()) { 58 case android.R.id.home: 59 mMenuDrawer.toggleMenu(); 60 return true; 61 } 62 63 return super.onOptionsItemSelected(item); 64 } 65 66 @Override 67 public void onBackPressed() { 68 final int drawerState = mMenuDrawer.getDrawerState(); 69 if (drawerState == MenuDrawer.STATE_OPEN || drawerState == MenuDrawer.STATE_OPENING) { 70 mMenuDrawer.closeMenu(); 71 return; 72 } 73 74 super.onBackPressed(); 75 } 76 77 @Override 78 public void onClick(View v) { 79 //为当前选中的组件后面添加“指针提示标志” 80 mMenuDrawer.setActiveView(v); 81 mContentTextView.setText("Active item: " + ((TextView) v).getText()); 82 mMenuDrawer.closeMenu(); 83 } 84 }
R.layout.activity_windowsample.xml文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:gravity="center" 6 android:orientation="vertical" 7 android:padding="16dp"> 8 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="@string/sample_window" /> 13 14 <TextView 15 android:id="@+id/contentText" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" /> 18 19 </LinearLayout>
R.layout.menu_scrollview.xml文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <net.simonvt.menudrawer.samples.MenuScrollView 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:id="@+id/scrollView" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:orientation="vertical"> 12 13 <TextView 14 android:id="@+id/item1" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:drawableLeft="@drawable/ic_action_refresh_dark" 18 android:text="Item 1" 19 style="@style/MenuDrawer.Widget.Title" /> 20 21 <TextView 22 android:id="@+id/item2" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:drawableLeft="@drawable/ic_action_select_all_dark" 26 android:text="Item 2" 27 style="@style/MenuDrawer.Widget.Title" /> 28 29 <TextView 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:text="Cat 1" 33 style="@style/MenuDrawer.Widget.Category" /> 34 35 <TextView 36 android:id="@+id/item3" 37 android:layout_width="match_parent" 38 android:layout_height="wrap_content" 39 android:drawableLeft="@drawable/ic_action_refresh_dark" 40 android:text="Item 3" 41 style="@style/MenuDrawer.Widget.Title" /> 42 43 <TextView 44 android:id="@+id/item4" 45 android:layout_width="match_parent" 46 android:layout_height="wrap_content" 47 android:drawableLeft="@drawable/ic_action_select_all_dark" 48 android:text="Item 4" 49 style="@style/MenuDrawer.Widget.Title" /> 50 51 <TextView 52 android:layout_width="match_parent" 53 android:layout_height="wrap_content" 54 android:text="Cat 2" 55 style="@style/MenuDrawer.Widget.Category" /> 56 57 <TextView 58 android:id="@+id/item5" 59 android:layout_width="match_parent" 60 android:layout_height="wrap_content" 61 android:drawableLeft="@drawable/ic_action_refresh_dark" 62 android:text="Item 5" 63 style="@style/MenuDrawer.Widget.Title" /> 64 65 <TextView 66 android:id="@+id/item6" 67 android:layout_width="match_parent" 68 android:layout_height="wrap_content" 69 android:drawableLeft="@drawable/ic_action_select_all_dark" 70 android:text="Item 6" 71 style="@style/MenuDrawer.Widget.Title" /> 72 73 74 <!-- 多添加几个,为了能够使滑动条滚动起来 --> 75 76 <TextView 77 android:layout_width="match_parent" 78 android:layout_height="wrap_content" 79 android:drawableLeft="@drawable/ic_action_select_all_dark" 80 android:text="Item" 81 style="@style/MenuDrawer.Widget.Title" /> 82 <TextView 83 android:layout_width="match_parent" 84 android:layout_height="wrap_content" 85 android:drawableLeft="@drawable/ic_action_select_all_dark" 86 android:text="Item 6" 87 style="@style/MenuDrawer.Widget.Title" /> 88 <TextView 89 android:layout_width="match_parent" 90 android:layout_height="wrap_content" 91 android:drawableLeft="@drawable/ic_action_select_all_dark" 92 android:text="Item 6" 93 style="@style/MenuDrawer.Widget.Title" /> 94 <TextView 95 android:layout_width="match_parent" 96 android:layout_height="wrap_content" 97 android:drawableLeft="@drawable/ic_action_select_all_dark" 98 android:text="Item 6" 99 style="@style/MenuDrawer.Widget.Title" /> 100 <TextView 101 android:layout_width="match_parent" 102 android:layout_height="wrap_content" 103 android:drawableLeft="@drawable/ic_action_select_all_dark" 104 android:text="Item 6" 105 style="@style/MenuDrawer.Widget.Title" /> 106 <TextView 107 android:layout_width="match_parent" 108 android:layout_height="wrap_content" 109 android:drawableLeft="@drawable/ic_action_select_all_dark" 110 android:text="Item 6" 111 style="@style/MenuDrawer.Widget.Title" /> 112 <TextView 113 android:layout_width="match_parent" 114 android:layout_height="wrap_content" 115 android:drawableLeft="@drawable/ic_action_select_all_dark" 116 android:text="Item 6" 117 style="@style/MenuDrawer.Widget.Title" /> 118 119 </LinearLayout> 120 </net.simonvt.menudrawer.samples.MenuScrollView>
MenuScrollView.java 文件
1 package net.simonvt.menudrawer.samples; 2 3 import android.content.Context; 4 import android.util.AttributeSet; 5 import android.widget.ScrollView; 6 7 public class MenuScrollView extends ScrollView { 8 9 public interface OnScrollChangedListener { 10 11 void onScrollChanged(); 12 } 13 14 private OnScrollChangedListener mOnScrollChangedListener; 15 16 public MenuScrollView(Context context) { 17 super(context); 18 } 19 20 public MenuScrollView(Context context, AttributeSet attrs) { 21 super(context, attrs); 22 } 23 24 public MenuScrollView(Context context, AttributeSet attrs, int defStyle) { 25 super(context, attrs, defStyle); 26 } 27 28 //当手指在ScrollView中滑动时,这个方法会自动回调 29 @Override 30 protected void onScrollChanged(int l, int t, int oldl, int oldt) { 31 super.onScrollChanged(l, t, oldl, oldt); 32 if (mOnScrollChangedListener != null) mOnScrollChangedListener.onScrollChanged(); 33 } 34 35 public void setOnScrollChangedListener(OnScrollChangedListener listener) { 36 mOnScrollChangedListener = listener; 37 } 38 }
当然如果能够使用“指针提示标志”,还要在Manifest.xml文件中进行配置theme,参考之前的博文
                    
                
                
            
        
浙公网安备 33010602011771号