Are there examples of ViewPager implementation code inside a DialogFragment? I found some examples, but every time there were different errors that did not allow us to understand what the problem was all about, so I ask for a complete example from and to containing xml whenever possible.

    1 answer 1

    enter image description here

    Tab_1.class

    public class Tab_1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.tab_1, container, false); } } 

    Tab_2.class

     public class Tab_2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.tab_2, container, false); } } 

    Tab_3.class

     public class Tab_3 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.tab_3, container, false); } } 

    tab_1.xml

     <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#c8ff0004" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textColor="#000" android:text="Tab 1" android:textAppearance="?android:attr/textAppearanceLarge"/> </RelativeLayout> 

    tab_2.xml

      <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#c80044ff" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textColor="#000" android:text="Tab 2" android:textAppearance="?android:attr/textAppearanceLarge"/> </RelativeLayout> 

    tab_3.xml

      <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#c85eff00" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textColor="#000" android:text="Tab 3" android:textAppearance="?android:attr/textAppearanceLarge"/> </RelativeLayout> 

    MainActivity.class

    Button to call custom dialogue.

      public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View v){ new FragmentsPopup().show(getSupportFragmentManager(), ""); } } 

    activity_main.xml

      <?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" android:background="@color/colorPrimary" tools:context="com.app.shwarz.viewpager_fragments_dialog_ver10.MainActivity"> <Button android:onClick="onClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Dialog" android:id="@+id/btn_1" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout> 

    PageAdapter.class

      public class PageAdapter extends FragmentPagerAdapter { private List<Fragment> fragments; public PageAdapter(FragmentManager fm, List<Fragment> fragments){ super(fm); this.fragments = fragments; } @Override public Fragment getItem(int position) { return this.fragments.get(position); } @Override public int getCount() { return this.fragments.size(); } } 

    FragmentsPopup.class

    Well, the dialogue class itself, for the sake of which all this.

      public class FragmentsPopup extends DialogFragment { PageAdapter pageAdapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.dialog_activity, container); ViewPager viewPager = (ViewPager) view.findViewById(R.id.pager); pageAdapter = new PageAdapter(getChildFragmentManager(), getFragments()); viewPager.setAdapter(pageAdapter); getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); //уберем a_bar return view; } private List getFragments(){ List list = new ArrayList(); list.add(new Tab_1()); list.add(new Tab_2()); list.add(new Tab_3()); return list; } } 

    dialog_activity.xml

      <RelativeLayout android:id="@+id/main_layout" 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" tools:context=".MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="250dp" android:layout_height="200dp" /> </RelativeLayout> 
    • one
      Thank you for your reply! Everything works great! - abbath0767