ListView(recycleView)和佈局其他控制元件一起滑動
阿新 • • 發佈:2019-02-16
如何讓ListView及以上的佈局一起滑動,
或者說,如何讓整個Activity的佈局都隨著ListView一起滑動
就是其實很簡單,不排除其他的實現方法,
但個人實現方法是使用一個特殊的ScrollView以及一個重寫的ListView
重寫ListView:
- public class UnScrollListView extends ListView {
- public UnScrollListView(Context context) {
- super(context);
- }
- public UnScrollListView(Context context, AttributeSet attrs)
- super(context, attrs);
- }
- public UnScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2
- MeasureSpec.AT_MOST);
- super.onMeasure(widthMeasureSpec, expandSpec);//這裡返回的是剛寫好的expandSpec
- }
- }
- class MyScrollView extends ScrollView {
- OnTouchListener mGestureListener;
- // 滑動距離及座標
- private float xDistance, yDistance, xLast, yLast;
- public MyScrollView(Context context)
- super(context);
- }
- public MyScrollView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
- public boolean onInterceptTouchEvent(MotionEvent ev) {
- switch (ev.getAction()){
- case MotionEvent.ACTION_DOWN:
- xDistance = yDistance = 0f;
- xLast = ev.getX();
- yLast = ev.getY();
- break;
- case MotionEvent.ACTION_MOVE:
- final float curX = ev.getX();
- final float curY = ev.getY();
- xDistance += Math.abs(curX - xLast);
- yDistance += Math.abs(curY - yLast);
- xLast = curX;
- yLast = curY;
- if(xDistance > yDistance){
- return false;
- }
- break;
- }
- return super.onInterceptTouchEvent(ev);
- }
- }
然後在需要使用ListView的地方寫上這樣的佈局:
將所有需要一起滾動的佈局(如下面程式碼中的fragment,imageButton,以及剛才重寫的UnScrollListView)放入一個LinerLayout或RelativeLayout中,最後放入MyScrollView中
- "1.0" encoding="utf-8" xml version=
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/content_main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- tools:context="com.han.mobilelib.MainActivity"
- tools:showIn="@layout/app_bar_main"
- >
- <com.han.mobilelib.BookRecord.MyScrollView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_alignParentTop="true"
- android:layout_alignParentStart="true">
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <fragment
- android:id="@+id/fragment_cycle_viewpager_content"
- android:name="com.han.mobilelib.CycleImage.CycleViewPager"
- android:layout_width="match_parent"
- android:layout_height="180dp"
- />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_below="@+id/fragment_cycle_viewpager_content"
- android:orientation="horizontal"
- android:layout_marginTop="20dp"
- android:layout_marginBottom="20dp">
- <ImageButton
- android:id="@+id/imageButton1"
- android:layout_width="70dp"
- android:layout_height="70dp"
- android:background="@drawable/img_my_note"
- android:layout_gravity="center"
- android:layout_marginLeft="50dp"
- android:scaleType="fitCenter"
- />
- <ImageButton
- android:id="@+id/imageButton2"
- android:layout_width="70dp"
- android:layout_height="70dp"
- android:background="@drawable/img_book"
- android:layout_marginLeft="40dp"
- android:layout_gravity="center"
- android:scaleType="centerInside"
- />
- <ImageButton
- android:id="@+id/imageButton3"
- android:layout_width="70dp"
- android:layout_height="70dp"
- android:layout_gravity="center"
- android:layout_marginLeft="40dp"
- android:background="@drawable/guess"
- android:scaleType="fitCenter" />
- </LinearLayout>
- <com.han.mobilelib.BookRecord.UnScrollListView
- android:id="@+id/usedBooksList"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="300dp"
- android:background="@color/miHuangSe"/>
- </RelativeLayout>
- </com.han.mobilelib.BookRecord.MyScrollView>
- </RelativeLayout>
最後在對應的Activity中,繫結控制元件就可以了
- private UnScrollListView mListView;
- private MyScrollView mScrollView;
- mListView= (UnScrollListView) findViewById(R.id.usedBooksList);
- mScrollView = (MyScrollView) findViewById(R.id.scrollView);
最後還有一點就是,在開啟Activity時,首先看到ListView,並不能看到其上面的佈局,必須要拖上去
所以最後在上面的程式碼下設定一句
mListView.setFocusable(false);
ok,這樣就可以了