Android Custom view to achieve sideslip effect _android

Source: Internet
Author: User

Effect Chart:

Look at the Internet are two view stitching, the default right side does not show, the level of movement when the right view shows. But see the latest version of the effect of QQ is not like this, but the feeling is very good, so shortcoming to one more high imitation.

Knowledge Points:

1, the use of viewdraghelper;
2, the solution of sliding conflict;
3, Custom ViewGroup.

Viewdraghelper out has been more than a long time to believe that we are more familiar with, unfamiliar words Google a lot here is a simple use of its several methods

1, Trycaptureview (View child, int pointerid): Make sure that the view can slide

2, Getviewhorizontaldragrange (View child): In my broken English translation is ' returned is the child View in the horizontal direction of the movable size, in pixels, return 0 when the horizontal direction can not drag '

3. Clampviewpositionhorizontal (View child, int left, int dx): Here you can check the boundaries, and left and DX represent where you're going to move.

4, onviewpositionchanged (View changedview, int left, int top,
int dx, int dy): When you want to capture view, the callback when a position change occurs due to drag or set

Its basic usage is:

Public Swipelayout (context, AttributeSet attrs, int defstyle) {
    Super (context, attrs, defstyle);
    Init ();
  }
  private void Init () {
    Viewdraghelper = Viewdraghelper.create (this, callback);
  }
   public boolean onintercepttouchevent (motionevent ev) {
    Boolean result = Viewdraghelper.shouldintercepttouchevent ( EV);
   }
   public boolean ontouchevent (Motionevent event) {
    viewdraghelper.processtouchevent (event);
    return true;
  }

1), creating in the construction method

2), in the onintercepttouchevent to determine whether to intercept

3), in the ontouchevent out of the event

Well, the most difficult to understand is already done. Next look at the specific implementation:

First look at the layout:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
  android:layout_width= "Match_ Parent "
  android:layout_height=" wrap_content "
  android:orientation=" vertical ">
  < Scrollviewgroup.lly.com.swiplayout.SwipeLayout
    android:id= "@+id/swipelayout"
    android:layout_width= " Match_parent "
    android:layout_height=" wrap_content >
    <!--the layout of the delete area-->
    <include layout= "@layout/layout_delete"/>
    <!--The layout of the item content-->
    <include layout= "@layout/layout_content"/>
  </scrollviewgroup.lly.com.swiplayout.SwipeLayout>
</LinearLayout>

This is nothing to say. A custom viewgroup contains two child controls.

Then see how the Swipelayout is implemented:

@Override
  protected void Onfinishinflate () {
    super.onfinishinflate ();
    Deleteview = Getchildat (0);
    Contentview = Getchildat (1);
  }
  @Override
  protected void onsizechanged (int w, int h, int oldw, int oldh) {
    super.onsizechanged (W, H, OLDW, OLDH); C9/>deleteheight = Deleteview.getmeasuredheight ();
    Deletewidth = Deleteview.getmeasuredwidth ();
    Contentwidth = Contentview.getmeasuredwidth ();
    ScreenWidth = GetWidth ();
  }
  @Override
  protected void OnLayout (Boolean changed, int left, int. top, int right,
              int bottom) {
    //super.onl Ayout (changed, left, top, right, bottom);
    Deleteview.layout (screenwidth-deletewidth, 0, (screenwidth-deletewidth)
        + deletewidth, deleteheight);
    Contentview.layout (0, 0, contentwidth, deleteheight);
  

The above code carries on some initialization operation, the focus looks onlayout inside, we inherit is framelayout here first draws out Deleteview and lets him be on the right, then changes the above layer Contentview, When this is displayed, only the Contentview is displayed.

Next look at the Ontouch method

 public boolean ontouchevent (Motionevent event) {//If currently open, the following logic cannot execute if (!
      Swipelayoutmanager.getinstance (). Isshouldswipe (This)) {requestdisallowintercepttouchevent (true);
    return true;
        Switch (event.getaction ()) {Case MotionEvent.ACTION_DOWN:downX = Event.getx ();
        DownY = Event.gety ();
      Break
        Case Motionevent.action_move://1. Gets the distance that the X and y directions move in float MoveX = Event.getx ();
        float Movey = event.gety (); float Delatx = movex-downx;//x move distance float delaty = movey-downy;//y direction Moving distance if (Math.Abs (DELATX) >mat H.abs (Delaty)) {//means that the move is biased in the horizontal direction, then should be swipelayout should be handled, request the parent view not to intercept requestdisallowintercepttouchevent (true
        );
        }//update downx,downy DOWNX = MoveX;
        DownY = Movey;
      Break
    Case MotionEvent.ACTION_UP:break;
    } viewdraghelper.processtouchevent (event);
  return true; }

The above is to deal with the conflict of events, when it is horizontal move to request the parent view do not intercept.

The next point is coming up.

Private Viewdraghelper.callback Callback = new Viewdraghelper.callback () {@Override public boolean Trycapturevie
    W (View child, int pointerid) {return child==contentview;
    @Override public int Getviewhorizontaldragrange [View child] {return deletewidth;
        @Override public int Clampviewpositionhorizontal (View child, int left, int dx) {if (Child==contentview) {
        if (left>0) left = 0;
      if (left<-deletewidth) left =-deletewidth;
    return to left; @Override public void onviewpositionchanged (View changedview, int left, int top, int dx, in
      T dy) {super.onviewpositionchanged (Changedview, left, top, DX, dy);
        Judge Open and closed logical if (Contentview.getleft () ==0 && currentstate!=swipestate.close) {//description should change state to closed
        CurrentState = Swipestate.close;
        Callback interface Shutdown method if (listener!=null) {listener.onclose (Gettag ()); }//Description of current SWipelayout has been turned off and needs to have the manager clear Swipelayoutmanager.getinstance (). Clearcurrentlayout ();
        }else if (Contentview.getleft () ==-deletewidth && currentstate!=swipestate.open) {//description should change state to open
        CurrentState = Swipestate.open;
        The callback interface opens the method if (listener!=null) {Listener.onopen (Gettag ());
      //The current swipelayout is already open and you need to have the manager record the next swipelayoutmanager.getinstance (). Setswipelayout (Swipelayout.this); @Override public void onviewreleased (View releasedchild, float xvel, float yvel) {Super.onvie
      Wreleased (Releasedchild, Xvel, Yvel);
      if (Contentview.getleft () &LT;-DELETEWIDTH/2) {//The open () should be opened;
      }else {//should be closed close (); }
    }
  };

The method in the above code we all said in the beginning, in the Trycaptureview we let Contentview can slide, in the Getviewhorizontaldragrange but the East sliding range is deletewidth, The bounds are restricted in the clampviewpositionhorizontal, the status is updated in the onviewpositionchanged, and finally the view is automatically rolled back when the finger is raised.

 /**
   * Open method *
  /public void Open () {
    viewdraghelper.smoothslideviewto (contentview,-deletewidth), Contentview.gettop ());
    Viewcompat.postinvalidateonanimation (swipelayout.this);
  }
  /**
   * Closed method */public
  void Close () {
    viewdraghelper.smoothslideviewto (contentview,0), Contentview.gettop ());
    Viewcompat.postinvalidateonanimation (swipelayout.this);
  };
  public void Computescroll () {
    if (viewdraghelper.continuesettling (True)) {
      Viewcompat.postinvalidateonanimation (this);
    }
  

Note Here must rewrite the Computescroll method, otherwise the sliding effect will not move.

This custom framelayout is complete.

But there's a problem, we're sliding up and down in a view that's already sliding, and the view's Deleteview is still showing, so we have to deal with it in the activity:

 Recyview.setonscrolllistener (New Recyclerview.onscrolllistener () {
      @Override public
      Void Onscrollstatechanged (Recyclerview recyclerview, int newstate) {
        super.onscrollstatechanged (RecyclerView, newstate);
      }
      @Override public
      void onscrolled (recyclerview recyclerview, int dx, int dy) {
        super.onscrolled (recyclerview, DX , DY);
        if (dy>0 | | dy<0) {
          swipelayoutmanager.getinstance (). Closecurrentlayout ();}}}
    );

When this recyclerview is sliding up and down, let the child view reset.
Wrap

PS: It was originally implemented in Eclipse ListView, but think that Google has no support for Eclipse, and that ListView is quickly replaced by Recyclerview, so finally switch to Android studio, With Recyclerview to achieve a set.

Source Address

The above is a small set to introduce the Android custom view to achieve sideslip effect, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.