Android解决ViewPager嵌套ViewPager中多层Fragment子ViewPager中加载不出来Fragment

博客介绍了如何处理Android中ViewPager嵌套ViewPager,以及Fragment嵌套Fragment的情况。关键点在于,当在Fragment中创建内部ViewPager的Adapter时,不应使用getActivity().getSupportFragmentManager(),而应使用getChildFragmentManager(),以确保正确管理子Fragment。

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


最近UI重新设计了一个店铺首页的效果,要用到ViewPager嵌套ViewPager才能实现效果,可是在子ViewPager中出现了加载不出来Fragment,由于里面用了多层的Fragment。

老规矩,先看效果图,没图说个j8。
在这里插入图片描述

先看代码:

首页代码:
在这里插入图片描述
第一层布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="-4dp"
    android:background="@drawable/ic_shop_bg2"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="28dp"
        android:layout_marginLeft="@dimen/text_layout_15_dp"
        android:layout_marginRight="@dimen/text_layout_15_dp"
        android:layout_marginTop="25dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_shop_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="ShopName"
            android:textColor="@color/white"
            android:textSize="@dimen/text_size_16sp"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <Button
                android:id="@+id/bt_shop_is_coll"
                android:layout_width="61dp"
                android:layout_height="28dp"
                android:layout_marginRight="@dimen/text_layout_12_dp"
                android:background="@drawable/ic_shop_text_bg"
                android:text="已关注"
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_13_sp" />

            <LinearLayout
                android:layout_width="75dp"
                android:layout_height="28dp"
                android:background="@drawable/ic_shop_text_bg"
                android:gravity="center"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tv_shop_other"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:paddingLeft="@dimen/text_layout_5_dp"
                    android:paddingRight="@dimen/text_layout_10_dp"
                    android:text="···"
                    android:textColor="@color/white"
                    android:textSize="@dimen/text_size_20_sp" />

                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="@dimen/text_layout_5_dp"
                    android:layout_marginTop="@dimen/text_layout_5_dp"
                    android:background="@color/white"
                    android:gravity="center_vertical" />

                <ImageView
                    android:id="@+id/iv_shop_exit"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:paddingLeft="@dimen/text_layout_10_dp"
                    android:paddingRight="@dimen/text_layout_5_dp"
                    android:src="@drawable/ic_shop_exit"
                    android:textSize="@dimen/text_size_15_sp" />
            </LinearLayout>
        </LinearLayout>

    </LinearLayout>

    <com.jkgl.view.NoSlideViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="@dimen/text_layout_5_dp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/text_layout_50_dp"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:background="@color/white"
        android:paddingLeft="@dimen/text_layout_15_dp"
        android:paddingRight="@dimen/text_layout_15_dp"
        >
        <RelativeLayout
            android:id="@+id/rl_shop_home"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"</
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值