zoukankan      html  css  js  c++  java
  • 89、Android EditText 悬浮停靠

     1 package com.willen.topFloatDemo;
     2 
     3 import android.content.Context;
     4 import android.os.Handler;
     5 import android.util.AttributeSet;
     6 import android.view.MotionEvent;
     7 import android.widget.ScrollView;
     8 
     9 /*
    10  * ScrollView并没有实现滚动监听,所以我们必须自行实现对ScrollView的监听,
    11  * 我们很自然的想到在onTouchEvent()方法中实现对滚动Y轴进行监听
    12  * ScrollView的滚动Y值进行监听
    13  */
    14 public class MyScrollView extends ScrollView {
    15     
    16     private OnScrollListener onScrollListener;  
    17     /** 
    18      * 主要是用在用户手指离开MyScrollView,MyScrollView
    19      * 还在继续滑动,我们用来保存Y的距离,然后做比较 
    20      */  
    21     private int lastScrollY;
    22     
    23     public MyScrollView(Context context) {
    24         super(context, null);
    25     }
    26     public MyScrollView(Context context, AttributeSet attrs) {
    27         super(context, attrs, 0);
    28     }
    29     public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
    30         super(context, attrs, defStyle);
    31     }
    32     
    33     /** 
    34      * 设置滚动接口 
    35      * @param onScrollListener 
    36      */
    37     public void setOnScrollListener(OnScrollListener onScrollListener){
    38         this.onScrollListener = onScrollListener;
    39     }
    40     /** 
    41      * 用于用户手指离开MyScrollView的时候获取MyScrollView滚动的Y距离,然后回调给onScroll方法中 
    42      */  
    43     private Handler handler = new Handler() {  
    44         public void handleMessage(android.os.Message msg) {  
    45             int scrollY = MyScrollView.this.getScrollY();  
    46             //此时的距离和记录下的距离不相等,在隔5毫秒给handler发送消息  
    47             if(lastScrollY != scrollY){  
    48                 lastScrollY = scrollY;  
    49                 handler.sendMessageDelayed(handler.obtainMessage(), 5);    
    50             }  
    51             if(onScrollListener != null){  
    52                 onScrollListener.onScroll(scrollY);  
    53             }  
    54         };  
    55     }; 
    56     
    57     /** 
    58      * 重写onTouchEvent, 当用户的手在MyScrollView上面的时候, 
    59      * 直接将MyScrollView滑动的Y方向距离回调给onScroll方法中,当用户抬起手的时候, 
    60      * MyScrollView可能还在滑动,所以当用户抬起手我们隔5毫秒给handler发送消息,在handler处理 
    61      * MyScrollView滑动的距离 
    62      */ 
    63     @Override
    64     public boolean onTouchEvent(MotionEvent ev) {
    65         if(onScrollListener != null){  
    66             onScrollListener.onScroll(lastScrollY = this.getScrollY());  
    67         }  
    68         switch(ev.getAction()){  
    69         case MotionEvent.ACTION_UP:  
    70              handler.sendMessageDelayed(handler.obtainMessage(), 20);    
    71             break;  
    72         }  
    73         return super.onTouchEvent(ev);
    74     }
    75 
    76     /** 
    77      * 滚动的回调接口 
    78      */  
    79     public interface OnScrollListener{  
    80         // 回调方法, 返回MyScrollView滑动的Y方向距离   
    81         public void onScroll(int scrollY);  
    82     }
    83 }
    package com.willen.topFloatDemo;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.RelativeLayout;
    import com.willen.topFloatDemo.MyScrollView.OnScrollListener;
    
    public class MainActivity extends Activity implements OnScrollListener{
        private EditText search_edit;
        private MyScrollView myScrollView;
        private int searchLayoutTop;
        
        LinearLayout search01,search02;
        RelativeLayout rlayout;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            
            setContentView(R.layout.activity_main);
            
            init();
        }
    
        private void init() {
            search_edit = (EditText)findViewById(R.id.search_edit);
            myScrollView = (MyScrollView)findViewById(R.id.myScrollView);
            search01 = (LinearLayout)findViewById(R.id.search01);
            search02 = (LinearLayout)findViewById(R.id.search02);
            rlayout = (RelativeLayout)findViewById(R.id.rlayout);
            myScrollView.setOnScrollListener(this);  
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);    
            if(hasFocus){  
                searchLayoutTop = rlayout.getBottom();//鑾峰彇searchLayout鐨勯《閮ㄤ綅缃�
            }
        }
    
        //鐩戝惉婊氬姩Y鍊煎彉鍖栵紝閫氳繃addView鍜宺emoveView鏉ュ疄鐜版偓鍋滄晥鏋�
        @Override
        public void onScroll(int scrollY) {
            if(scrollY >= searchLayoutTop){  
                if (search_edit.getParent()!=search01) {
                    search02.removeView(search_edit);
                    search01.addView(search_edit);
                }
            }else{
                if (search_edit.getParent()!=search02) {
                    search01.removeView(search_edit);
                    search02.addView(search_edit);
                }
            }
        }
    }
  • 相关阅读:
    PHP中pack、unpack的详细用法
    Rbac
    composer
    tp5+workman
    apache
    Vs2005安装后没有模板的解决方法
    java中使用mysqldump 备份数据库
    java中文件上传下载将file转为MultipartFile
    hibernate中的schema
    Java之 1.8新特性
  • 原文地址:https://www.cnblogs.com/androidsj/p/5429941.html
Copyright © 2011-2022 走看看