android - Using same fragment class in ViewPager - android

I have a ViewPager that I want to use same Fragment in the each page with different variables. But when I do this, only one page of ViewPager is viewing and other pages are blank. How can i do this? I can't create different layout for each tab because I want to add them dynamically.
ViewPagerAdapter:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
class FragmentStoreTabPageAdapter extends FragmentPagerAdapter {
String[] pageTitles = {"Y","Z","X"};
public FragmentStoreTabPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return FragmentStorePages.newInstance(pageTitles[position],position);
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return pageTitles[position];
}
}
Main Fragment that contains the ViewPager:
public class FragmentStore extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_store,container,false);
ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager_store);
viewPager.setAdapter(new FragmentStoreTabPageAdapter(getChildFragmentManager()));
TabPageIndicator tpi = (TabPageIndicator) view.findViewById(R.id.indicator_store);
tpi.setViewPager(viewPager);
return view;
}
}
The Fragment that uses in ViewPager:
public class FragmentStorePages extends Fragment implements ReplaceListener {
String categoryName;
int categoryId;
public static FragmentStorePages newInstance(String categoryName, int categoryId){
FragmentStorePages fsp = new FragmentStorePages();
Bundle args = new Bundle();
args.putString("categoryName", categoryName);
args.putInt("categoryId", categoryId);
fsp.setArguments(args);
return fsp;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_store_pages,container,false);
Bundle args = getArguments();
categoryName = args.getString("categoryName");
categoryId = args.getInt("categoryId");
getFragmentManager().beginTransaction().replace(R.id.fragment_store_pages,FragmentStoreSubCategories.newInstance(categoryName),"SubCategoryFragment").commit();
return view;
}
#Override
public void onReplace(Bundle args) {
}
}

Use This PagerAdapter
class FragmentStoreTabPageAdapter extends FragmentPagerAdapter {
String[] pageTitles = {"Y","Z","X"};
public FragmentStoreTabPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
FragmentStorePages fsp = new FragmentStorePages();
Bundle args = new Bundle();
args.putString("categoryName", categoryName);
args.putInt("categoryId", categoryId);
fsp.setArguments(args);
return fsp;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return pageTitles[position];
}
}
Use This Fragment
public class FragmentStorePages extends Fragment implements ReplaceListener {
String categoryName;
int categoryId;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_store_pages,container,false);
Bundle args = getArguments();
if(args!=null && args.containsString("categoryName")&& args.containsString("categoryId")){
categoryName = args.getString("categoryName");
categoryId = args.getInt("categoryId");
}
return view;
}
#Override
public void onReplace(Bundle args) {
}
}

Related

How to set few fragments that have same structure in viewpager?

I'm a beginner who learning Android basic course
I want to make 6 fragments that have same structure (only title, image, texts will be different) by using single fragment.
I tried this code but I can see only one fragment.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.fragment_container);
MoviePagerAdapter adapter = new MoviePagerAdapter(getSupportFragmentManager());
mainfragment = new MainFragment();
adapter.addItem(mainfragment);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(6);
}
ViewPager Adapter (in MainActivity)
class MoviePagerAdapter extends FragmentStatePagerAdapter {
public ArrayList<Fragment> items = new ArrayList<Fragment>();
#Override
public Fragment getItem(int position) {
return items.get(position);
}
public MoviePagerAdapter(#NonNull FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
public void addItem(Fragment item) {
items.add(item);
}
#Override
public int getCount() {
return items.size();
}
}
MainFragment.java
public class MainFragment extends androidx.fragment.app.Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.main_frargment, container,
false);
//...
'i want to make 6fragments with datas below '
movieData = new MovieData("title1", "rate1", "grade1");
addData(movieData);
movieData = new MovieData("title2", "rate2", "grade2");
addData(movieData);
movieData = new MovieData("title3", "rate3", "grade3");
addData(movieData);
movieData = new MovieData("title4", "rate4", "grade4");
addData(movieData);
movieData = new MovieData("title5", "rate5", "grade5");
addData(movieData);
movieData = new MovieData("title6", "rate6", "grade6");
addData(movieData);
return rootView;
}
public void addData(MovieData movieData){
Bundle bundle = new Bundle();
bundle.putParcelable("data", movieData);
MainFragment mainFragment = new MainFragment();
mainFragment.setArguments(bundle);
title.setText(movieData.title);
reservation_rate.setText(movieData.getReservation_rate());
grade.setText(movieData.getGrade());
image.setImageResource(R.drawable.image6);
}
}
change your code like this:
public class MainActivity extends AppCompatActivity {
private MoviePagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.fragment_container);
adapter = new MoviePagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(6);
for(int i =0;i<6;i++){
createFrag(i);
}
}
}
private void createFrag(int number) {
MainFragment fragment = new MainFragment();
Bundle args = new Bundle();
args.putInt("NUMBER",number);
fragment.setArguments(args);
adapter.addItem(fragment);
adapter.notifyDataSetChanged();
}
class MoviePagerAdapter extends FragmentStatePagerAdapter {
public ArrayList<Fragment> items = new ArrayList<Fragment>();
#Override
public Fragment getItem(int position) {
return items.get(position);
}
public MoviePagerAdapter(#NonNull FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
public void addItem(Fragment item) {
items.add(item);
}
#Override
public int getCount() {
return items.size();
}
}
and change your Fragment like this:
public class MainFragment extends androidx.fragment.app.Fragment {
//1st
private int number;
#Nullable
//this is variant 1 - with static Movie data
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.main_frargment,
container,
false);
//...
//2d
assert getArguments() != null;
number = getArguments().getInt("NUMBER");
switch (number) {
case 0:
MmovieData movieData1 = new MovieData("title1", "rate1",
"grade1");
addData(movieData1);
break;
case 1:
MmovieData movieData2 = new MovieData("title2", "rate2",
"grade2");
addData(movieData2);
break;
//...
}
return rootView;
}
public void addData(MovieData movieData){
title.setText(movieData.title);
reservation_rate.setText(movieData.getReservation_rate());
grade.setText(movieData.getGrade());
image.setImageResource(R.drawable.image6);
}
}
and this is variant 2 ( with dynamic data and listener)
public class MainFragment extends androidx.fragment.app.Fragment {
//1st
private int number;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.main_frargment,
container,
false);
//...
//2d
assert getArguments() != null;
number = getArguments().getInt("NUMBER");
return rootView;
}
//some method of listener changed
public void dataChanged(MovieData movieData, int position){
if(number==position){
title.setText(movieData.title);
reservation_rate.setText(movieData.getReservation_rate());
grade.setText(movieData.getGrade());
image.setImageResource(R.drawable.image6);
}
}

ViewPager inside tab fragment

I have 3 tabs each are sharing a single fragment. But this fragment contains a viewPager. So all the three tabs will have a UI with a viewPager and viewPager have three different fragments. After following the examples available I managed to implement Tab layout with single fragment. But when I tried to implement a viewPager inside this fragment I am facing issue. Tab is not scrolling smoothly and its not moving to third tab. I am also not able to see my viewPager fragment.
Main Fragment with tablayout implementation:
public class DashboardFragment extends android.support.v4.app.Fragment {
Activity mActivity;
private TabLayout tabLayout;
private ViewPager viewPager;
private static final String TAG = "DashboardFragment";
public DashboardFragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = getActivity();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dashboard_fragment, container, false);
tabLayout = (TabLayout)view.findViewById(R.id.tabs);
viewPager = (ViewPager)view.findViewById(R.id.viewpager);
tabLayout.addTab(tabLayout.newTab().setText("Yesterday"));
tabLayout.addTab(tabLayout.newTab().setText("Last Week"));
tabLayout.addTab(tabLayout.newTab().setText("This Month"));
setupViewPager(viewPager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
DashboardTabPagerAdapter adapter = new DashboardTabPagerAdapter(((AppCompatActivity)getActivity()).getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
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) {
}
});
}
}
DashboardPagerAdapter:
public class DashboardTabPagerAdapter extends FragmentStatePagerAdapter {
private int mNoOfTabs;
public DashboardTabPagerAdapter(FragmentManager fm, int tabCount) {
super(fm);
mNoOfTabs=tabCount;
}
#Override
public Fragment getItem(int position) {
DashboardYesterdayFragment fragment = new DashboardYesterdayFragment();
Bundle args = new Bundle();
switch (position) {
case 0:
args.putSerializable(AppConstants.TAB_DASHBOARD_TYPE, AppConstants.TAB_DASHBOARD_YESTERDAY);
break;
case 1:
args.putSerializable(AppConstants.TAB_DASHBOARD_TYPE, AppConstants.TAB_DASHBOARD_LAST_WEEK);
break;
case 2:
args.putSerializable(AppConstants.TAB_DASHBOARD_TYPE, AppConstants.TAB_DASHBOARD_THIS_MONTH);
break;
default:
return null;
}
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return mNoOfTabs;
}
}
TabFragment which contains a childViewPager:
public class DashboardYesterdayFragment extends android.support.v4.app.Fragment {
private String title;
private Bundle bundle;
ViewPager mViewPager;
public DashboardYesterdayFragment(){
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
bundle = getArguments();
}
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dashboard_yesterday_layout, container, false);
mViewPager = view.findViewById(R.id.vpPager);
setupViewPager(mViewPager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
DashboardViewPagerAdapter adapter = new DashboardViewPagerAdapter( getChildFragmentManager(),bundle);
mViewPager.setAdapter(adapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
mViewPager.setPageMargin(24);
}
ChildViewPagerAdapter:
public class DashboardViewPagerAdapter extends FragmentStatePagerAdapter {
private static int NUM_ITEMS = 3;
private Bundle bundle;
public DashboardViewPagerAdapter(FragmentManager fm, Bundle bundle) {
super(fm);
this.bundle=bundle;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return IncidentsFragment.newInstance(bundle);
case 1:
return VehicleUtilizationFragment.newInstance(bundle);
case 2:
return FleetPerformanceFragment.newInstance(bundle);
default:
return null;
}
}
#Override
public int getCount() {
return NUM_ITEMS;
}
}
ChildViewPager first fragment:
public class IncidentsFragment extends Fragment {
private TextView mOverSpeeding,mGeoFence,mIdlingCase,mFuelFilling,mMosetIncidences, mLeastIncidences;
public static IncidentsFragment newInstance(Bundle bundle) {
IncidentsFragment fragmentFirst = new IncidentsFragment();
Bundle args = new Bundle();
args.putBundle("bundle", bundle);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.incident_layout, container, false);
initViews(view);
return super.onCreateView(inflater, container, savedInstanceState);
}
private void initViews(View view) {
mOverSpeeding = (TextView)view.findViewById(R.id.over_speeding_value);
mGeoFence = (TextView)view.findViewById(R.id.geofence_value);
mIdlingCase = (TextView)view.findViewById(R.id.idling_value);
mFuelFilling = (TextView)view.findViewById(R.id.fuel_value);
mMosetIncidences = (TextView)view.findViewById(R.id.vehicle_reg_no);
mLeastIncidences = (TextView)view.findViewById(R.id.least_incidences_value);
}
}
I am confused what is going wrong.
Change this:
DashboardTabPagerAdapter adapter = new DashboardTabPagerAdapter(((AppCompatActivity)getActivity()).getSupportFragmentManager(),tabLayout.getTabCount());
to this:
DashboardTabPagerAdapter adapter = new DashboardTabPagerAdapter(((AppCompatActivity)getActivity()).getChildFragmentManager(),tabLayout.getTabCount());

nestedviewpager nested fragment error

I have a Viewpager contains Recylerview which has 2 different holders. One of them is also view pager. Hence I have nested viewpager nad nested fragments. When I tap the second tab and tap back the first tab it is fine .But when I tap the third tab and tap back the first tab. It gives following error:
java.lang.IllegalArgumentException: No view found for id 0x7f0f00ce (:id/viewPagerComments) for fragment CommentFragment
MainAcitivty
viewPager.setAdapter(new MainViewPagerAdapter(getSupportFragmentManager(),models.size()));
navigationTabBar.setModels(models);
navigationTabBar.setViewPager(viewPager, 0);
MainViewPagerAdapter
private int size;
public MainViewPagerAdapter(FragmentManager fm, int size) {
super(fm);
this.size = size;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return DashboardtFragment.newInstance(size, position);
default:
return TempFragment.newInstance(size, position);
}
}
DashboardtFragment
public static DashboardtFragment newInstance(int size, int index) {
Bundle args = new Bundle();
args.putInt(ARG_I,index);
args.putInt(ARG_S,size);
DashboardtFragment fragment = new DashboardtFragment();
fragment.setArguments(args);
return fragment;
}
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
final View v = inflater.inflate(R.layout.dashboard_fragment, null);
return v;
}
#Override
public void showRecyleview(DashboardResponseModel model) {
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
DashboardRecyleViewAdapter adapter = new DashboardRecyleViewAdapter(getActivity(),getChildFragmentManager(),model);
recyclerView.setAdapter(adapter);
}
DashboardRecyleViewAdapter
public static class ViewHolder1 extends RecyclerView.ViewHolder {
public ViewHolder1(View itemView) {
super(itemView);
viewPager = itemView.findViewById(R.id.viewPagerComments);
}
}
case 1 :
return new ViewHolder1( layoutInflater.inflate(R.layout.dashboard_comments, parent,
false));
case 1:
ViewHolder1 viewHolder1 = (ViewHolder1) holder;
viewPager.setAdapter(new CommentViewPagerAdapter(
childFragmentManager,
model.getCommentList().size(),
model.getCommentList()
));
break;
CommentViewPagerAdapter
public CommentViewPagerAdapter(FragmentManager fm, int size,
List<CommentList> list) {
super(fm);
this.size = size;
this.list=list;
}
#Override
public Fragment getItem(int position) {
return CommentFragment.newInstance(size,position,list.get(position));
}
CommentFragment
public static CommentFragment newInstance(int size, int index, CommentList
comment) {
Bundle args = new Bundle();
args.putString(ARG_C,new Gson().toJson(comment));
args.putInt(ARG_I,index);
args.putInt(ARG_S,size);
CommentFragment fragment = new CommentFragment();
fragment.setArguments(args);
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState)
{
final View v = inflater.inflate(R.layout.comment_content, null);
}
PagerAdapter mPagerAdapter;
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
super.setAdapter(null);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (mPagerAdapter != null) {
super.setAdapter(mPagerAdapter);
}
}
#Override
public void setAdapter(PagerAdapter adapter) {
}
public void storeAdapter(PagerAdapter pagerAdapter) {
mPagerAdapter = pagerAdapter;
}
first create a customviewpager than add the above code. Use storeAdapter instead of setAdapter.

Updating data in viewpager

How i can update data in my case
public class SpaFragment extends Fragment {
public SpaFragment(){}
private SpaList mSpa;
private TabHost tabs;
private View rootView;
private ViewPager pager;
private PagerAdapter pagerAdapter;
public void setSpa(SpaList s)
{
mSpa=s;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.spa_tab_fragment, container, false);
FragmentManager f = ((FragmentActivity) getActivity()).getSupportFragmentManager();
pagerAdapter = new MyPagerAdapter( f );
pager = (ViewPager) rootView.findViewById(R.id.pager);
pager.setAdapter(pagerAdapter);
pagerAdapter.notifyDataSetChanged();
Log.d(""+mSpa.getSpaName(),"");
return rootView;
}
private class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public CharSequence getPageTitle(int position) {
Resources res = getResources();
String[] title = res.getStringArray(R.array.title_spa_viewpager_array);
return title[position];
}
#Override
public android.support.v4.app.Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment.newInstance(mSpa.getSpaName());
case 1: return SecondFragment.newInstance(mSpa.getSpaAddr());
case 2: return SecondFragment.newInstance(mSpa.getCityEngName());
default: return FirstFragment.newInstance(mSpa.getSpaPhone());
}
}
#Override
public int getCount() {
return 3;
}
}
}
and fragment code
public class FirstFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.viewpager_tab_text, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
tv.setText(getArguments().getString("msg"));
return v;
}
public static FirstFragment newInstance(String text) {
FirstFragment f = new FirstFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}

need help to fix code for getting current view in viewpager

Need help to fix a code I saw in a https://stackoverflow.com/a/9275732/1938357. The solution I need is get the current view onany page in my viewpager based application
Below is the code for reference
public View getCurrentView(ViewPager pager) {
for (int i = 0; i < pager.getChildCount(); i++) {
View child = pager.getChildAt(i);
if (child.getX() <= pager.getScrollX() + pager.getWidth()
&& child.getX() + child.getWidth() >= pager.getScrollX() + pager.getWidth()) {
return child;
}
}
return getChildAt(0);
I am trying to access the current view in the view pager and used the code above. It works fine when I scroll forwards. But when I scroll backwards then after 2 backward scrolls this stops working.
Public class myActivity extends FragmentActivity
//Variable declarations
protected void onCreate(Bundle savedInstanceState) {
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
View CurrView;
OnPageChangeListener pageChangelistener = new OnPageChangeListener() {
#Override
public void onPageSelected(int pageSelected) {
doTextViewChnges();//access the text view and update it based on pageSelected
---THIS IS WHERE I AM STUCK IN TRYING TO GET THE TEXTVIEW IN MY CURRENT FRAGMWNT/VIEW-------
}
mPager.setOnPageChangeListener(pageChangelistener);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position, <other parameters I want to pass>);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
And below is my ScreenSlidePageFragment class
public class ScreenSlidePageFragment extends android.support.v4.app.Fragment {
public static final String ARG_PAGE = "pagenumber";
public static final String ARG_PAGEARRAY = "pages";
private int mPageNumber;
//Declare other variables
#SuppressLint("NewApi")
public static ScreenSlidePageFragment create(int pageNumber, String[] pages, int bookmarkPageNumber, String storyName, int linesInOnePage) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
args.putStringArray(ARG_PAGEARRAY, pages);
fragment.setArguments(args);
return fragment;
}
#SuppressLint("NewApi")
public ScreenSlidePageFragment() {
}
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
pages = getArguments().getStringArray(ARG_PAGEARRAY);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = (ViewGroup) inflater
.inflate(R.layout.activity_story, container, false);
//load layout with all components filled up for that page
loadView(rootView);
return rootView;
}
public int getPageNumber() {
return mPageNumber;
}
It is very hard to discuss in comments with example of code.
As I see in grepcode FragmentStatePagerAdapter.setPrimaryItem(), receive object as android.support.v4.app.Fragment. But you are receive ClassCastException when trying to cast object as Fragment. Can you check your casting: do you really cast object in android.support.v4.app.Fragment not in android.app.Fragment? If all right, add your exception message to your question, please.
UPDATE:
If your ScreenSlidePagerAdapter has method getCurrentView(), then your method 'getCurrentView()' can work like this:
public View getCurrentView(ViewPager pager) {
return ((ScreenSlidePagerAdapter) pager.getAdapter()).getCurrentView();
}
And method getCurrentView() of your ScreenSlidePagerAdapter:
public View getCurrentView() {
return currView;
}
UPDATE 2:
I've made some modification with your code:
Your Activity with private Adapter:
public class myActivity extends FragmentActivity implements ISelectItemListener {
// Your variable declarations and methods
#Override
protected void onCreate(Bundle savedInstanceState) {
final ViewPager pager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
pager.setAdapter(mPagerAdapter);
pager.setOnPageChangeListener(
new OnPageChangeListener() {
#Override
public void onPageSelected(int pageSelected) {
final ScreenSlidePageFragment fragment = mPagerAdapter.getFragment(pageSelected);
if (fragment != null) {
fragment.doTextViewChnges();
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
//Your variable declarations
private final ScreenSlidePageFragment[] mFragments;
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
mFragments = new ScreenSlidePageFragment[NUM_PAGES];
}
#Override
public int getCount() {
return NUM_PAGES;
}
// It will create new ScreenSlidePageFragment by position if it's not exsist
#Override
public android.support.v4.app.Fragment getItem(int position) {
if (getFragment(position) == null) {
mFragments[position] = ScreenSlidePageFragment.create(position, <other parameters I want to pass>);
}
return getFragment(position);
}
// It will give you ScreenSlidePageFragment if it's exists, or null
public ScreenSlidePageFragment getFragment(int position) {
return mFragments[position];
}
}
}
Your Fragment:
public class ScreenSlidePageFragment extends Fragment {
//Your variables and methods
public static ScreenSlidePageFragment create(int pageNumber, String[] pages, int bookmarkPageNumber, String storyName, int linesInOnePage) {
final Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
args.putStringArray(ARG_PAGEARRAY, pages);
final ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
pages = getArguments().getStringArray(ARG_PAGEARRAY);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = (ViewGroup) inflater.inflate(R.layout.activity_story, container, false);
loadView(rootView);
return rootView;
}
public int getPageNumber() {
return mPageNumber;
}
// Let's keep logic of modifications in fragment
public void doTextViewChnges() {
final View view = getView();
// do what you want with your View and his children
}
}

Categories

Resources