Slide-back layout (similar to iOS Side-slip fallback to previous Activity)

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-family:Arial Black; font-size:18px">Anyone who has used Apple should know that most iOS apps support a slide-back fallback, which is not explained in detail, directly:</span></p></p><p><p></p></p><p><p><span style="font-family:Arial Black; font-size:18px">As an Android developer using ios, I'm particularly fond of this feature, which, in this case, is also implemented on ANDROID.</span></p></p><p><p><span style="font-family:Arial Black; font-size:18px">Idea:</span></p></p><p><p><span style="font-family:Arial Black; font-size:18px">1, to handle the sliding event, and the priority is relatively high, so must be processed in the parent view, that is, our layout of the top view (of course, The top view here is not decorview, only the layout of the first layer view), is generally viewgroup.</span></p></p><p><p><span style="font-family:Arial Black; font-size:18px">2, since it is viewgroup, we have to think, this sliding event we are in which method to deal with, some classmates said, ontouchevent (), OK, We re-analysis, if we in <span style="font-family:‘Arial Black‘; font-size:18px">ontouchevent () processing, Event distribution by View we can know:</span></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px"><br></span></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px">We know this in a sequential way, so we have to choose the preferred method of handling the event, which can be selected from Onintercepttouchevent () and dispatchtouchevent (), where I chose <span style="font-family:‘Arial Black‘; font-size:18px">dispatchtouchevent (), why, I don't know</span> ...</span></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px">3. Principle of realization</span></span></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px">Only the horizontal slide is processed, and the only way to trigger this event is to swipe from the edge, which is actually straightforward and directly on the Code.</span></span></span></p></p><p><p> <span style="font-family:arial black; font-size:18px"><span style="font-family: ' Arial Black '; font-size:18px">< Span style= "font-family: ' Arial Black '; font-size:18px "> </span> </span> </p></p><pre code_snippet_id="1623807" snippet_file_name="blog_20160325_1_6926413" name="code" class="java"> @Override public boolean dispatchtouchevent (motionevent event) {switch (event.getaction ()) {cas E MotionEvent.ACTION_DOWN:startX = Event.getx (); Determines whether to slide from edge//not, This event continues to distribute if (startX <= offset_distance) {return TR Ue } else {super.dispatchtouchevent (event); } case MotionEvent.ACTION_MOVE:if (startX <= offset_distance) {currentx = (int) Event.getx (); Distancex = (int) (currentx-startx); Mscroller.startscroll (-currentx, 0,-distancex, 0); Invalidate (); } break; Case MotionEvent.ACTION_UP:if (startX <= offset_distance) {endx = Event.getx (); Determine if the threshold to close activity is reached if (endx-startx > back_distance) { is through an interface callback if (callback! = Null) {mscroller.startscrol L (-currentx, 0,-(getscreentwidth ()-currentx), 0); Callback.invokeback (); } else {mscroller.startscroll (0, 0, 0, 0); }} else {mscroller.startscroll (0, 0, 0, 0); } Invalidate (); } break; } return super.dispatchtouchevent (event); }</pre><pre code_snippet_id="1623807" snippet_file_name="blog_20160325_2_3306126" name="code" class="java"><pre code_snippet_id="1623807" snippet_file_name="blog_20160325_2_3306126" name="code" class="java"><pre name= "code" class= "java" > public void Setbacklistener (backviewinterface callback) { this.callback = callback; } public interface backviewinterface { void Invokeback (); }</pre></pre><br><br><p><p></p></p><pre><pre></pre></pre>How to Use:<p><p></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px">1. Implement the interface in Baseactivity</span></span></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px"><br></span></span></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px"><br></span></span></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px">2. Use in sub-activity</span></span></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px"><br></span></span></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px; color:#ff0000"><span style="font-family:‘Arial Black‘; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px">Don't forget to register the interface in the child activity</span></span></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px; color:#ff0000"><span style="font-family:‘Arial Black‘; font-size:18px"><span style="font-family:‘Arial Black‘; font-size:18px"><br></span></span></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px">3. Special attention</span></p></p><p><p><span style="font-family:Arial Black; font-size:18px">Since we are just sliding view and the real top view mentioned above is still not moving, it will cause problems, although the top activity of the view sliding, but the back of the slide is <span style="color:#ff0000">all white</span> , this is the real top view of the background color, So speaking of which, you know what to do? Setting the activity theme to transparent is ok, but be careful to set the layout background to White.</span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><br></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><br></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px">It's done,:</span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><br></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><br></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px">The next time to share a simple case of suspension effect:</span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><br></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><br></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px">For details, please pay attention to https://github.com/ray0807</span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><br></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px">https://github.com/ray0807/ShareFramework/blob/master/balloon/simplifyCorelibs/src/main/java/com/corelibs/ Views/splidebacklinearlayout.java<br></span></p></p><p><p><span style="font-family:Arial Black; font-size:18px"><br></span></p></p><p><p>Slide-back layout (similar to iOS Side-slip fallback to previous Activity)</p></p></span>
Related Article

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.