1. 程式人生 > >ListView(recycleView)和佈局其他控制元件一起滑動

ListView(recycleView)和佈局其他控制元件一起滑動

如何讓ListView及以上的佈局一起滑動,

或者說,如何讓整個Activity的佈局都隨著ListView一起滑動

就是其實很簡單,不排除其他的實現方法,

但個人實現方法是使用一個特殊的ScrollView以及一個重寫的ListView

重寫ListView:

  1. public class UnScrollListView extends ListView {
  2. public UnScrollListView(Context context) {
  3. super(context);
  4. }
  5. public UnScrollListView(Context context, AttributeSet attrs)
    {
  6. super(context, attrs);
  7. }
  8. public UnScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
  9. super(context, attrs, defStyleAttr);
  10. }
  11. @Override
  12. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  13. int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2
    ,
  14. MeasureSpec.AT_MOST);
  15. super.onMeasure(widthMeasureSpec, expandSpec);//這裡返回的是剛寫好的expandSpec
  16. }
  17. }
重寫ScrollView:
  1. class MyScrollView extends ScrollView {
  2. OnTouchListener mGestureListener;
  3. // 滑動距離及座標
  4. private float xDistance, yDistance, xLast, yLast;
  5. public MyScrollView(Context context)
    {
  6. super(context);
  7. }
  8. public MyScrollView(Context context, AttributeSet attrs) {
  9. super(context, attrs);
  10. }
  11. public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
  12. super(context, attrs, defStyleAttr);
  13. }
  14. @Override
  15. public boolean onInterceptTouchEvent(MotionEvent ev) {
  16. switch (ev.getAction()){
  17. case MotionEvent.ACTION_DOWN:
  18. xDistance = yDistance = 0f;
  19. xLast = ev.getX();
  20. yLast = ev.getY();
  21. break;
  22. case MotionEvent.ACTION_MOVE:
  23. final float curX = ev.getX();
  24. final float curY = ev.getY();
  25. xDistance += Math.abs(curX - xLast);
  26. yDistance += Math.abs(curY - yLast);
  27. xLast = curX;
  28. yLast = curY;
  29. if(xDistance > yDistance){
  30. return false;
  31. }
  32. break;
  33. }
  34. return super.onInterceptTouchEvent(ev);
  35. }
  36. }

然後在需要使用ListView的地方寫上這樣的佈局:

將所有需要一起滾動的佈局(如下面程式碼中的fragment,imageButton,以及剛才重寫的UnScrollListView)放入一個LinerLayout或RelativeLayout中,最後放入MyScrollView中

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/content_main"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  9. tools:context="com.han.mobilelib.MainActivity"
  10. tools:showIn="@layout/app_bar_main"
  11. >
  12. <com.han.mobilelib.BookRecord.MyScrollView
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. android:layout_alignParentTop="true"
  16. android:layout_alignParentStart="true">
  17. <RelativeLayout
  18. android:layout_width="match_parent"
  19. android:layout_height="match_parent">
  20. <fragment
  21. android:id="@+id/fragment_cycle_viewpager_content"
  22. android:name="com.han.mobilelib.CycleImage.CycleViewPager"
  23. android:layout_width="match_parent"
  24. android:layout_height="180dp"
  25. />
  26. <LinearLayout
  27. android:layout_width="match_parent"
  28. android:layout_height="match_parent"
  29. android:layout_below="@+id/fragment_cycle_viewpager_content"
  30. android:orientation="horizontal"
  31. android:layout_marginTop="20dp"
  32. android:layout_marginBottom="20dp">
  33. <ImageButton
  34. android:id="@+id/imageButton1"
  35. android:layout_width="70dp"
  36. android:layout_height="70dp"
  37. android:background="@drawable/img_my_note"
  38. android:layout_gravity="center"
  39. android:layout_marginLeft="50dp"
  40. android:scaleType="fitCenter"
  41. />
  42. <ImageButton
  43. android:id="@+id/imageButton2"
  44. android:layout_width="70dp"
  45. android:layout_height="70dp"
  46. android:background="@drawable/img_book"
  47. android:layout_marginLeft="40dp"
  48. android:layout_gravity="center"
  49. android:scaleType="centerInside"
  50. />
  51. <ImageButton
  52. android:id="@+id/imageButton3"
  53. android:layout_width="70dp"
  54. android:layout_height="70dp"
  55. android:layout_gravity="center"
  56. android:layout_marginLeft="40dp"
  57. android:background="@drawable/guess"
  58. android:scaleType="fitCenter" />
  59. </LinearLayout>
  60. <com.han.mobilelib.BookRecord.UnScrollListView
  61. android:id="@+id/usedBooksList"
  62. android:layout_width="match_parent"
  63. android:layout_height="wrap_content"
  64. android:layout_marginTop="300dp"
  65. android:background="@color/miHuangSe"/>
  66. </RelativeLayout>
  67. </com.han.mobilelib.BookRecord.MyScrollView>
  68. </RelativeLayout>

最後在對應的Activity中,繫結控制元件就可以了
  1. private UnScrollListView mListView;
  2. private MyScrollView mScrollView;
  3. mListView= (UnScrollListView) findViewById(R.id.usedBooksList);
  4. mScrollView = (MyScrollView) findViewById(R.id.scrollView);

最後還有一點就是,在開啟Activity時,首先看到ListView,並不能看到其上面的佈局,必須要拖上去

所以最後在上面的程式碼下設定一句

mListView.setFocusable(false);

ok,這樣就可以了