Android-slide back through the latest version of SlidingMenu WeChat 6.2 (2), android Sina Weibo

Source: Internet
Author: User

Android-slide back through SlidingMenu 6.2 latest version of gesture (2), android Sina Weibo

Reprinted please indicate the source:
Http://blog.csdn.net/hanhailong726188/article/details/46453627
This article is from: [Hailong's blog]

I. Overview

In the previous blog post, Android-uses SlidingPaneLayout high imitation 6.2 to send the latest version of gesture slide return (1). In this blog post, we use SlidingPaneLayout to send gesture slide returns, we will use SlidingMenu for high imitation. In fact, the implementation principle is the same, but it is only to replace SlidingPaneLayout with SlidingMenu. However, SlidingMenu itself is more powerful than its own, not only can it achieve sliding return on the same edge, full Screen sliding is also supported. You can also add an animation effect. However, there is not much time for the author's business, and it is not implemented here!

First, let's take a look. One is edge sliding, the other is full screen sliding, and the effect is very good, which is more powerful than what we achieved in the previous blog!

  • Edge slide
  • Full Screen sliding

Next we will start from scratch step by step to achieve this gesture slide return effect, from creating a project to introducing SlidingMenu, then by modifying the Activity theme style, extracting out the parent class BaseSwipeBackActivity that implements SwipeBack, etc.

Ii. Enter the topic below

First, you must create a SlidingMenuSwipeBackDemo project through AndroidStudio, and then move the next SlidingMenu Library to the project, in settings. add the gradle file to this library and build it in the app. gradle adds this library dependency

Settings. gradle

Include ': app'
Include ': SlidingLibrary'

Build. gradle in the app

compile project(":SlidingLibrary")

Here I changed the library name of SlidingMenu to SlidingLibrary, and the project is almost ready. See:

The next step is to copy some of the resource files used in our previous blog, such as the animation resources slide_in_right.xml, slide_out_right.xml, and styles. xml. styles is shown in the beginning. the code of the xml file is as follows:

<style name="JK.SwipeBack.Transparent.Theme" parent="AppTheme">        <item name="android:windowBackground">@android:color/transparent</item>        <item name="android:windowIsTranslucent">true</item>        <item name="android:windowAnimationStyle">@style/JK.Animation.SlidingBack</item>        <item name="android:actionBarStyle">@style/JKActionBar.Custom</item>    </style>    <style name="JKActionBar.Custom" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">        <item name="displayOptions">showCustom</item>        <item name="android:background">@android:color/transparent</item>        <item name="background">@android:color/transparent</item>        <item name="android:displayOptions" tools:ignore="NewApi">showCustom</item>        <item name="android:height">?actionBarSize</item>    </style>    <style name="JK.Animation.SlidingBack" parent="@android:style/Animation.Activity">        <item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item>        <item name="android:activityOpenExitAnimation">@anim/slide_out_right</item>        <item name="android:activityCloseEnterAnimation">@anim/slide_in_right</item>        <item name="android:activityCloseExitAnimation">@anim/slide_out_right</item>        <item name="android:wallpaperOpenEnterAnimation">@anim/slide_in_right</item>        <item name="android:wallpaperOpenExitAnimation">@anim/slide_out_right</item>        <item name="android:wallpaperCloseEnterAnimation">@anim/slide_in_right</item>        <item name="android:wallpaperCloseExitAnimation">@anim/slide_out_right</item>        <item name="android:wallpaperIntraOpenEnterAnimation">@anim/slide_in_right</item>        <item name="android:wallpaperIntraOpenExitAnimation">@anim/slide_out_right</item>        <item name="android:wallpaperIntraCloseEnterAnimation">@anim/slide_in_right</item>        <item name="android:wallpaperIntraCloseExitAnimation">@anim/slide_out_right</item>    </style>

Next, we will create the BaseSwipeBackActivity base class. All the classes that implement gesture slide return inherit from it and it will be OK. If you do not need gesture slide, you don't need to inherit it. Now, let's look at the BaseSwipeBackActivity source code file, because the code comments are more detailed, I directly paste the code here, the code is very simple

Package com. hhl. slidingmenuswipebackdemo; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. view. keyEvent; import android. view. view; import android. view. viewGroup; import com. jeremyfeinstein. slidingmenu. lib. slidingMenu; import com. jeremyfeinstein. slidingmenu. lib. app. slidingActivityHelper;/*** Created by hailonghan on 15/6/11. */public abstract class BaseSwipeBackAc Tianyuan extends AppCompatActivity implements SlidingMenu. onOpenedListener {private SlidingActivityHelper mHelper; // SlidingMenu private SlidingMenu mSlidingMenu; @ Override protected void onCreate (Bundle savedInstanceState) {mHelper = new listener (this); mHelper. onCreate (savedInstanceState); // here, the setBehindContentView method of SlidingMenu is used to set a transparent menu View behindView = new View (this); behindVie W. setLayoutParams (new ViewGroup. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, ViewGroup. layoutParams. MATCH_PARENT); behindView. setBackgroundColor (getResources (). getColor (android. r. color. transparent); setBehindContentView (behindView); mSlidingMenu = getSlidingMenu (); // set the shadow width to 10 px mSlidingMenu. setShadowWidth (10); // sets the shadow mSlidingMenu. setShadowDrawable (R. drawable. slide_shadow); // set the following layout, that is, we The transparency menu defined above is 0 from the edge of the right screen, that is, after sliding, the menu will display mSlidingMenu on the full screen. setBehindOffset (0); mSlidingMenu. setFadeDegree (0.35f); // listener for opening the menu, because after the menu is opened, we need to finish the current Activity mSlidingMenu. setOnOpenedListener (this); // set the sliding direction of the gesture, because we want to achieve the right sliding effect, set it to SlidingMenu. LEFT mode mSlidingMenu. setMode (SlidingMenu. LEFT); // because only the edges slide, we set it to TOUCHMODE_MARGIN mode. If you want to slide the full screen, you only need to change this to TOUCHMODE_FULLSCREEN to OK mSlidingMenu. setTouchModeAbove (sregistringmenu. T OUCHMODE_MARGIN); super. onCreate (savedInstanceState);} @ Override public void onPostCreate (Bundle savedInstanceState) {super. onPostCreate (savedInstanceState); mHelper. onPostCreate (savedInstanceState) ;}@ Override public boolean onSupportNavigateUp () {return true ;}@ Override public View findViewById (int id) {View v = super. findViewById (id); if (v! = Null) return v; return mHelper. findViewById (id) ;}@ Override protected void onSaveInstanceState (Bundle outState) {super. onSaveInstanceState (outState); mHelper. onSaveInstanceState (outState) ;}@ Override public void setContentView (int id) {setContentView (getLayoutInflater (). inflate (id, null);} @ Override public void setContentView (View v) {setContentView (v, new ViewGroup. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, ViewGroup. layoutParams. MATCH_PARENT);} @ Override public void setContentView (View v, ViewGroup. layoutParams params) {super. setContentView (v, params); mHelper. registerAboveContentView (v, params);} public void setBehindContentView (int id) {setBehindContentView (getLayoutInflater (). inflate (id, null);} public void setBehindContentView (View v) {setBehindContentView (v, new ViewGroup. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, ViewGroup. layoutParams. MATCH_PARENT);} public void setBehindContentView (View v, ViewGroup. layoutParams params) {mHelper. setBehindContentView (v, params);} public sjavasingmenu getsjavasingmenu () {return mHelper. getSlidingMenu ();} public void toggle () {mHelper. toggle ();} public void showContent () {mHelper. showContent ();} public void showMenu () {mHelper. showMenu ();} public void showSecondaryMenu () {mHelper. showSecondaryMenu ();} public void setSlidingActionBarEnabled (boolean B) {mHelper. setSlidingActionBarEnabled (B) ;}@ Override public boolean onKeyUp (int keyCode, KeyEvent event) {boolean B = mHelper. onKeyUp (keyCode, event); if (B) return B; return super. onKeyUp (keyCode, event);} // After the menu is fully open, the current Activity @ Override public void onOpened () {this. finish () ;}@ Override public void finish () {super. finish (); this. overridePendingTransition (0, R. anim. slide_out_right );}}

Note that if you want to return the result by sliding the edge, you only need to change the sliding mode of SlidingMenu to TOUCHMODE_MARGIN. If you want to slide the screen, you can change it to TOUCHMODE_FULLSCREEN.

It's almost the same as writing it. I don't feel difficult. I just want to change the style of the Activity to transparent, extract a parent class BaseSwipeBackActivity, and add SlidingMenu to the parent class, set the left menu to transparent. After the menu is fully open, finish the current Activity.

The github source code is attached.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.