ScrollView做的上拉效果



Java代码   收藏代码
  1. import android.content.Context;  
  2. import android.util.AttributeSet;  
  3. import android.view.MotionEvent;  
  4. import android.widget.ScrollView;  
  5.   
  6. public class MyScrollView extends ScrollView {  
  7.   
  8.     // 滚动监听接口  
  9.     private OnScrollChangedListeneer onScrollChangedListeneer;  
  10.   
  11.     public MyScrollView(Context context) {  
  12.         super(context);  
  13.         // TODO Auto-generated constructor stub  
  14.     }  
  15.   
  16.     public MyScrollView(Context context, AttributeSet attrs) {  
  17.         super(context, attrs);  
  18.         // TODO Auto-generated constructor stub  
  19.     }  
  20.   
  21.     public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {  
  22.         super(context, attrs, defStyleAttr);  
  23.         // TODO Auto-generated constructor stub  
  24.     }  
  25.   
  26.     @Override  
  27.     public boolean onTouchEvent(MotionEvent ev) {  
  28.         // TODO Auto-generated method stub  
  29.         // 屏蔽touch事件,才能在监听其子控件的touch事件  
  30.         super.onTouchEvent(ev);  
  31.         return false;  
  32.     }  
  33.   
  34.     @Override  
  35.     public boolean onInterceptTouchEvent(MotionEvent event) {  
  36.         // 屏蔽touch事件传递,才能在监听其子控件的touch事件  
  37.         super.onInterceptTouchEvent(event);  
  38.         return false;  
  39.     }  
  40.   
  41.     @Override  
  42.     protected void onScrollChanged(int l, int t, int oldl, int oldt) {  
  43.         // TODO Auto-generated method stub  
  44.         super.onScrollChanged(l, t, oldl, oldt);  
  45.         if (onScrollChangedListeneer != null) {  
  46.             onScrollChangedListeneer.onScrollChanged(l, t, oldl, oldt);  
  47.         }  
  48.     }  
  49.   
  50.     // 滚动事件监听,获取滚动的距离,用户处理一些其他事  
  51.     public interface OnScrollChangedListeneer {  
  52.         public void onScrollChanged(int l, int t, int oldl, int oldt);  
  53.     }  
  54.   
  55.     public void setOnScrollChangedListeneer(  
  56.             OnScrollChangedListeneer onScrollChangedListeneer) {  
  57.         this.onScrollChangedListeneer = onScrollChangedListeneer;  
  58.     }  
  59.   
  60. }  


Java代码   收藏代码
  1. import android.app.Activity;  
  2. import android.content.Context;  
  3. import android.graphics.Point;  
  4. import android.os.Bundle;  
  5. import android.util.DisplayMetrics;  
  6. import android.util.Log;  
  7. import android.view.MotionEvent;  
  8. import android.view.View;  
  9. import android.view.View.OnTouchListener;  
  10. import android.view.WindowManager;  
  11. import android.widget.RelativeLayout.LayoutParams;  
  12.   
  13. import com.mb.door.MyScrollView.OnScrollChangedListeneer;  
  14. import com.ywl5320.scrollanima.R;  
  15.   
  16. public class MainActivity extends Activity {  
  17.   
  18.     private View layout_content;  
  19.     private MyScrollView scrollView;  
  20.     private int offsetsum = 0;// 总的手指滑动距离  
  21.     private Point point = new Point();  
  22.     private View layout_sliding;  
  23.   
  24.     private boolean isOpen = false// true:显示详情 false 反之  
  25.     private int screenHeight = 0;  
  26.   
  27.     private int handlerHeight = 100;// 把手的高度,该高度最好动态获取  
  28.     private int threshold=300;   
  29.     private Context context;  
  30.   
  31.     @Override  
  32.     protected void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.activity_main);  
  35.         context = this;  
  36.         initViews();  
  37.     }  
  38.   
  39.     private void initViews(){  
  40.         handlerHeight = dip2px(context, 100);// 把手的高度,该高度最好动态获取  
  41.         int statusBarHeight=getStatusBarHeight();  
  42.           
  43.         scrollView = (MyScrollView) findViewById(R.id.scrollView);  
  44.         layout_content = findViewById(R.id.layout_content);  
  45.         layout_sliding = findViewById(R.id.layout_sliding);  
  46.   
  47.         // 设置滑动层为屏幕高度  
  48.         LayoutParams lp = (LayoutParams) layout_content.getLayoutParams();  
  49.         screenHeight = getScreenHeight();  
  50.         lp.height = screenHeight - statusBarHeight;  
  51.         layout_content.setLayoutParams(lp);  
  52.   
  53.         // 设置详细层的高度:等于屏幕高度-状态栏高度-阴影提示高度  
  54.         LayoutParams lp2 = (LayoutParams) layout_sliding.getLayoutParams();  
  55.         lp2.height = screenHeight  - statusBarHeight- handlerHeight;  
  56.         layout_sliding.setLayoutParams(lp2);  
  57.   
  58.         // 为上层添加touch事件,控制详情页显示隐藏  
  59.         layout_content.setOnTouchListener(new OnTouchListener() {  
  60.   
  61.             @Override  
  62.             public boolean onTouch(View v, MotionEvent event) {  
  63.                 // TODO Auto-generated method stub  
  64.                 int action = event.getAction();  
  65.                 int offsety = 0;  
  66.                 int y = 0;  
  67.                 switch (action) {  
  68.                 case MotionEvent.ACTION_DOWN:  
  69.                     point.y = (int) event.getRawY();  
  70.                     offsetsum = 0;  
  71.                     break;  
  72.                 case MotionEvent.ACTION_MOVE:  
  73.                     y = (int) event.getRawY();  
  74.                     offsety = y - point.y;  
  75.                     offsetsum += offsety;  
  76.                     point.y = (int) event.getRawY();  
  77.                     scrollView.scrollBy(0, -offsety);  
  78.                     break;  
  79.                 case MotionEvent.ACTION_UP:  
  80.                     if(offsetsum==0){  
  81.                         return true;  
  82.                     }  
  83.                     if (offsetsum > 0) {// offsetsum大于0时是往下拉  
  84.                         if (offsetsum > threshold) {  
  85.                             close();  
  86.                         } else {  
  87.                             open();  
  88.                         }  
  89.                     } else {// offsetsum小于0时是往上拉  
  90.                         if (offsetsum < -threshold) {  
  91.                             open();  
  92.                         } else {  
  93.                             close();  
  94.                         }  
  95.                     }  
  96.   
  97.                     break;  
  98.                 }  
  99.                 return true;  
  100.             }  
  101.         });  
  102.   
  103.         scrollView.setOnScrollChangedListeneer(new OnScrollChangedListeneer() {  
  104.   
  105.             @Override  
  106.             public void onScrollChanged(int l, int t, int oldl, int oldt) {  
  107.                 // TODO Auto-generated method stub  
  108.                 Log.i("tag", l + "--" + t + "--" + oldl + "--"+ oldt);  
  109.             }  
  110.         });  
  111.     }  
  112.       
  113.     private void open() {  
  114.         scrollView.smoothScrollTo(0, screenHeight - handlerHeight);  
  115.         isOpen = true;  
  116.     }  
  117.   
  118.     private void close() {  
  119.         scrollView.smoothScrollTo(00);  
  120.         isOpen = false;  
  121.     }  
  122.   
  123.     public void toggle(){  
  124.         if(isOpen){  
  125.             close();  
  126.         }else{  
  127.             open();  
  128.         }  
  129.     }  
  130.       
  131.     /** 
  132.      * 获取屏幕高度 
  133.      *  
  134.      * @return 
  135.      */  
  136.     public int getScreenHeight() {  
  137.         WindowManager wManager = (WindowManager) getApplicationContext()  
  138.                 .getSystemService(Context.WINDOW_SERVICE);  
  139.         DisplayMetrics dm = new DisplayMetrics();  
  140.         wManager.getDefaultDisplay().getMetrics(dm);  
  141.         return dm.heightPixels;  
  142.     }  
  143.   
  144.     /** 
  145.      * dip转换为px 
  146.      *  
  147.      * @param context 
  148.      * @param dipValue 
  149.      * @return 
  150.      */  
  151.     public int dip2px(Context context, float dipValue) {  
  152.         final float scale = context.getResources().getDisplayMetrics().density;  
  153.         return (int) (dipValue * scale + 0.5f);  
  154.     }  
  155.   
  156.     /** 
  157.      * 获取状态栏高度 
  158.      *  
  159.      * @return 
  160.      */  
  161.     public int getStatusBarHeight() {  
  162.         int result = 0;  
  163.         int resourceId = getResources().getIdentifier("status_bar_height",  
  164.                 "dimen""android");  
  165.         if (resourceId > 0) {  
  166.             result = getResources().getDimensionPixelSize(resourceId);  
  167.         }  
  168.         return result;  
  169.     }  
  170.   
  171. }  


Xml代码   收藏代码
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="${relativePackage}.${activityClass}" >  
  6.   
  7.     <com.mb.door.MyScrollView  
  8.         android:id="@+id/scrollView"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:scrollbars="none" >  
  12.   
  13.         <RelativeLayout  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="match_parent" >  
  16.   
  17.             <FrameLayout  
  18.                 android:id="@+id/layout_content"  
  19.                 android:layout_width="match_parent"  
  20.                 android:layout_height="match_parent" >  
  21.   
  22.                 <FrameLayout  
  23.                     android:layout_width="match_parent"  
  24.                     android:layout_height="match_parent"  
  25.                     android:layout_gravity="center"  
  26.                     android:background="@android:color/holo_blue_dark" >  
  27.                 </FrameLayout>  
  28.   
  29.                 <TextView  
  30.                     android:id="@+id/handler"  
  31.                     android:layout_width="match_parent"  
  32.                     android:layout_height="100dip"  
  33.                     android:layout_gravity="bottom"  
  34.                     android:background="#aa000000"  
  35.                     android:gravity="center"  
  36.                     android:text="上滑查看详情"  
  37.                     android:textColor="#ffffff" />  
  38.             </FrameLayout>  
  39.   
  40.             <FrameLayout   
  41.                 android:id="@+id/layout_sliding"  
  42.                 android:layout_width="match_parent"  
  43.                 android:layout_height="match_parent"  
  44.                 android:layout_below="@id/layout_content"  
  45.                 android:background="@android:color/white"  
  46.                 >  
  47.             </FrameLayout>  
  48.               
  49.         </RelativeLayout>  
  50.     </com.mb.door.MyScrollView>  
  51.   
  52. </RelativeLayout>  


事实上,同样的效果可以使用 ViewDragHelper实现
http://gundumw100.iteye.com/blog/2114716
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值