Reputation: 193
I have a ViewPager placed inside a ConstraintLayout, but it is not showing up at all. Any other view will show up no problems. Here is my layout code:
<?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>
If I use a LinearLayout for example, the ViewPager does show up, but it simply won't scroll to show the extra content. Not sure if this helps, but the final layout I want to achieve looks something like this:
But instead I have this:
Upvotes: 0
Views: 559
Reputation: 3663
I set everything up for you. You need to have a BaseFragment
that will hold the ViewPager
. The Viewpager
invokes the 2 other Fragments. Here FirstFragment
(Phone) and SecondFragment
(Email). So first define your BaseFragment.java
like this:
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);
}
}
}
And also define it's fragment_base.xml
with the stuff you gave us:
<?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>
Now the more unattractive part. Define FirstFragment
and SecondFragment
with it's Views. That will be hold from the ViewPager
as TabLayout
, your Phone and Email pages.
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>
RESULT
Now you see the Viewpager
is working properly.
If you want to make the fragment scrollable you need to add ScrollView
in fragment_first.xml
and fragment_second.xml
. Cheers!
Upvotes: 1