ScrollView nesting solution EditText associated with the slide

EditText slip event ScrollView solution nested, and unite with sliding between both of them. What is it associated with sliding, sliding in the end is when EditText part of the time, then they should be allowed to slide along the outside of ScrollView, so that the sliding event is complete continuity between them. Let's look at the effect.

Online could not find an example of a full implementation, he had his own line and the demo.
Annotated code, all code is as follows:

package chn.fz.thatjay.scrolleditview.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Layout;
import android.util.AttributeSet;
import android.support.v7.widget.AppCompatEditText;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import chn.fz.thatjay.scrolleditview.R;


public class ScrollMulrowsEditText extends AppCompatEditText {

    private final String TAG = "ScMulrowsEditText";
    //滑动距离的最大边界
    private int mOffsetHeight;

    private int mHeight;


    private int mVert = 0;


    public ScrollMulrowsEditText(Context context) {
        super(context);
    }

    public ScrollMulrowsEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttribute(context, attrs, 0);
    }

    public ScrollMulrowsEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr); 
        initAttribute (context, attrs, defStyleAttr); 
    } 


    @Override 
    protected  void onMeasure ( int widthMeasureSpec, int heightMeasureSpec) {
         Super .onMeasure (widthMeasureSpec, heightMeasureSpec); 

        int the paddingTop;
         int paddingBottom;
         int height;
         int mLayoutHeight; 

        // get content pane 
        Layout mLayout = getLayout ();
         // get the contents of the panel height 
        mLayoutHeight = mLayout.getHeight ();
         // Get the padding
        = the paddingTop getTotalPaddingTop ();
         // get the next padding 
        paddingBottom = getTotalPaddingBottom (); 

        // get the actual height of the control 
        height = mHeight; // getHeight () to give first 0, it is preferable to specify the setting from the outside 

        / / calculated when the sliding distance of the boundary mOffsetHeight content less, no scroll bars when a value of 0 
        mOffsetHeight the paddingTop + + = mLayoutHeight paddingBottom - height; 

        setOnTouchListener (); 
        iF (getId () == R.id.edittext2) { 
            Log.d (the TAG, "ffffaaaa onMeasure ==" + mOffsetHeight); 
        } 
    } 

    Private  void initAttribute (the context context, AttributeSet attrs,int defStyleAttr) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ScrollMulrowsEditText, defStyleAttr, 0);
        int count = array.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.ScrollMulrowsEditText_sc_mul_edit_height:
                    mHeight = array.getDimensionPixelSize(attr, 0);
                    break;
            }
        }
        array.recycle (); 
    } 

    @Override 
    protected  void onScrollChanged ( int Horiz, int Vert, int oldHoriz, int oldVert) {
         Super .onScrollChanged (Horiz, Vert, oldHoriz, oldVert); 
        mVert = Vert;
         IF (getId () == R.id.edittext2) { 
            Log.d (the TAG, "ffffaaaa mOffsetHeight ==" + mOffsetHeight + ",, Vert ===" + Vert); 
        } 
        IF (Vert mOffsetHeight == == 0 || Vert ) {
             / / here triggering event slide layout parent or grandfather layout
            getParent () requestDisallowInterceptTouchEvent (. to false ); 
            Log.d (the TAG, "Vert requestDisallowInterceptTouchEvent to false" ); 
        } 
    } 

    // slid onto the upper edge of 
    public  Boolean isUpperEdge () {
         return mVert == 0 ; 
    } 

    // slid to the lower edge of the 
    public  Boolean isLowerEdge () {
         return mVert == mOffsetHeight; 
    } 

    Private  a float scrollBeginY;
     public  void setOnTouchListener () { 
        setOnTouchListener ( new newView.OnTouchListener () { 
            @Override 
            public  Boolean OnTouch (View V, the MotionEvent Event) {
                 // canScrollVertically () method to specify whether the scrolling direction is determined, the argument is positive or negative, negative, check whether to scroll up, positive to check whether the scroll-down 
                iF (MotionEvent.ACTION_DOWN == event.getAction ()) { 
                    scrollBeginY = event.getY (); 
                    . v.getParent () requestDisallowInterceptTouchEvent ( to true ); // requires the layout of the parent class is not a touch event interception 
                    return  to false ; 
                } 
                Log.d (the TAG, "event.getY" + event.getY ()); // EditText if the extreme edge, getY obtained is not a fixed value 
                if(canScrollVertically (. 1)) { // can scroll down 
                    IF (isUpperEdge () && event.getY ()> = scrollBeginY) { // has an upper edge, the slide downward gesture 
                        v.getParent (). requestDisallowInterceptTouchEvent ( to false ) ; // to the parent layout 
                    } the else { 
                        v.getParent () requestDisallowInterceptTouchEvent (. to true ); 
                    } 
                } the else  IF (canScrollVertically (-1)) { // can scroll up 
                    IF (isLowerEdge () && event.getY () < scrollBeginY =) { // has the lower edge, the slide upward gesture
                        v.getParent () requestDisallowInterceptTouchEvent (. to false ); // to the parent layout 
                    } the else { 
                        v.getParent () requestDisallowInterceptTouchEvent (. to true ); 
                    } 
                } the else { 
                    v.getParent () requestDisallowInterceptTouchEvent (. to false ); // to father layout 
                }
                 // getY small phone screen on top getY value
                 // getY phone below the screen is greater getY 
                return  false ; 
            } 
        }); 
    }


}

 

activity codes, the following is ImmersionBar of third-party libraries, used to monitor keyboard input method when disappear, so edittext loses focus, otherwise edittext cursor has been difficult to look at.

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initStatusBar(edittext1, edittext2, edittext3, edittext4, edittext5);
}
public void initStatusBar(final EditText... ets){
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColor(R.color.color1)
                .keyboardEnable(true)
                .setOnKeyboardListener(new OnKeyboardListener() {
                    @Override
                    public void onKeyboardChange(boolean isPopup, int keyboardHeight) {
                        if (!isPopup) {
                            for (EditText et:ets
                            ) {
                                et.clearFocus();
                            }
                        }
                    }
                })
                .init();
    }

 

Complete project code: github Download

 

Guess you like

Origin www.cnblogs.com/that-jay/p/11487535.html