0

Here is my MainActivity, Adapter and one Fragment as an example. The app itself is working and the Fragments themselves are created well but nothing appears on the PagerView. If somebody can help me through this, I will appreciate so much.

This is MainActivity.java

public class MainActivity extends AppCompatActivity {

private PagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

private TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSectionsPagerAdapter = new PagerAdapter(getSupportFragmentManager());

    HomeFragment homeFragment = new HomeFragment();
    FreeFragment freeFragment = new FreeFragment();
    ExpFragment expFragment = new ExpFragment();
    SettingsFragment settingsFragment = new SettingsFragment();

    mSectionsPagerAdapter.addFragment(homeFragment);
    mSectionsPagerAdapter.addFragment(freeFragment);
    mSectionsPagerAdapter.addFragment(expFragment);
    mSectionsPagerAdapter.addFragment(settingsFragment);



    mViewPager = findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    tabLayout = findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

}

}

This is an example Fragment source

public class HomeFragment extends Fragment {

public HomeFragment() {
}

public static HomeFragment newInstance() {
    HomeFragment fragment = new HomeFragment();
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_home, container, 
                    false);
    return rootView;

}

}

This is Adapter source

public class PagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> mFragments = new ArrayList<Fragment>();

public PagerAdapter(FragmentManager fm) {
    super(fm);
}

public void addFragment(Fragment fragment) {
    mFragments.add(fragment);
    notifyDataSetChanged();

}

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

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


}

Layout files are here, first one is layout for fragment

<?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"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">


<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

This is one for MainActivity

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

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

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab_text_1" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab_text_2" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab_text_3" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab 4" />

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

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>
CrimsonFoot
  • 325
  • 3
  • 13
  • Please add enough code so we can reproduce the error. For Android this means: if you have a "where are my Views?" type of problem, always add the layout files – Bö macht Blau May 23 '18 at 16:50
  • 1
    Is this the full content of your app? Because everything seems to be in order. Maybe the problem is somewhere else. – Luís Henriques May 23 '18 at 16:52
  • Thank you everybody. This is not a real app but it's made to test the functions. @0x0nosugr, I added the layout files. Can you please check it out? – CrimsonFoot May 23 '18 at 17:13
  • @Luis Henriques, Yeah they seem to be in order. Actually it worked yesterday in my original app. I didn't touch anything and I reopened it to proceed more but the layout disappeared suddenly. Maybe I missed something but I can't figure out what it is. Please help me. – CrimsonFoot May 23 '18 at 17:13
  • So the problem is that I can't see the simple button that I added to test if I can see the layout. – CrimsonFoot May 23 '18 at 17:15
  • 1
    If you use [LayoutInspector](https://developer.android.com/studio/debug/layout-inspector) you can see that the `Fragment` with the `Button` is somewhere below the `Tab`s. The `Tab`s on the other hand are at the bottom of the screen, in the middle is a big empty space. Did you want the pages to show up above the `Tab`s? – Bö macht Blau May 23 '18 at 18:17
  • @0x0nosugar, Yeah thank you so much. The problem was in the layout file. I placed the AppBarLayout on the top and it's solved. Thank you but should I use navigation bar to make what I meant to make? – CrimsonFoot May 24 '18 at 00:33
  • According to the material design guidelines a navigation bar is the way to go. Maybe this [post by Jay Rathod RJ](https://stackoverflow.com/a/36033640/5015207) is helpful. Happy coding! – Bö macht Blau May 24 '18 at 18:06

0 Answers0