ViewPager does not scroll using FragmentPagerAdapter

I have two fragments in Activity. In portrait mode, the first (ListFragment) is displayed when the user clicks somewhere in the ListView, the second is displayed, and in landscape mode, both fragments are shown. However, I want to use the swipe function in portrait mode, and the following fragment should be displayed in it, for example this example ,

This is my main laucher activity that extends FragmentActivity,

package com.myexample.fragmentdemoexample; import java.util.LinkedList; import java.util.List; import java.util.Vector; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.ListFragment; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.ViewGroup; public class MainFragmentActivity extends FragmentActivity { private static int NUM_ITEMS = 4; MyAdapter mAdapter; ViewPager mPager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mPager = (ViewPager) findViewById(R.id.pager); instantiateFragments(); Log.i("MainFragmentActivity", "MainFragmentActivity is Created"); } public void instantiateFragments() { List<Fragment> fragments = new Vector<Fragment>(); fragments.add(Fragment.instantiate(this, MyListFragment.class.getName())); fragments.add(Fragment.instantiate(this, DetailFragment.class.getName())); mAdapter = new MyAdapter(getSupportFragmentManager(), fragments); mPager.setAdapter(mAdapter); mPager.setCurrentItem(0); } public class MyAdapter extends FragmentPagerAdapter { private FragmentManager frmanager; private List<Fragment> fragments; public MyAdapter(FragmentManager fm, List<Fragment> fragList) { super(fm); frmanager = fm; fragments = fragList; } @Override public int getCount() { return NUM_ITEMS; } @Override public Fragment getItem(int position) { return fragments.get(position); } } } 

This is my layout / main.xml file

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="0px" android:layout_weight="1" > </android.support.v4.view.ViewPager> <fragment android:id="@+id/listFragment" android:layout_width="150dip" android:layout_height="match_parent" class="com.myexample.fragmentdemoexample.MyListFragment" android:tag="listFragment" > </fragment> <fragment android:id="@+id/detailFragment" android:layout_width="match_parent" android:layout_height="match_parent" class="com.myexample.fragmentdemoexample.DetailFragment" android:tag="detailFragment" > <!-- Preview: layout=@layout /details --> </fragment> </LinearLayout> 

and /layout-port/main.xml contains

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <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.ViewPager> <fragment android:id="@+id/listFragment" android:layout_width="match_parent" android:layout_height="match_parent" class="com.myexample.fragmentdemoexample.MyListFragment" /> </LinearLayout> 

I don’t know why View Pager does not iterate over the next fragment ... How can I do this? I am using the Fragment API for the first time. Therefore, please give a few pointers in the right direction.

NOTE For other files, see this question.

+4
source share
2 answers

Finally, after an hour of insults, the problem was resolved by removing the tag tags from the main layout file. Now main.xml contains

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="0px" android:layout_weight="1" > </android.support.v4.view.ViewPager> </LinearLayout> 

This may be useful for others with a similar problem ...

+3
source

Even if he does not answer the question directly, but he has an answer to a similar problem that I encountered. maybe this will help others having a similar problem.

If you have a layout like

 <AppBarLayout> <ToolBar /> <TabLayout /> <ViewPager /> </AppBarLayout> 

The error here is ViewPager should not be inside AppBarLayout . So change your code to:

 <AppBarLayout> <ToolBar /> <TabLayout /> </AppBarLayout> <ViewPager /> 
+1
source

All Articles