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 th
Solution 1:
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:
publicclassmyActivityextendsFragmentActivityimplementsISelectItemListener {
// Your variable declarations and methods@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
finalViewPagerpager= (ViewPager) findViewById(R.id.pager);
mPagerAdapter = newScreenSlidePagerAdapter(getSupportFragmentManager());
pager.setAdapter(mPagerAdapter);
pager.setOnPageChangeListener(
newOnPageChangeListener() {
@OverridepublicvoidonPageSelected(int pageSelected) {
finalScreenSlidePageFragmentfragment= mPagerAdapter.getFragment(pageSelected);
if (fragment != null) {
fragment.doTextViewChnges();
}
}
@OverridepublicvoidonPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@OverridepublicvoidonPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
}
privateclassScreenSlidePagerAdapterextendsFragmentStatePagerAdapter {
//Your variable declarationsprivatefinal ScreenSlidePageFragment[] mFragments;
publicScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
mFragments = newScreenSlidePageFragment[NUM_PAGES];
}
@OverridepublicintgetCount() {
return NUM_PAGES;
}
// It will create new ScreenSlidePageFragment by position if it's not exsist@Overridepublic 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 nullpublic ScreenSlidePageFragment getFragment(int position) {
return mFragments[position];
}
}
}
Your Fragment:
publicclassScreenSlidePageFragmentextendsFragment {
//Your variables and methodspublicstatic ScreenSlidePageFragment create(int pageNumber, String[] pages, int bookmarkPageNumber, String storyName, int linesInOnePage) {
finalBundleargs=newBundle();
args.putInt(ARG_PAGE, pageNumber);
args.putStringArray(ARG_PAGEARRAY, pages);
finalScreenSlidePageFragmentfragment=newScreenSlidePageFragment();
fragment.setArguments(args);
return fragment;
}
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
pages = getArguments().getStringArray(ARG_PAGEARRAY);
}
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = (ViewGroup) inflater.inflate(R.layout.activity_story, container, false);
loadView(rootView);
return rootView;
}
publicintgetPageNumber() {
return mPageNumber;
}
// Let's keep logic of modifications in fragmentpublicvoiddoTextViewChnges() {
finalViewview= getView();
// do what you want with your View and his children
}
}
Post a Comment for "Need Help To Fix Code For Getting Current View In Viewpager"