android list弹性滑动 2.3以下的实现 的两个方法 (备份)

来源:互联网 发布:u盘mac系统安装教程 编辑:程序博客网 时间:2024/06/06 00:34

方法一:

弹性滑动的效果比较好看。但是 在2.2以下版本中,android 本身没有实现,想要实现这中效果要自己去写

前几天在iteye上看见了一个博客给我很大的启示,于是自己动手实验了一下感觉还可以。在这里把我的源码给大家贴上让大家一起分享,希望大家也能提出更好的意见。废话不多说上源码:

1.自己些一个MyListview  继承listview 类:

view plaincopy to clipboardprint?
  1. package com.wljie;  
  2. import android.content.Context;  
  3. import android.graphics.Rect;  
  4. import android.util.AttributeSet;  
  5. import android.util.Log;  
  6. import android.view.GestureDetector;  
  7. import android.view.MotionEvent;  
  8. import android.view.View;  
  9. import android.view.GestureDetector.OnGestureListener;  
  10. import android.view.animation.TranslateAnimation;  
  11. import android.widget.ListView;  
  12. public class MyListView extends ListView {  
  13. private Context context;  
  14. private boolean outBound = false;  
  15. private int distance;  
  16. private int firstOut;  
  17. private static final String TAG = "wljie";  
  18. public MyListView(Context context, AttributeSet attrs) {  
  19. super(context, attrs);  
  20. this.context = context;  
  21. Log.d(TAG, "IN 1");  
  22. }  
  23. public MyListView(Context context, AttributeSet attrs, int defStyle) {  
  24. super(context, attrs, defStyle);  
  25. this.context = context;  
  26. Log.d(TAG, "IN 2");  
  27. }  
  28. public MyListView(Context context) {  
  29. super(context);  
  30. this.context = context;  
  31. Log.d(TAG, "IN 3");  
  32. }  
  33. GestureDetector gestureDetector = new GestureDetector(  
  34. new OnGestureListener() {  
  35. @Override  
  36. public boolean onSingleTapUp(MotionEvent e) {  
  37. // TODO Auto-generated method stub  
  38. return false;  
  39. }  
  40. @Override  
  41. public void onShowPress(MotionEvent e) {  
  42. // TODO Auto-generated method stub  
  43. }  
  44. /** 
  45. * 手势滑动的时候触发 
  46. */  
  47. @Override  
  48. public boolean onScroll(MotionEvent e1, MotionEvent e2,  
  49. float distanceX, float distanceY) {  
  50. Log.d(TAG, "ENTER onscroll");  
  51. int firstPos = getFirstVisiblePosition();  
  52. int lastPos = getLastVisiblePosition();  
  53. int itemCount = getCount();  
  54. // outbound Top  
  55. if (outBound && firstPos != 0 && lastPos != (itemCount - 1)) {  
  56. scrollTo(00);  
  57. return false;  
  58. }  
  59. View firstView = getChildAt(firstPos);  
  60. if (!outBound)  
  61. firstOut = (int) e2.getRawY();  
  62. if (firstView != null  
  63. && (outBound || (firstPos == 0  
  64. && firstView.getTop() == 0 && distanceY < 0))) {  
  65. // Record the length of each slide  
  66. distance = firstOut - (int) e2.getRawY();  
  67. scrollTo(0, distance / 2);  
  68. return true;  
  69. }  
  70. // outbound Bottom  
  71. return false;  
  72. }  
  73. @Override  
  74. public void onLongPress(MotionEvent e) {  
  75. // TODO Auto-generated method stub  
  76. }  
  77. @Override  
  78. public boolean onFling(MotionEvent e1, MotionEvent e2,  
  79. float velocityX, float velocityY) {  
  80. // TODO Auto-generated method stub  
  81. return false;  
  82. }  
  83. @Override  
  84. public boolean onDown(MotionEvent e) {  
  85. // TODO Auto-generated method stub  
  86. return false;  
  87. }  
  88. });  
  89. @Override  
  90. public boolean dispatchTouchEvent(MotionEvent event) {  
  91. Log.d(TAG, "dispatchTouchEvent");  
  92. int act = event.getAction();  
  93. if ((act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_CANCEL)  
  94. && outBound) {  
  95. outBound = false;  
  96. // scroll back  
  97. }  
  98. if (!gestureDetector.onTouchEvent(event)) {  
  99. outBound = false;  
  100. else {  
  101. outBound = true;  
  102. }  
  103. Rect rect = new Rect();    
  104.         getLocalVisibleRect(rect);    
  105.         TranslateAnimation am = new TranslateAnimation( 00, -rect.top, 0);    
  106.         am.setDuration(300);    
  107.         startAnimation(am);    
  108.         scrollTo(00);  
  109. return super.dispatchTouchEvent(event);  
  110. }  
  111. }  
 

2.在main.xml 中写入

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout     
  3.         android:id="@+id/LinearLayout01"     
  4.         android:layout_width="fill_parent"     
  5.         android:layout_height="fill_parent"     
  6.         xmlns:android="http://schemas.android.com/apk/res/android"  
  7.         >    
  8.         <com.wljie.MyListView  
  9.         android:layout_width="wrap_content"     
  10.                   android:layout_height="wrap_content"     
  11.                   android:id="@+id/MyListView"/>    
  12. </LinearLayout>  
 

3.创建一个my_listitem.xml 

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout     
  3.         android:layout_width="fill_parent"     
  4.         xmlns:android="http://schemas.android.com/apk/res/android"     
  5.         android:orientation="vertical"    
  6.         android:layout_height="wrap_content"     
  7.         android:id="@+id/MyListItem"     
  8.         android:paddingBottom="3dip"     
  9.         android:paddingLeft="10dip">    
  10.         <TextView     
  11.                 android:layout_height="wrap_content"     
  12.                 android:layout_width="fill_parent"     
  13.                 android:id="@+id/ItemTitle"     
  14.                 android:textSize="30dip">    
  15.         </TextView>    
  16.         <TextView     
  17.                 android:layout_height="wrap_content"     
  18.                 android:layout_width="fill_parent"     
  19.                 android:id="@+id/ItemText">    
  20.         </TextView>   
  21. </LinearLayout>  
 

4.在Main.java中进行下一步的显示

view plaincopy to clipboardprint?
  1. package com.wljie;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import android.app.Activity;  
  5. import android.graphics.Rect;  
  6. import android.os.Bundle;  
  7. import android.view.animation.TranslateAnimation;  
  8. import android.widget.SimpleAdapter;  
  9. public class Main extends Activity {  
  10. /** Called when the activity is first created. */  
  11. @Override  
  12. public void onCreate(Bundle savedInstanceState) {  
  13. super.onCreate(savedInstanceState);  
  14. setContentView(R.layout.main);  
  15. // 绑定XML中的ListView,作为Item的容器  
  16. MyListView list = (MyListView) findViewById(R.id.MyListView);  
  17. // 生成动态数组,并且转载数据  
  18. ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();  
  19. for (int i = 0; i < 30; i++) {  
  20. HashMap<String, String> map = new HashMap<String, String>();  
  21. map.put("ItemTitle""This is Title.....");  
  22. map.put("ItemText""This is text.....");  
  23. mylist.add(map);  
  24. }  
  25. // 生成适配器,数组===》ListItem  
  26. SimpleAdapter mSchedule = new SimpleAdapter(this// 没什么解释  
  27. mylist,// 数据来源  
  28. R.layout.my_listitem,// ListItem的XML实现  
  29. // 动态数组与ListItem对应的子项  
  30. new String[] { "ItemTitle""ItemText" },  
  31. // ListItem的XML文件里面的两个TextView ID  
  32. new int[] { R.id.ItemTitle, R.id.ItemText });  
  33. // 添加并且显示  
  34. list.setAdapter(mSchedule);  
  35.    
  36. }  




方法二:可能跟方法一类似,还没仔细看 仅作备份

最近项目中用到了弹性滑动,但是这个弹性滑动只有在2.3之后才有了功能函数,在2.2以及之前的版本中,只有自己去实现了。
    查了一下网上的资源,貌似没有提供出来一个具体的方式来实现。看到一个牛人写的一个软件中实现了弹性滑动,查了查源码,然后看了看ListView的源码,然后自己搞了一下,实现了弹性滑动。
    基本思路就是得到出当前的可显示的item的位置,然后判断出否是越界,这里的越界就是是否有过度的滑动。如果有的话,就利用ScrollTo()这个方法,先把控件滑动到手势触摸事件的位置,当触摸事件结束时,滑动到屏幕顶端,或者末端。
    首先是自定义一个类,继承ListView,然后在其中加入手势的事件模型。在处理touch事件时将事件交给手势listener来处理。并且返回父类的处理结果。
   
Java代码  收藏代码
  1. public class BouncyListView extends ListView {  
  2.   
  3.     private Context context;  
  4.     /* 
  5.      * Slide over so that cross-border 
  6.      */  
  7.     private boolean outBound = false;  
  8.     private int distance;  
  9.     private int firstOut;  
  10.   
  11.     public BouncyListView(Context context) {  
  12.         super(context);  
  13.         this.context = context;  
  14.     }  
  15.   
  16.     public BouncyListView(Context context, AttributeSet attrs) {  
  17.         super(context, attrs);  
  18.         this.context = context;  
  19.     }  
  20.   
  21.     public BouncyListView(Context context, AttributeSet attrs, int defStyle) {  
  22.         super(context, attrs, defStyle);  
  23.         this.context = context;  
  24.     }  
  25.   
  26.     GestureDetector mGestureDetector = new GestureDetector(context,  
  27.             new GestureDetector.OnGestureListener() {  
  28.   
  29.                 @Override  
  30.                 public boolean onSingleTapUp(MotionEvent e) {  
  31.                     return false;  
  32.                 }  
  33.   
  34.                 @Override  
  35.                 public void onShowPress(MotionEvent e) {  
  36.   
  37.                 }  
  38.   
  39.                 @Override  
  40.                 public boolean onScroll(MotionEvent e1, MotionEvent e2,  
  41.                         float distanceX, float distanceY) {  
  42.                     int firstPos = getFirstVisiblePosition();  
  43.                     int lastPos = getLastVisiblePosition();  
  44.                     int itemCount = getCount();  
  45.                     //outbound Top  
  46.                     if (outBound && firstPos != 0 && lastPos != (itemCount - 1)) {  
  47.                         scrollTo(00);  
  48.                         return false;  
  49.                     }  
  50.                     View firstView = getChildAt(firstPos);  
  51.                     if (!outBound)  
  52.                         firstOut = (int) e2.getRawY();  
  53.                     if (firstView != null  
  54.                             && (outBound || (firstPos == 0  
  55.                                     && firstView.getTop() == 0 && distanceY < 0))) {  
  56.                         //Record the length of each slide  
  57.                         distance = firstOut - (int) e2.getRawY();  
  58.                         scrollTo(0, distance/2);  
  59.                         return true;  
  60.                     }  
  61.                     //outbound Bottom  
  62.                        
  63.   
  64.                     return false;  
  65.                 }  
  66.   
  67.                 @Override  
  68.                 public void onLongPress(MotionEvent e) {  
  69.   
  70.                 }  
  71.   
  72.                 @Override  
  73.                 public boolean onFling(MotionEvent e1, MotionEvent e2,  
  74.                         float velocityX, float velocityY) {  
  75.                     return false;  
  76.                 }  
  77.   
  78.                 @Override  
  79.                 public boolean onDown(MotionEvent e) {  
  80.                     return false;  
  81.                 }  
  82.             });  
  83.   
  84.     @Override  
  85.     public boolean dispatchTouchEvent(MotionEvent event) {  
  86.   
  87.         int act = event.getAction();  
  88.         if ((act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_CANCEL)  
  89.                 && outBound) {  
  90.             outBound = false;  
  91.             //scroll back  
  92.         }  
  93.         if (!mGestureDetector.onTouchEvent(event)) {  
  94.             outBound = false;  
  95.         } else {  
  96.             outBound = true;  
  97.         }  
  98.         return super.dispatchTouchEvent(event);  
  99.     }  
  100.   
  101. }  


    实现过度滑动,item跟随手势的代码是这一段:
Java代码  收藏代码
  1. int firstPos = getFirstVisiblePosition();  
  2.                     int lastPos = getLastVisiblePosition();  
  3.                     int itemCount = getCount();  
  4.   
  5.                     if (outBound && firstPos != 0  
  6.                             && lastPos != (itemCount - 1)) {  
  7.                         scrollTo(00);  
  8.                         return false;  
  9.                     }  
  10.                     View firstView = getChildAt(firstPos);  
  11.   
  12.                     if (!outBound)  
  13.                         firstOut = (int) e2.getRawY();  
  14.                     if (firstView != null && (outBound ||   
  15.                             (firstPos == 0 && firstView.getTop() == 0 && distanceY < 0))){  
  16.                           
  17.                         distance = firstOut - (int) e2.getRawY();  
  18.                         scrollTo(0, distance/2);  
  19.                         return true;  
  20.                     }  


    scrollTo(0, distance/2);使得item只是过度滑动你所滑动距离的一半,这样更美观。
   
    而实现弹性滑动的基本思路就是滑动回去加个时间。
    代码如下:
   
Java代码  收藏代码
  1. Rect rect = new Rect();  
  2.             getLocalVisibleRect(rect);  
  3.             TranslateAnimation am = new TranslateAnimation( 00, -rect.top, 0);  
  4.             am.setDuration(300);  
  5.             startAnimation(am);  
  6.                         scrollTo(00);  

    这个代码是从项目中摘出的。只有上面的弹性滑动,下边部分其实一样。
    显示效果如下


    看别人的代码往往会给自己带来一些思路,然后自己去研究,以上的思路可以应用与Gridview,我这里实现Gridview的弹性滑动是没有问题的。我目前遇到的一个问题就是ScrollView的弹性滑动问题。看了很长时间的ScrollView的源码,但是没有太好的思路,主要是在显示部分的坐标拿不到,不像ListView里可以通过getFirstVisiblePosition()这样一个方法来拿到item的位置,继而拿到item,ScrollView中只有一个子控件。他的滑动实现貌似跟ListView,Gridview这些采用Adapter的不一样。在研究中,哪位兄台如果知道其中原来,还请告知一声。
    
原创粉丝点击