【问题标题】:Viewpager not showing in constraint layoutViewpager 未在约束布局中显示
【发布时间】:2021-09-24 18:47:07
【问题描述】:

我在 ConstraintLayout 中放置了一个 ViewPager,但它根本没有显示出来。任何其他视图都不会显示任何问题。这是我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true">
        
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
                
                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:id="@+id/close_login_screen"
                    android:layout_gravity="left"
                    android:src="@drawable/icondelete"/>
                
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:src="@drawable/ic_launcher_background"
                    android:layout_marginTop="60dp"
                    android:layout_marginBottom="40dp"/>
                
                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/login_view_pager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <com.google.android.material.tabs.TabLayout
                        android:id="@+id/tab_layout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:tabIndicatorColor="@color/colorPrimaryDark"
                        app:tabTextColor="@android:color/darker_gray"
                        app:tabSelectedTextColor="@color/colorPrimaryDark"
                        app:tabIndicatorFullWidth="false"
                        app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"/>
                
                </androidx.viewpager.widget.ViewPager>
                
            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>
        
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

例如,如果我使用 LinearLayout,ViewPager 会显示出来,但它根本不会滚动显示额外的内容。不确定这是否有帮助,但我想要实现的最终布局如下所示:

但是我有这个:

【问题讨论】:

  • 您是否向ViewPager 提供了(电话号码和电子邮件)页面片段?
  • 是的,两者的片段都在那里,并且内容在线性布局中显示良好,但缺点是内容不会滚动。
  • 我更新了我的答案并重建了一切。现在 Viewpager 正在正常工作。随意复制类和 XML 来尝试一下。干杯!

标签: android android-layout android-fragments


【解决方案1】:

我为你安排好了一切。你需要有一个BaseFragment 来保存ViewPagerViewpager 调用另外两个片段。这里FirstFragment(电话)和SecondFragment(电子邮件)。所以首先像这样定义你的BaseFragment.java

public class BaseFragment extends Fragment {

    public BaseFragment(){
    }

    public static FirstFragment firstFragment;
    public static SecondFragment secondFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base, container, false);


        TabLayout tabLayout = view.findViewById(R.id.tab_layout);
        ViewPager viewPager = view.findViewById(R.id.login_view_pager);
        firstFragment = new FirstFragment();
        secondFragment = new SecondFragment();
        tabLayout.setupWithViewPager(viewPager);

        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager(), 0);
        viewPagerAdapter.addFragment(firstFragment,"Phone");
        viewPagerAdapter.addFragment(secondFragment,"Email");
        viewPager.setAdapter(viewPagerAdapter);

        return view;
    }

    private class ViewPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments = new ArrayList<>();
        private List<String> fragmentTitle = new ArrayList<>();

        public ViewPagerAdapter(@NonNull FragmentManager fm, int behavior) {
            super(fm, behavior);
        }

        public void addFragment(Fragment fragment, String title) {
            fragments.add(fragment);
            fragmentTitle.add(title);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return fragmentTitle.get(position);
        }


    }
}

并用你给我们的东西定义它的fragment_base.xml

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


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        tools:ignore="MissingConstraints">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:id="@+id/close_login_screen"
                    android:layout_gravity="left"
                    android:src="@drawable/icondelete"/>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:src="@drawable/ic_launcher_background"
                    android:layout_marginTop="60dp"
                    android:layout_marginBottom="40dp"/>

                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/login_view_pager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <com.google.android.material.tabs.TabLayout
                        android:id="@+id/tab_layout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:tabIndicatorColor="#121212"
                        app:tabTextColor="@android:color/darker_gray"
                        app:tabSelectedTextColor="#121212"
                        app:tabIndicatorFullWidth="false"
                        app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"/>

                </androidx.viewpager.widget.ViewPager>

            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

</RelativeLayout>

现在是更不吸引人的部分。用它的视图定义FirstFragmentSecondFragment。这将从ViewPagerTabLayout、您的电话和电子邮件页面中保留。

FirstFragment.java:

public class FirstFragment extends Fragment {



    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);


        TextView textView = view.findViewById(R.id.textView_phone);

        return view;
    }
}

fragment_first.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".FirstFragment">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Phone"
        android:textSize="28sp"
        android:id="@+id/textView_phone"
        android:layout_centerInParent="true"/>

</ScrollView>

SecondFragment.java:

public class SecondFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_second, container, false);


        TextView textView = view.findViewById(R.id.textView_email);

        return view;
    }
}

fragment_second.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".SecondFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Email"
        android:textSize="28sp"
        android:id="@+id/textView_email"
        android:layout_centerInParent="true"/>

</ScrollView>

结果

现在您看到Viewpager 工作正常。 如果要使片段可滚动,则需要在fragment_first.xmlfragment_second.xml 中添加ScrollView。干杯!

【讨论】:

  • viewPager 使用这种方法确实可以正常工作,但如果片段布局的内容与屏幕重叠,它仍然不会滚动。
  • 好吧,你是指不滚动的 BaseFragment 还是 Phone 和 Email Fragment?
  • 正如您在图片中看到的,您可以通过向其中添加 ScrollView 来滚动您的 2 个片段。我在更新答案的最后解释了它。
  • 啊,我明白了!所以我只能滚动片段本身,而不是它的父容器?我假设包含片段的整个活动将是要滚动的活动。
  • 不,这在您的情况下是不可能的,也没有任何意义。您有一个可以滚动的关闭按钮。我认为我建议的解决方案是最好的。 (有经验的)。随意勾选这个问题是正确的,以帮助社区。干杯!
猜你喜欢
  • 1970-01-01
  • 2019-05-10
  • 2022-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
  • 2018-12-12
  • 2018-05-31
相关资源
最近更新 更多