I am making a book reading application. Each chapter is an entry in drawer list. I have multiple view pagers, each corresponding to an item in drawer list. How to launch next view pager (next chapter) on reaching to end of previous one.
Post some code to understand the context.
But as a general solution, we can add a page dynamically to viewpager while swiping, instead of adding a new viewpager.
This seems to be ugly but you can try changing the visibility of the viewpager when required.
When you finish the first viewpager i.e. viewpager1,set:
viewpager1.setVisibility(View.GONE);
viewpager2.setVisibility(View.VISIBLE);
P.S.: Initially set all viewpagers .setVisibility(View.GONE); except the first one.
Related
I want to animate a view with SharedElement transition from one Fragment to another fragment in ViewPager.
I've 3 fragments in a ViewPager, each fragment is having a single TextView with same transition name in XML file.
Now, when ViewPager slides another Fragment, I want to see SharedElement in action.
I’ve had the same issue. Standard shared element transitions are not working between ViewPager pages.
So I wrote a Shared Element View Pager library. It works both for transitions caused by onClick() events (such as viewPager.setCurrentItem(x) ) and page slides caused by user. The animation is bound to finger movement.
It's not an android transition so there could be some obstacles. Take a look though.
SharedElementPageTransformer demo
Usage
Add all fragments from your ViewPager to the List in the same order.
ArrayList<Fragment> fragments = new ArrayList<>();
fragments.add(hello_fragment);
fragments.add(small_picture_fragment);
Create SharedElementPageTransformer presumably in Activity.onCreate().
this refers to activity:
SharedElementPageTransformer transformer =
new SharedElementPageTransformer(this, fragments);
Add shared transition by passing pairs of view ids, that need to be linked together
transformer.addSharedTransition(R.id.smallPic_image_cat, R.id.bigPic_image_cat);
Set our transformer to ViewPager's pageTransformer AND onPageChangeListener.
viewPager.setPageTransformer(false, transformer);
viewPager.addOnPageChangeListener(transformer);
it's been maybe a too long time but could be helpful though.
It's totally possible to have shared elements transitions between 2 viewPagers since i already did it myself.
I think the thing you are maybe missing is that transition name has to be unique !
So it has to be set programmatically and not in XML ! It's very important in ViewPager since same view could be inflated many times.
imageView.setTransitionName("unique_name");
This way the transition knows on what element it should be played;)
I've been trying to figure this out for the past week. I am an absolute beginner and have been learning stuff on my own.
So i have this ViewPager that displays fragments that have the same layout: an ImageView at the top, and a TextView at the center.
When I swipe between fragments, the imageviews and textviews get animated together and that's fine.
There are a sections however, where i want the textviews to swipe, but the image views to stay put, but will go back to swipe after one or two fragment swipes
Please take a look at this illustration:
Think of it like an iOS contacts list where a letter of the alphabet, for example "A", stays put as a header when swiping through names that begin with that letter.
I've made a PageTransformer work for the whole viewpager but when i try to do it between ImageViews, it sort of works but everything gets messed up and eventually crashes.
You can try viewpager within viewpager for the textView.
Similar question: ViewPager inside ViewPager.
Another way that I can think of is to register a listener to the textview and if the user pressed down and scroll it will check if there are more text. If there is the fragment will be replaced by the next text.
Example:
text.setOnClickListener(new View.OnClickListener()
{
ACTION.DOWN:
if(there is more text)
{
//replaced text fragment with new text;
}
}
I just added a viewpager inside a fragment, that is inside another viewpager without adding extra code to the main activity. It worked just the way i wanted it to, albeit unexpectedly. But now my navigation button skips some pages inside the nested viewpager.
Apparently, this feature has been available for quite some time.
Alright, I encountered some troubles to release my code yesterday. I'm very interested in this effect, so I've practiced this and successfully to accomplished. The whole project was publish in my github, following is my effort's result.
I am using ViewPager with 3 pages , each holding a listfragment. I place a footer for each listfragment's listview. Now, when I go to 3rd tab and then return to first, the footer is missing and the scroll listener also stops firing. I can get the scroll listener started again by binding the listener in onActivityCreated, but since list.addFooterView doesn't work after setListAdapter, I am not able to put in the footer view again into the listview.
Any help is much appreciated.
If you will only ever have 3 pages in your ViewPager, the easiest solution I can think of is to call viewPager.setOffscreenPageLimit(2). The first fragment will no longer move beyond the offscreen page limit when you visit the third fragment, so it will never be removed from the ViewPager.
once again, I am following this tutorial: androidhive - tab layout with swipe able views . All 3 fragment tabs are loaded with different information and logic and layouts. I would like to refresh each fragment when their tabs have been clicked as it auto loads the fragments from start as it uses the view pager. Is there any simplest way out that I could load only a/1 fragment using view pager? Or to reload the fragments? Thanks for your help!
Try to add this to your viewpager:
MY_VIEWPAGER.setOffscreenPageLimit(0);
Check out the documentation.
Pages beyond this limit will be recreated from the adapter when needed.
The limit of 0 will cause a recreation of the fragment when you open a tab.
I'm developing an app, which consist of:
1. main activity, which has action bar, list fragment and some buttons at bottom
2. when you click on some item, another activity is launched - some buttons at top and webview under it.
I have everything working, but now I have to implement swiping between items from list, and I cant figure out how to swipe between activities. I though of changing items into fragments but then I have no idea, how layout should be worked out. I've read blog about viewpager, but as I understood, you can swipe only between 5 tabs with it, and I have 20+ items in listfragment. any ideas? thanks
create one Activity with ViewPager as it's main control. change items into fragments and add them to the ViewPager via FragmentAdapter
You can go ahead and user viewpager. there is no arbitrary limit to the number of pages.
It would be the quickest solution for you and seems to cover what you are doing.