How can I change ViewPager fragment from another fragment? Without doing new - java

This application is a questionnaire.
I used ViewPager to host the fragments.
When the user gets to the last screen, EndingQuestionsFragment, they have a button which then press on and it takes them to the main screen (not important for my problem).
If there is a question which is not answered, when the button is clicked, it should take them to the fragment which contains the question which is not answered.
I used:
MainActivity parent = (MainActivity) getActivity();
parent.setPagerFragment(0);
Inside a setOnClickListener.
However, when that button is clicked, any answers that were given are reset. (the radioButtons are reset).
How can I navigate through the fragments without losing the inputs?
FragmentCommunicator.java
package com.example.zdroa.testinggrounds;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.example.zdroa.testinggrounds.Question_Fragments.AnxiousQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.DependentQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.DepressiveQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.EndingQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.HistrionicQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.InitializerFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.NarcissistQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.ObsessiveQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.ParanoidQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.SchizoidQuestionsFragment;
public class FragmentCommunicator extends FragmentPagerAdapter{
public FragmentCommunicator(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new InitializerFragment();
case 1:
return new AnxiousQuestionsFragment();
case 2:
return new ParanoidQuestionsFragment();
case 3:
return new HistrionicQuestionsFragment();
case 4:
return new ObsessiveQuestionsFragment();
case 5:
return new NarcissistQuestionsFragment();
case 6:
return new SchizoidQuestionsFragment();
case 7:
return new DepressiveQuestionsFragment();
case 8:
return new DependentQuestionsFragment();
case 9:
return new EndingQuestionsFragment();
}
return null;
}
#Override
public int getCount() {
return 10;
}
}
MainActivity.java
package com.example.zdroa.testinggrounds;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import com.example.zdroa.testinggrounds.Question_Fragments.AnxiousQuestionsFragment;
public class MainActivity extends AppCompatActivity {
public static String PERSON_TYPE;
ViewPager viewPager;
public static FragmentCommunicator communicator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.vpQuestionsActivity);
communicator = new FragmentCommunicator(getSupportFragmentManager());
viewPager.setAdapter(communicator);
viewPager.setCurrentItem(0);
int anxious_result = new AnxiousQuestionsFragment().getResult();
if (anxious_result != 0){
PERSON_TYPE = "Anxious person";
}
}
public String getPersonType() {
return PERSON_TYPE;
}
public void setPagerFragment(int a){
viewPager.setCurrentItem(a);
}
}
EndingQuestionsFragment.java
package com.example.zdroa.testinggrounds.Question_Fragments;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.example.zdroa.testinggrounds.MainActivity;
import com.example.zdroa.testinggrounds.R;
public class EndingQuestionsFragment extends Fragment {
public EndingQuestionsFragment() {
// Required empty public constructor
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_ending_questions, container, false);
Button button1 = (Button) view.findViewById(R.id.bGotoMainAreaActivity);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity parent = (MainActivity) getActivity();
parent.setPagerFragment(0);
}
});
return view;
}
}
fragment_ending_questions.xml
<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="#fefefe"
android:padding="16dp"
tools:context="com.example.zdroa.testinggrounds.Question_Fragments.AnxiousQuestionsFragment">
<TextView
android:id="#+id/ending_tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The End"
android:textAlignment="center"
android:textColor="#000"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/ending_tv_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/ending_tv_title"
android:layout_marginTop="30dp"
android:text="Now that you have answered all the questions please click the Finish button.\n\nIf any questions have been left unanswered you will be taken back to the screen with the missing answeres. Your other answeres will not be affected."
android:textAlignment="center"
android:textColor="#000"
android:textSize="20sp" />
<Button
android:id="#+id/bGotoMainAreaActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="30dp"
android:text="Finish" />
</RelativeLayout>
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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/vpQuestionsActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>

Use `ViewPager.setOffscreenPageLimit() in FragmentActivity
Reason why??
Default fragment pager adapter which u used,it destroys and recreates the fragments every time u slide across,using this code u tell it not to destroy the initial fragment state of the previously loaded fragments
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setOffscreenPageLimit(10); // i assumed there are 10 fragments in total

Instead of doing:
public Fragment getItem(int position) {
switch (position){
case 0:
return new InitializerFragment();
case 1:
return new AnxiousQuestionsFragment();
case 2:
return new ParanoidQuestionsFragment();
case 3:
return new HistrionicQuestionsFragment();
case 4:
return new ObsessiveQuestionsFragment();
case 5:
return new NarcissistQuestionsFragment();
case 6:
return new SchizoidQuestionsFragment();
case 7:
return new DepressiveQuestionsFragment();
case 8:
return new DependentQuestionsFragment();
case 9:
return new EndingQuestionsFragment();
}
return null;
}
Create an instance of each fragment at the start (in onCreate maybe)
So:
InitializerFragment fragmet = new InitializerFragment();
Then change your getItem method to do this instead:
public Fragment getItem(int position) {
switch (position){
case 0:
return fragment;
case 1:
// And so on...
}
Do this for all the fragments in the viewpager, this way the view pager will preserve the instance of the fragment and keep hold of the answers in the fragment.

Related

How to use Tabbed navigation layout inside a fragment

I want to put tabbed layout inside a fragment layout, i have tried many methods but none of them worked.
Things to be noted first are I am already having a navigation drawer with 4 fragments and I want to apply tabbed layout to one of its fragment not All.
This is my JAVA code of the fragment on which i want to use Tabbed layout ,i am using android studio v3.3
package com.androidapp.kjsscapp;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class NoticeBoard extends Fragment {
public NoticeBoard() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_notice_board, container, false);
TabLayout tabLayout = v.findViewById(R.id.NoticeBoard_Tab);
final ViewPager viewPager = v.findViewById(R.id.Mpager1);
tabpageAdapter tabpageAdapter = new tabpageAdapter(getChildFragmentManager(),tabLayout.getTabCount());
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return v;
}
}
The currosponding xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NoticeBoard">
<android.support.design.widget.TabLayout
android:id="#+id/NoticeBoard_Tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:visibility="visible"
app:tabBackground="#color/colorPrimary"
app:tabIndicatorColor="#android:color/holo_blue_dark"
app:tabTextColor="#color/White">
<android.support.design.widget.TabItem
android:id="#+id/TabItem_general"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="General" />
<android.support.design.widget.TabItem
android:id="#+id/TabItem_admissions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Admissions" />
<android.support.design.widget.TabItem
android:id="#+id/TabItem_scholarship"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scholarships" />
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/Mpager1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/NoticeBoard_Tab" />
</RelativeLayout>
The tab controller:
package com.androidapp.kjsscapp;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class tabpageAdapter extends FragmentStatePagerAdapter {
int countTab;
public tabpageAdapter(FragmentManager fm, int tabCount) {
super(fm);
this.countTab = countTab;
}
#Override
public Fragment getItem(int i) {
Tab_General gen = new Tab_General();
Tab_Admissions add = new Tab_Admissions();
Tab_ScholarShip sch = new Tab_ScholarShip();
switch (i){
case 0:
return gen;
case 1:
return add;
case 2:
return sch;
default:
return gen;
}
}
#Override
public int getCount() {
return countTab;
}
}
I expect to view the fragments of tablayout inside the fragment of drawer layout as soon as the fragment of drawer is called. Kindly provide the solution if any

Errors when trying to implement Swipe Views in android

Sorry for the code dump, I'm new to android and don't know where the problem is. I'm trying to implement swipe views in my android app and I'm having some trouble. I tried following this tutorial and this video but I'm getting some errors. I want the tabbed interface in my MainActivity
Here is my MainActivity.java
package com.loomius.loomius;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
PagerAdapter pagerAdapter = new FixedTabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
}
}
I'm getting this error for getSupporFragmentManager()
'FixedTabsPagerAdapter(android.app.FragmentManager)' in 'com.loomius.loomius.FixedTabsPagerAdapter' cannot be applied to '(android.support.v4.app.FragmentManager)'
and here is my FixedTabsPagerAdapter.java
package com.loomius.loomius;
import android.app.FragmentManager;
import android.content.Context;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.app.Fragment;
import values.MatchesFragment;
import values.SuggestedSongsFragment;
import values.UserFragment;
public class FixedTabsPagerAdapter extends FragmentPagerAdapter{
public FixedTabsPagerAdapter (FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 4;
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new SearchFragment();
case 1:
return new UserFragment();
case 2:
return new MatchesFragment();
case 3:
return new SuggestedSongsFragment();
default:
return null;
}
}
Context context;
#Override
public CharSequence getPageTitle (int position) {
switch(position) {
case 0:
return context.getResources().getString(R.string.search_frag_title);
case 1:
return context.getResources().getString(R.string.user_frag_title);
case 2:
return context.getResources().getString(R.string.matches_frag_title);
case 3:
return context.getResources().getString(R.string.sugg_frag_title);
default:
return null;
}
}
}
I'm getting this error for the return type Fragment in the overridden method getItem
'getItem(int)' in 'com.loomius.loomius.FixedTabsPagerAdapter' clashes with 'getItem(int)' in 'android.support.v13.app.FragmentPagerAdapter'; attempting to use incompatible return type
I put the android.support.v4.view.ViewPager widget in my activity_main.xml right below the android.support.v7.widget.Toolbar widget.
Look at your FixedTabPagerAdapter constructor, you are trying to catch the reference of android.app.FragmentManager instance while passing the fragment manager of type android.support.v4.app.FragmentManagerwhich are two different classes.
Change the type of FragmentManager in your FixedTabPageAdapter to android.support.v4.app.FragmentManager and it should fix the issue.
For sliding pages with tabs do following.
Download or copy following two files on github and paste your project.
this is same as on developers.google.com except setDistributeEvenly method.
https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabLayout.java
https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabStrip.java
activity_main.xml
<your.package.name.SlidingTabLayout
android:clickable="true"
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</your.package.name.SlidingTabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
MyAdapter.java (Here i used two pages only)
class MyPagerAdapter extends FragmentPagerAdapter
{
String[] title = {"All","Favourites"};
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment=null;
if (position==0)
fragment= new All();
if (position==1)
fragment= new Favourites();
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
tab_view.xml (view of tab only , if you want u can also use ImageView here)
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tab_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:padding="15dp"
android:textStyle="bold"
android:textSize="25dp"
/>
</FrameLayout>
MainActivity.java
private SlidingTabLayout tabLayout;
private ViewPager pager;
tabLayout= (SlidingTabLayout) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
tabLayout.setCustomTabView(R.layout.tab_view,R.id.tab_title);
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabLayout.setDistributeEvenly(true);
tabLayout.setViewPager(pager);

Android Swipe View that doesn't use the whole Activity?

Is it possible to use the ViewPager/Swipe View to create swiping fragments within a section of an activity, rather than taking up the whole activity?
Currently, when I swipe on the activity the whole page changes to the new fragment, instead, I want the fragment that changes to take up a small section on the activity layout.
Activity Layout:
<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="wrap_content"
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</RelativeLayout>
Activity Code:
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
public class DemoActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_skip);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager());
viewPager.setAdapter(pageAdapter);
}
}
ViewPager Class:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PageAdapter extends FragmentPagerAdapter {
public PageAdapter(FragmentManager fragmentManager){
super(fragmentManager);
}
#Override
public Fragment getItem(int i){
switch(i){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
}
return null;
}
#Override
public int getCount(){
return 3;
}
}
Any ideas on how to achieve my goal?

Android Studio: How do I create sliding tab navigation?

I'm fairly new to Android Development so I'm a little lost. I am trying to make a app with a sliding tab type navigation to get to different screens.
I've been googling it for like hours and I can't seem to find anything that breaks it down enough for me to understand. From what I read, I would have to use fragments? Right now, I just have my all my screens as separate activities, which I probably would have to change if I'm trying to do this, right?
I haven't really written any code besides messing with the xml so I don't really have much to show.
Anyway, if someone could explain to me how to do this that would be great!
What's wrong with using the new project wizard and selecting "Tabbed Activity"?
You can use Viewpager with PagerTitleStrip..
<FrameLayout 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="com.example.myapp.ContainerFragment">
<!-- TODO: Update blank fragment layout -->
<android.support.v4.view.ViewPager
android:id="#+id/mPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#android:color/transparent"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
here is the ContainerFragment..
import android.os.Bundle;
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class ContainerFragment extends android.support.v4.app.Fragment {
ViewPager mViewPager;
PagerAdapter pagerAdapter;
public NewsContainerFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_container, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
pagerAdapter = new PagerAdapter(getChildFragmentManager(), this);
mViewPager = (ViewPager) getActivity().findViewById(R.id.mPager);
mViewPager.setAdapter(pagerAdapter);
}
}
Here is the adapter..
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.Log;
public class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FirstFrag();
break;
case 1:
fragment = new SecondFrag();
break;
case 2:
fragment = new ThirdFrag();
break;
default:
break;
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return "FIRST FRAG";
} else if (position == 1) {
return "SECOND FRAG";
} else if (position == 2) {
return "THIRD FRAG";
} else {
return super.getPageTitle(position);
}
}
}

android - findViewById NULL under using Fragment

I do not speak English well. So a lot of it is awkward. Also, I wish me to understand, so I used the google translator.
I had the source code from the Null Pointer Error.
how can I it fix?
First It's Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="ToggleButton" />
<ToggleButton
android:id="#+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="ToggleButton" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager2"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
It's java File
package ui.tab;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.kdevproject.game.mining.R;
public class Tab2Activity extends Fragment{
// newInstance constructor for creating fragment with arguments
public static Tab2Activity newInstance() {
Tab2Activity tab2Activity = new Tab2Activity();
return tab2Activity;
}
// Store instance variables based on arguments passed
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// Inflate the view for the fragment based on layout XML
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_tab2, container, false);
if (container == null) {
return null;
}
ViewPager mViewPager = (ViewPager) getView().findViewById(R.id.pager2);
return view;
}
}
class MyPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 6;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages
#Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for that page
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show FirstFragment
return Tab2_1Activity.newInstance();
case 1:
return Tab2_1Activity.newInstance();
}
return null;
}
// Returns the page title for the top indicator
#Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
Use view instead of getView() in onCreateView to initialize ViewPager :
ViewPager mViewPager = (ViewPager)view.findViewById(R.id.pager2);

Categories

Resources