Android ViewPager 覆盖下的子布局点击失效解决办法

使用层布局(FrameLayout)把viewpage放在底层 效果如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.viewpager.widget.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

            </androidx.viewpager.widget.ViewPager>
            
        </FrameLayout>
        
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:contentInsetStart="0dp"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <ImageView
                android:id="@+id/iv_map"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_alignParentEnd="true"
                android:layout_marginLeft="4dp"
                android:background="@drawable/selector_bg_img"
                android:padding="12dp"
                android:src="@mipmap/icon_add" />


            <ImageView
                android:id="@+id/location_img"
                android:layout_width="@dimen/dp_25"
                android:layout_height="@dimen/dp_24"
                android:layout_gravity="center"
                android:src="@mipmap/icon_location"
                android:visibility="gone" />


            <TextView
                android:id="@+id/tv_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="定位中"
                android:textColor="@color/white"
                android:textSize="18sp" />


            <ImageView
                android:id="@+id/iv_add"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_alignParentEnd="true"
                android:layout_gravity="right"
                android:layout_marginRight="4dp"
                android:background="@drawable/selector_bg_img"
                android:padding="14dp"
                android:src="@mipmap/icon_titler" />

        </androidx.appcompat.widget.Toolbar>
    </RelativeLayout>


</RelativeLayout>

ViewPager中嵌套ListView时,可能会遇到ListView无法滑动的问题,这是因为ViewPager会拦截ListView的滑动事件。解决方法如下: 1. 自定义ListView,重写其onInterceptTouchEvent()方法,返回false,让ViewPager不拦截ListView的滑动事件。 ``` public class MyListView extends ListView { public MyListView(Context context) { super(context); } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); } public MyListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: setParentScrollAble(false); break; case MotionEvent.ACTION_UP: setParentScrollAble(true); break; } return super.onInterceptTouchEvent(ev); } private void setParentScrollAble(boolean flag) { getParent().requestDisallowInterceptTouchEvent(!flag); } } ``` 2. 在ViewPager的适配器中,将ListView所在的布局设置为android:descendantFocusability="blocksDescendants",防止ListView获取焦点而导致ViewPager无法滑动。 ``` <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants"> <com.example.MyListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> ``` 以上两种方法都可以解决ViewPager中ListView失效的问题。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值