I have one issue with PagerAdapter in which when i load TextView
in it it works perfectly code is below:
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test, container, false);
TextView tv = (TextView) view.findViewById(R.id.tvtest);
tv.setText("" + position);
container.addView(view);
return view;
}
But when i use a Fragment
instead of TextView
like in below code PagerAdapter
is only loading first fragment.
Please help me to solve this problem.
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test, container, false);
MyFragment fragment = new MyFragment ();
FragmentTransaction transaction = mActivity.getFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, fragment).commit();
container.addView(view);
return view;
}
XML code:
When using TextView
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvtest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Test"
android:textSize="30sp" />
When using Fragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Thank you.