dialog、PopupWindow里面实现布局切换

本文介绍了一种使用HorizontalScrollView和自定义PopupWindow实现的自定义Dialog布局切换方案。该方案解决了视图页面ID找不到的问题,并通过手动设置布局宽度实现了平滑的布局切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  根据产品需求,需要实现自定义dialog,里面包含多个布局切换:类似效果如下图所示(子布局样式根据需求来自定义):

第一方案:popWindows+TabLayout+viewapge

结果:一直报viewpage对应的id一直找不到,无论我怎么修改还是删除id,还是这样子报错,捣鼓了一天,感谢sakura_L写的简书:https://www.jianshu.com/p/8b76de9969b4让我知道错在哪了,里面有实现,但是效果不是想要的,后面还是放弃了。

第二方案:HorizontalScrollView、fragment、Viewpager结合使用,参考:https://blog.youkuaiyun.com/qq_41673194/article/details/79357629,但是代码量增加了很多,报的错还是前面那个(因为当时还没有找到方案一错误的原因),还是放弃了。

第三方案:HorizontalScrollView、自定义PopupWindow(位置可以随意指定)具体实现步骤如下:

 

步骤一:activity_main布局就一个button控件,代码不贴。

item_main(自定义PopupWindow)布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/www">

    <!--若子布局有滑动事件操作,可参考https://blog.youkuaiyun.com/wangyongyao1989/article/details/76615827博客,把HorizontalScrollView换成博客里的MyHorizontalScrollView 即可-->
    <HorizontalScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="394dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">
 <!--HorizontalScrollView只允许有一个子布局-->
        <LinearLayout
            android:id="@+id/contentLinearLayout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#E91E63"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/layout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary"
                android:orientation="horizontal" >
                <!--在这里定义布局一-->
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="我是布局一"
                    android:textSize="18sp"
                    android:textColor="@color/www"/>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/layout2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#E91E63"
                android:orientation="horizontal">
                <!--在这里定义布局一-->
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="我是布局二"
                    android:textSize="18sp"
                    android:textColor="@color/www"/>

            </LinearLayout>
        </LinearLayout>
    </HorizontalScrollView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示布局一"
        app:layout_constraintBottom_toTopOf="@+id/scrollView"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示布局二"
        app:layout_constraintBottom_toTopOf="@+id/scrollView"
        app:layout_constraintLeft_toRightOf="@id/button1" />


</android.support.constraint.ConstraintLayout>

步骤二:MainActivity里实现button点击事件,调用showPopupWindow()方法,代码如下:

private void showPopupWindow() {
        View wh = LayoutInflater.from(this).inflate(R.layout.item_main, null);
        scrollView = wh.findViewById(R.id.scrollView);
        layout1 = wh.findViewById(R.id.layout1);
        layout2 = wh.findViewById(R.id.layout2);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        final int width = getWidthInPx(this);

       //重点:代码设置布局的宽度,不然所有布局全部缩小版显示在一起
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT);
        ViewGroup.LayoutParams params1 = layout1.getLayoutParams();
        ViewGroup.LayoutParams params2 = layout2.getLayoutParams();
        params1.width = width;
        params2.width = width;
        layout1.setLayoutParams(layoutParams);
        layout2.setLayoutParams(layoutParams);

        final WPopupWindow popupWindow = new WPopupWindow(wh);
        popupWindow.showAtLocation(getContentView(MainActivity.this), Gravity.BOTTOM, 0, 0);

        scrollView.setOnTouchListener(new View.OnTouchListener() {//scrollView滑动事件静止
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

        wh.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.scrollTo(0, 0);
            }
        });
        wh.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.scrollTo(width, 0);
            }
        });

    }

    //获取屏幕宽度
    public int getWidthInPx(Context context) {
        final int width = context.getResources().getDisplayMetrics().widthPixels;
        return width;
    }

    private static View getContentView(Activity ac) {
        ViewGroup view = (ViewGroup) ac.getWindow().getDecorView();
        FrameLayout content = view.findViewById(android.R.id.content);
        return content.getChildAt(0);
    }

 

总结:若子布局中有滑动事件,就不能滑动切换布局,只能点击按钮进行切换

Demo地址:https://github.com/mcl22222/HorizontalScrollView

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值