Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put viewpager inside fragment in android? [closed]

Tags:

I've an activity and I can show some fragments on framelayout on this activity. I would like to put a viewpager made up of fragments inside a fragment instead of a FragmentActivity. How can this be done?

like image 799
Sayed Avatar asked Mar 04 '15 15:03

Sayed


People also ask

How do I load a fragment in ViewPager?

Try pager. setOffscreenPageLimit(1); This will retain one fragment at a time. this will retain in "memory" one however it will create the fragment every time the use swype.

How do you call ViewPager fragment from activity?

You'll just need to cast it to the real fragment class if you want to call a specific method. int pos = viewpager. getCurrentItem(); Fragment activeFragment = adapter. getItem(pos); if(pos == 0) ((NPListFragment)activeFragment).

How do I use ViewPager?

You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .


1 Answers

To have a ViewPager be in a fragment, and to have fragments be the pages in that ViewPager, you need to use nested fragments. If your minSdkVersion is 17 or higher, the native implementation of fragments supports nested fragments. Otherwise, you will need to use the support-v13 (or support-v4) backport of fragments.

Then, you just need to give getChildFragmentManager() to your FragmentPagerAdapter or FragmentStatePagerAdapter:

/***
  Copyright (c) 2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.pagernested;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PagerFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater,
                           ViewGroup container,
                           Bundle savedInstanceState) {
    View result=inflater.inflate(R.layout.pager, container, false);
    ViewPager pager=(ViewPager)result.findViewById(R.id.pager);

    pager.setAdapter(buildAdapter());

    return(result);
  }

  private PagerAdapter buildAdapter() {
    return(new SampleAdapter(getActivity(), getChildFragmentManager()));
  }
}

(from this sample project)

like image 193
CommonsWare Avatar answered Sep 28 '22 11:09

CommonsWare