Why my Fragment class is not showing it`s content when using PagerTabStrip?

44 Views Asked by At

I am stucked at one problem regarding my fragments. I just want to switch between different fragments and show a menu on the top - quite standard I think - smth. like here:

enter image description here

For this purpose I set up a PageAdapter (where SMartFragmentStatePageAdapter is just derived from FragmentStatePagerAdapter):

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;

class ViewPagerAdapter extends SmartFragmentStatePagerAdapter {


    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {        
        return ArrayListFragment.newInstance(position);
    }

    @Override
    public int getCount() {
        return categories.length;        
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return categories[position];
    }
}

I initializing the page adapter in my main class:

viewPager = (ViewPager) findViewById(R.id.pager);
SmartFragmentStatePagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);

And my ArrayFragment Class looks like this:

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.Collections;

public class ArrayListFragment extends ListFragment {
    int mNum;

    /**
     * Create a new instance of CountingFragment, providing "num"
     * as an argument.
     */
    public static ArrayListFragment newInstance(int num) {
        ArrayListFragment f = new ArrayListFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

    /**
     * When creating, retrieve this instance's number from its arguments.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
    }

    /**
     * The Fragment's UI is just a simple text view showing its
     * instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
        View tv = v.findViewById(R.id.text);
        int page = getArguments().getInt("num", -1);
        ((TextView)tv).setText("Fragment #" + mNum);
        return v;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, Collections.singletonList("test1")));
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("FragmentList", "Item clicked: " + id);
    }
}

Now the weired thing is, that for showing up the heading titles in of the PageAdapter I need to add the "PagerTabStrip" in my layout file - so my layout file for my main class looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1">
        <android.support.v4.view.PagerTabStrip
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v4.view.PagerTabStrip>
    </android.support.v4.view.ViewPager>

        <LinearLayout android:orientation="horizontal"
        android:gravity="center" android:measureWithLargestChild="true"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_weight="0">
        <Button android:id="@+id/goto_first"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="first">
        </Button>
        <Button android:id="@+id/goto_last"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="last">
        </Button>
    </LinearLayout>
</LinearLayout>

The Problem now is, when I am using the PagerTabStrip, then the content of the ArrayFragment is not shown up:

With the PagerTabStrip:

enter image description here

Without the PagerTabStrip:

enter image description here

So without the PagerTabStrip the content is shown and I do not know why.

I read here that the solution might be to use getChildFragmentManager() instead of getSupportFragmentManager(). But this function can only be used if my main class derives from Fragment, but it derives from FragmentActivity so this can not be the solution for me.

Any ideas?

1

There are 1 best solutions below

0
On

I found out in the meanwhile what went wrong.

I was pretty sure, that there has to be a problem in the layout file and indeed it was. Probably I should not have set the android:layout_height="0px" :) Result of hours of hacking without break.

So with small adaptions of the ViewPager it worked like a charme:

<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
            <android.support.v4.view.PagerTabStrip
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:paddingBottom="10dp"
            android:paddingTop="10dp">
        </android.support.v4.view.PagerTabStrip>
    </android.support.v4.view.ViewPager>

Thanks everybody who has started analyzing already!