QQ left-side sliding display button switch, qq left-side sliding Switch
The previous article introduced how to set Custom Attributes. In this article, I will introduce how to use buttons to control the display and hiding of menus, to achieve this effect, we need to add three methods in SlidingMenu to achieve the above effect.
Our SlidingMenu code:
Public class SlidingMenu extends HorizontalScrollView {private LinearLayout mWapper; private ViewGroup mMenu; private ViewGroup mContent; private int mScreenWidth; // screen width private int mMenuWidth; // set the Menu width // dp private int mMenuRightPadding; private boolean once = false; private boolean isOpen = false; // indicates the Menu status: hide or show public SlidingMenu (Context context) {this (context, null);}/*** called when custom attributes are not used This method * @ param context * @ param attrs */public SlidingMenu (Context context, AttributeSet attrs) {this (context, attrs, 0 );} /*** call * @ param context * @ param attrs * @ param defStyle */public SlidingMenu (Context context, AttributeSet attrs, int defStyle) when a custom style is used) {super (context, attrs, defStyle); // gets the defined attribute TypedArray a = context. getTheme (). obtainStyledAttributes (attrs, R. styleable. slidingMen U, defStyle, 0); int n =. getIndexCount (); // get the set number of custom attributes for (int I = 0; I <n; I ++) {int attr =. getIndex (I); switch (attr) {case R. styleable. slidingMenu_rightPadding: mMenuRightPadding =. getDimensionPixelSize (attr, (int) TypedValue. applyDimension (TypedValue. COMPLEX_UNIT_DIP, 50, context. getResources (). getDisplayMetrics (); break;}. recycle (); WindowManager wm = (WindowManager) context. getSy StemService (context. WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics (); wm. getdefadisplay display (). getMetrics (outMetrics); mScreenWidth = outMetrics. widthPixels;}/*** set the internal View width and height, as well as its own width and height */@ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); if (! Once) {mWapper = (LinearLayout) getChildAt (0); mMenu = (ViewGroup) mWapper. getChildAt (0); mContent = (ViewGroup) mWapper. getChildAt (1); mMenuWidth = mMenu. getLayoutParams (). width = mScreenWidth-mMenuRightPadding; mContent. getLayoutParams (). width = mScreenWidth; once = true;}/*** set the position of the sub-View * Hide Menu */@ Override protected void onLayout (boolean changed, int l, int t, int r, int B) {super. onLayout (changed, l, t, r, B); if (changed) {this. scrollTo (mMenuWidth, 0) ;}}/*** controls the sliding effect of the finger */@ Override public boolean onTouchEvent (MotionEvent ev) {int action = ev. getAction (); switch (action) {case MotionEvent. ACTION_UP: int scrollx = getScrollX (); // The hidden area width on the left of the Menu if (scrollx> = mMenuWidth/2) {this. smoothScrollTo (mMenuWidth, 0); isOpen = false;} else {this. smoothScrollTo (0, 0); isOpen = true;} return super. onTouchEvent (ev);}/*** Open menu */public void OpenMenu () {if (isOpen) {return;} else {this. smoothScrollTo (0, 0); isOpen = true ;}/ *** close menu */public void CloseMenu () {if (isOpen) {this. smoothScrollTo (mMenuWidth, 0); isOpen = false;} else {return;}/*** switch menu */public void Toggle () {if (isOpen) {CloseMenu () ;}else {OpenMenu ();}}}
Different from the previous code, I have already made a special note for you. It is not hard to understand.
Layout file code:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" xmlns: hyman = "http://schemas.android.com/apk/res/com.example.android_qq_view" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <com. example. menu. slidingMenu android: id = "@ + id/slidingMenu" android: layout_width = "match_parent" android: layout_height = "match_parent" hyman: rightPadding = "100dp"> <LinearLayout android: layout_width = "wrap_content" android: layout_height = "match_parent" android: orientation = "horizontal"> <include layout = "@ layout/left_menu"/> <LinearLayout android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "@ drawable/qq"> <Button android: id = "@ + id/button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "_height ="/> </LinearLayout> </com. example. menu. slidingMenu> </RelativeLayout>
Our MainActivity:
public class MainActivity extends Activity { private Button button; private SlidingMenu sm ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); sm = (SlidingMenu) findViewById(R.id.slidingMenu); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { sm.Toggle(); } }); }}
Here, we have achieved the switching effect.