NestedScrollView中的Fragment(Viewpager)无法加载

5
我有一个布局为CoordinatorLayout的Fragment,里面包含一个NestedScrollView,在嵌套滚动视图中又包含了一个带有片段的ViewPagerTabLayout。选项卡可见,但与选项卡相关的片段未加载。

Visible Tab but not fragment

布局

                                                                                                            <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:clickable="true"
    android:background="?android:attr/colorBackground"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <!--app:contentScrim="?attr/colorPrimary" -->


          <android.support.v4.view.ViewPager
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/ViewPagerImages"
              app:layout_collapseMode="parallax"
              app:layout_collapseParallaxMultiplier="0.7"
              android:scrollIndicators="top|right">

          </android.support.v4.view.ViewPager>


            <ProgressBar
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:id="@+id/Progress_Bar_Image"
                android:layout_gravity="center_horizontal|center_vertical"
                android:visibility="visible"
                 />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="right|top"
                android:layout_marginRight="30dp"
                android:layout_marginTop="30dp"
                android:background="@drawable/round_default"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="25sp"
                    android:text="1"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="25sp"
                        android:text="/5"/>


            </LinearLayout>
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_gravity="bottom"
                android:padding="15dp"
                >
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/unsa"
                    android:orientation="horizontal"
                    android:gravity="center">
                    <ImageView
                        android:id="@+id/user_S"
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:padding="10dp" />
                    <TextView
                        android:id="@+id/ViewPager_Name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="10dp"
                        app:layout_behavior="com.boysjoys.com.pro_working1.CustomClass.UserProfile_Behaviour"
                        android:textSize="35sp" />


                    <TextView
                        android:id="@+id/ViewPager_A"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="10dp"
                        android:text="Age"
                        />

                </LinearLayout>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="New Delhi"
                    android:layout_below="@id/unsa"
                    android:layout_marginLeft="37dp"
                    android:id="@+id/ViewPager_City"
                   />

                <Button
                    android:id="@+id/Attached"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:background="@drawable/round_button"
                    android:layout_alignParentBottom="true"
                    android:elevation="14dp"
                    android:shadowColor="#A8A8A8"
                    android:shadowDx="0"
                    android:shadowDy="0"
                    android:shadowRadius="5"
                    android:text="WRITE ME"
                    android:textColor="#FFFFFF"
                    android:textSize="20sp" />



            </RelativeLayout>


            <View
                android:id="@+id/fabBGLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/custom_transparent_color1"
                android:visibility="gone"/>



            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fabOne"
                android:layout_gravity="bottom|end"
                android:padding="12dp"
                android:visibility="gone"
                android:layout_marginBottom="90dp"
                android:layout_marginRight="20dp"
                android:layout_width="45dp"
                android:layout_height="45dp" />

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fabTwo"
                android:padding="12dp"
                app:fabSize="mini"
                android:visibility="gone"
                android:layout_gravity="bottom|end"
                android:layout_marginBottom="90dp"
                android:layout_marginRight="20dp"
                android:layout_width="45dp"
                android:layout_height="45dp" />

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fabThree"
                android:layout_gravity="bottom|end"
                android:padding="12dp"
                app:fabSize="mini"
                android:visibility="gone"
                android:layout_marginBottom="90dp"
                android:layout_marginRight="20dp"
                android:layout_width="45dp"
                android:layout_height="45dp" />

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fabMain"
                android:padding="12dp"
                android:layout_gravity="bottom|end"
                android:layout_marginBottom="90dp"
                android:layout_marginRight="20dp"
                android:src="@drawable/com_facebook_tooltip_black_xout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />


        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>


//            NESTED SCROLL VIEW WHERE THE PROBLEM ARISE
//            TABLAYOUT IS VISIBLE BUT FRAGMENT IS NOT.

    <android.support.v4.widget.NestedScrollView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/userProfile_NestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="13dp"
        android:background="@android:color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">



       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ScrollViewChild"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="15dp">

            <!--To show tab on top of view pager-->
        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"
            app:tabTextColor="@color/place_autocomplete_prediction_primary_text_highlight"
            app:tabSelectedTextColor="@color/colorPrimary"
            app:tabIndicatorColor="@color/colorPrimary"
            android:id="@+id/userProfile_Viewpager_Tab">
        </android.support.design.widget.TabLayout>

            <android.support.v4.view.ViewPager
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/userProfile_Viewpager_Tab"
                android:id="@+id/userProfile_Viewpager_ViewPager">

            </android.support.v4.view.ViewPager>



</RelativeLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>
public class UserProfile_TabAdapter extends FragmentPagerAdapter {
    String TAG = "###TabAdapter###";
    Context context;

    public UserProfile_TabAdapter(FragmentManager fm,Context context) {
        super(fm);
        this.context=context;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        Log.d(TAG," Positions "+position);
        switch (position) {
            case 0:

                fragment = Fragment.instantiate(context,Info_fragment.class.getName());
                break;
            case 1:
                Log.d(TAG, "User Photos Running");
                fragment = Fragment.instantiate(context,Photo_fragment.class.getName());
                break;
            case 2:
                Log.d(TAG, "User Connections Running");
                fragment = Fragment.instantiate(context,Connections_fragment.class.getName());
                break;
        }


        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position){
            case 0:
                return "About";
            case 1:
                return "Photo";
            case 2:
                return "Connections";

        }
        return null;

    }

}

我尝试将片段加载到视图页面中的片段

 //NESTED SCROLL VIEW TAB LAYOUT AND VIEW PAGER
        userInfo_ViewPager=(ViewPager) view.findViewById(R.id.userProfile_Viewpager_ViewPager);
        UserProfile_TabAdapter userProfile_tabAdapter=new UserProfile_TabAdapter(getChildFragmentManager(),getActivity());
        userInfo_ViewPager.setAdapter(userProfile_tabAdapter);
        tabLayout=(TabLayout) view.findViewById(R.id.userProfile_Viewpager_Tab);
        tabLayout.setupWithViewPager(userInfo_ViewPager);

要加载到ViewPager中的片段

public class Info_fragment extends Fragment {
    public static final String TAG="### INFO ####";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.userprofile_photos,container,false);
        //Above Layout only have colorful background to match parent.
        return view;
    }
}

为什么你不使用 FragmentManagerFragmentTransaction 呢? - Aditya
@Heisen-Berg,因为这总是对我起作用的方式。我可以在日志中看到片段已加载,但未显示在viewpager上。 - Ritu
@Guruji 这只是加载颜色背景,没有任何内容。 - Ritu
@Ritu 它是否正常工作。 - Ratilal Chopda
@RatilalChopda 是的,现在它可以工作了,非常感谢您的帮助。 - Ritu
显示剩余6条评论
5个回答

7

试试这个

NestedScrollView scrollView = (NestedScrollView) findViewById (R.id.userProfile_NestedScrollView);
scrollView.setFillViewport (true);

1
请问您能告诉我为什么setFillViewport在我的情况下起作用吗? - Ritu
我可以在它获得焦点之前避免加载片段吗? - Ritu
1
@Ritu setFillViewport是一个方法,用于定义ScrollView是否应该拉伸其内容以填充整个视图窗口。您可以在https://developer.android.com/reference/android/widget/ScrollView.html找到更多相关信息。 - varotariya vajsi
也在 XML 视图中工作 android:fillViewport="true" - Dilroop Singh

1
在您的 NestedScrollView 中添加此行。
android:fillViewport="true"

0
我遇到了同样的问题,最好的解决方案是设置您的ViewPager的高度。
例如:
android:layout_height="500dp"

0

// 你的 appBar 布局高度是 match_parent,请将其改为 wrap_content

<android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

如果使用wrap_content,那么第一个视图页面显示不正确。在这里,对我来说match_parent是正常工作的。 - Ritu

0

在此输入图片描述问题出在您的视图页高度上。只需设置一些高度,您的页面就会显示出来。

例如:

          <android.support.v4.view.ViewPager
            android:id="@+id/userProfile_Viewpager_ViewPager"
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:layout_below="@id/userProfile_Viewpager_Tab">


        </android.support.v4.view.ViewPager>

你需要找到解决方案,为什么视图页面没有占用高度

或者

NestedScrollView scrollView = (NestedScrollView) findViewById (R.id.userProfile_NestedScrollView); scrollView.setFillViewport (true);


有两个视图页面,你指向了错误的视图页面。看一下布局,你会发现第二个是我遇到问题的地方。 - Ritu
请告诉我您遇到的问题。根据您的问题和代码,两个视图页面之间没有关系。<android.support.v4.view.ViewPager android:id="@+id/ViewPagerImages" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollIndicators="top|right" app:layout_collapseMode="parallax" app:layout_collapseParallaxMultiplier="0.7"> - Gnanaoly Annamalai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接