Search code examples
androidandroid-fragmentsandroid-viewpageradapter

Fragment not visible in ViewPager


I have a ViewPager that I want to add Fragments to. The ViewPager is viewed correctly on the screen but unfortunately it does not hold any content.

I have seen the suggestion in some posts to replace

getSupportFragmentManager() 

with

getChildFragmentManager()

But I need the ActionBar. Unfortunately the ChildFragmentmanager is not available in the AppCompatActivity.

While debugging I have realized that the Fragment's onCreateView() method is called and it returns a layout.

Question: How can I make the Fragments visible? And were is the main difference from my code to that sample ?

Activity:

public class ViewPagerActivity extends AppCompatActivity {

    private ViewPager mViewPager;

    private List<Location> locationList = new ArrayList<>();
    private locationsBaseHandler db;
    private CustomPagerAdapter mAdapter;

    private Location mLocation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_viewpager);

        locationList.add(someData);

        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mAdapter  = new CustomPagerAdapter(this, getSupportFragmentManager(), locationList);

        mViewPager.setAdapter(mAdapter);

    }

}

Pager Adapter:

public class CustomPagerAdapter extends FragmentPagerAdapter {

    private Context mContext;
    private List<Location> locationList = new ArrayList<>();

    public CustomPagerAdapter(Context mContext, FragmentManager fm, List<Location> locationList) {
        super(fm);

        this.locationList.addAll(locationList);
        this.mContext = mContext;
    }

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

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return false;
    }
}

Fragment:

public class LocationFragment extends Fragment {
    private static final String ARG_LAYOUT_ID = "page_number";

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

        return layout;
    }

    public static LocationFragment newInstance(int selectedIdForIndex) {
        LocationFragment fragment = new LocationFragment();

        Bundle args = new Bundle();
        args.putInt(ARG_LAYOUT_ID, selectedIdForIndex);
        fragment.setArguments(args);

        return fragment;
    }

    public int getPageNumber() {
        return getArguments().getInt(ARG_LAYOUT_ID);
    }
}

Solution

  • Try to remove the method isViewFromObject in CustomPagerAdapter.

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return false;
    }
    

    We can see this comment to isViewFromObject:

    Determines whether a page View is associated with a specific key object as returned by {@link #instantiateItem(ViewGroup, int)}. This method is required for a PagerAdapter to function properly.