I have created 30 scrollable tabs using tablayout.
So first three tabs are visible on screen and rest of them are invisible which can be scroll using swipe gesture.
The problem is when I am selecting last tab programmatically but it is not get visible (tab layout not get scrolled to last tab).
How can I make tablayout to scroll to last tab?
I found the solution.
First I had found the width of tablayout and scroll it's x position to width and than called the select() method of last tab.
And it works fine.
Below is the code.
mTabLayout.setScrollX(mTabLayout.getWidth());
mTabLayout.getTabAt(lastTabIndex).select();
Updated:
If above is not working you can use the below code as well, it is also working fine.
new Handler().postDelayed(
new Runnable() {
#Override public void run() {
mTabLayout.getTabAt(TAB_NUMBER).select();
}
}, 100);
write this method in your custom tablayout (Your own layout which extends tablayout). So, in future you can use this method whenever you need instad of code duplication
public void selectTabAt(int tabIndex) {
if (tabIndex >= 0 && tabIndex < getTabCount() && getSelectedTabPosition() != tabIndex) {
final Tab currentTab = getTabAt(tabIndex);
if (currentTab != null) {
this.post(new Runnable() {
#Override
public void run() {
currentTab.select();
}
});
}
}
}
If you don't want yo use CustomLayout. you can just do this
final Tab currentTab = mTabLayout.getTabAt(tabIndex);
if(currentTab != null){
mTabLayout.post(new Runnable() {
#Override
public void run() {
currentTab.select();
}
});
}
I found this solution for me:
TabLayout tabLayout = activity.getTabLayout();
tabLayout.setSmoothScrollingEnabled(true);
tabLayout.setScrollPosition(targetChannelPosition, 0f, true);
Also, if you receive this error: "Only the original thread that created a view hierarchy can touch its views.", you can use this code, in order to run on Ui thread:
// find a way to get the activity containing the tab layout
TabLayout tabLayout = activity.getTabLayout();
activity.runOnUiThread(new Runnable()
{
#Override
public void run()
{
TabLayout.Tab tab = tabLayout.getTabAt(targetChannelPosition);
tab.select();
}
});
Are you calling tab.select() before the TabLayout and its children have actually been measured and drawn? If so, your TabLayout won't animate to the selection with tab.select() (or Kayvan N's suggestion of scrollTo()). Using a Handler will probably work, but that's not an ideal solution.
Provided the layout hasn't been laid out yet, a ViewTreeObserver will allow you to move to your selected tab after the layout process is finished.
private void scrollToTabAfterLayout(final int tabIndex) {
if (getView() != null) {
final ViewTreeObserver observer = mTabLayout.getViewTreeObserver();
if (observer.isAlive()) {
observer.dispatchOnGlobalLayout(); // In case a previous call is waiting when this call is made
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
observer.removeOnGlobalLayoutListener(this);
} else {
//noinspection deprecation
observer.removeGlobalOnLayoutListener(this);
}
mTabLayout.getTabAt(tabIndex).select();
}
});
}
}
}
Please comment if you have any suggestions.
The above answer wouldn't work because first As agirardello mentioned you should not use mTabLayout.getWidth() since it doesn't return what we need (which is the position of the child you want to scroll to) and the updated solution doesn't always work because of a bug in TabLayout (reported here) but a work around is simple.
The tabs on the tabLayout are not direct children of the TabLayout so we need to go one level deeper using
((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(YOUR_DESIRED_TAB_INDEX).getRight()
the only child of tabLayout is a TabLayout.SlidingTabStrip which is also a ViewGroup and getRight() will give us the right most position of our desired tab view. Thus scrolling to that position will give us what we desire. Here is a complete code:
int right = ((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(4).getRight();
mTabLayout.scrollTo(right,0);
mTabLayout.getTabAt(4).select();
NOTE: Make sure you are calling these methods after the layout has been drown (like onResume and not onCreate)
Hope this helps.
new Handler().postDelayed(
new Runnable() {
#Override public void run() {
mTabLayout.getTabAt(TAB_NUMBER).select();
}
}, 100);
The code snippet below works for me
class TriggerOnceListener(private val v: View, private val block: () -> Unit) : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
block()
v.viewTreeObserver.removeOnPreDrawListener(this)
return true
}
}
fun onCreate() {
val position = ***The tab position you want to scroll to, 29 for your case here***
tabLayout.let { it.viewTreeObserver.addOnPreDrawListener(TriggerOnceListener(it)
{ it.setScrollPosition(position, 0f, true) } ) }
}
I dived into Tab.select(), and found Android uses Tablayout.setScrollPosition() to do this scrolling. And in onCreate() the widgets have not been measured, you need to postpone the call until layout is complete.
To select the last tab, use
tabLayout.getTabAt(X).select(); where X is the last tab index
If your TabLayout is used in conjunction with a ViewPager, which is common, simply add the following in the onCreate() method in your Activity:
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout);
That some of your tabs are not being shown indicates the tabMode attribute is set to app:tabMode="scrollable".
viewpager.setItem(position) should also set the position of the tab
This solution worked for me. My situation is a little bit different though; in my case, I am using the TabLayout with a ViewPager and adding more views and calling notifyDataSetChange().
The solution is to set a callback on the observer of TabLayout and scroll when the children are actually added to the TabLayout. Here is my example:
/**
Keep in mind this is how I set my TabLayout up...
PagerAdapter pagerAdapter = new PagerAdapter(...);
ViewPager pager = (ViewPager)findViewById(...);
pager.setAdapter(pagerAdapter);
TabLayout tabLayout = (TabLayout)findViewById(...);
tabLayout.setupWithViewPager(pager);
*/
public void loadTabs(String[] topics) {
animateTabsOpen(); // Irrelevant to solution
// Removes fragments from ViewPager
pagerAdapter.clear();
// Adds new fragments to ViewPager
for (String t : topics)
pagerAdapter.append(t, new TestFragment());
// Since we need observer callback to still animate tabs when we
// scroll, it is essential to keep track of the state. Declare this
// as a global variable
scrollToFirst = true;
// Alerts ViewPager data has been changed
pagerAdapter.notifyOnDataSetChanged();
// Scroll to the beginning (or any position you need) in TabLayout
// using its observer callbacks
tabs.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
/**
We use onGlobalLayout() callback because anytime a tab
is added or removed the TabLayout triggers this; therefore,
we use it to scroll to the desired position we want. In my
case I wanted to scroll to the beginning position, but this
can easily be modified to scroll to any position.
*/
if (scrollToFirst) {
tabs.getTabAt(0).select();
tabs.scrollTo(0, 0);
scrollToFirst = false;
}
}
});
}
Here is my code for the PagerAdapter if you need it too lol:
public class PagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragments;
private List<String> titles;
public PagerAdapter(FragmentManager fm) {
super(fm);
this.fragments = new ArrayList<>();
this.titles = new ArrayList<>();
}
/**
* Adds an adapter item (title and fragment) and
* doesn't notify that data has changed.
*
* NOTE: Remember to call notifyDataSetChanged()!
* #param title Fragment title
* #param frag Fragment
* #return This
*/
public PagerAdapter append(String title, Fragment frag) {
this.titles.add(title);
this.fragments.add(frag);
return this;
}
/**
* Clears all adapter items and doesn't notify that data
* has changed.
*
* NOTE: Rememeber to call notifyDataSetChanged()!
* #return This
*/
public PagerAdapter clear() {
this.titles.clear();
this.fragments.clear();
return this;
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public int getItemPosition(Object object) {
int position = fragments.indexOf(object);
return (position >= 0) ? position : POSITION_NONE;
}
}
I wonder if this is answer will be relevant since its coming very late. i actually achieved it in C# using Xamarin.
tabs.GetChildAt(0).Selected = true;
viewPager.SetCurrentItem(0, true);
tab = tabLayout.getSelectedTabPosition();
tab++;
TabLayout.Tab tabs = tabLayout.getTabAt(tab);
if (tabs != null) {
tabs.select();
}
else {
tabLayout.getTabAt(0).select();
}
if you want next tab on click event then use this code its work perfactly
Related
i update recycleView inside viewpager fragments and call pagerAdapter.notifyDataSetChanged(); to update them. It updates all recycleviews and the app takes long time. Now to overcome this problem i want to update only the current visible fragment and on ViewPage change update other fragments with notifyDataSetChanged(); is there a way to tell which fragment to update?
ViewPagerAdapter
private class MyPageAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragments;
private int[] mResources;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
#Override
public int getItemPosition(Object object) {
MyFragment f = (MyFragment) object;
if (f != null) {
f.update();
}
return super.getItemPosition(object);
}
}
this is fragment
public static class MyFragment extends Fragment implements CustomAdapterOdds.OnGameClickListener, Updateable {
public static final String COLUMN_IN_DISPLAY = "column_in_display";
RecyclerView recyclerView, recyclerView2;
HashMap<String, List<OddsFeed>> oddsList1;
HashMap<String, List<OddsFeed>> oddsList2;
int oddsColDisp;
CustomAdapterOdds adapterOdds1, adapterOdds2;
boolean isvisible = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
oddsList1 = new HashMap<>();
oddsList2 = new HashMap<>();
Bundle b = this.getArguments();
oddsColDisp = b.getInt(COLUMN_IN_DISPLAY);
List<GameFeed> leftGames = getArguments().getParcelableArrayList("list");
if (b.getSerializable("hashmap1") != null && b.getSerializable("hashmap2") != null) {
oddsList1 = (HashMap<String, List<OddsFeed>>) b.getSerializable("hashmap1");
oddsList2 = (HashMap<String, List<OddsFeed>>) b.getSerializable("hashmap2");
}
View v = inflater.inflate(R.layout.view_table_viewpager_fragment_layout, container, false);
ArrayList<GameFeed> gameFeedsCol0 = getArguments().getParcelableArrayList("list");
//recycleview1
recyclerView = v.findViewById(R.id.table_view_recycle_view_odds1);
adapterOdds1 = new CustomAdapterOdds(getContext(), leftGames, this, oddsList1, bestSitesList, oddsColDisp);
configRecyclerViewOdds(getContext(), recyclerView, adapterOdds1);
//recycleview2
recyclerView2 = v.findViewById(R.id.table_view_recycle_view_odds2);
adapterOdds2 = new CustomAdapterOdds(getContext(), leftGames, this, oddsList2, bestSitesList, oddsColDisp + 1, true);
configRecyclerViewOdds(getContext(), recyclerView2, adapterOdds2);
return v;
}
#Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (visible) {
isvisible = true;
}
}
#Override
public void onResume() {
super.onResume();
Toast.makeText(getContext(), "onresume", Toast.LENGTH_SHORT).show();
adapterOdds1.updateListOdds(leftGames, oddsList1, oddsColDisp);
adapterOdds2.updateListOdds(leftGames, oddsList2, oddsColDisp + 1);
}
#Override
public void OnGameClickListener(View view, int position) {
}
#Override
public void update() {
Toast.makeText(getContext(), "update", Toast.LENGTH_SHORT).show();
if (isVisible()) {
adapterOdds1.updateListOdds(leftGames, oddsList1, oddsColDisp);
adapterOdds2.updateListOdds(leftGames, oddsList2, oddsColDisp + 1);
updateViewpager++;
int size = bestSitesList.size() / 2;
adapterOdds1.updateListOdds(leftGames, oddsList1, oddsColDisp);
adapterOdds2.updateListOdds(leftGames, oddsList2, oddsColDisp + 1);
if (updateViewpager == size - 1) {
progressBar.setVisibility(View.INVISIBLE);
updateViewpager = 0;
}
}
}
}
I'm sorry this is a bit of rambling answer but there was not enough code example given to make a functioning answer, so more really trying to explain a concept. Really need details of when and how the data is updated BUT...
As you are using androidx, you might want to consider moving to viewpager2 or with viewpager you can change how the fragment lifecycle states are managed.
If you changed to using BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT https://developer.android.com/reference/androidx/fragment/app/FragmentStatePagerAdapter#BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT when you constructed the Adapter https://developer.android.com/reference/androidx/fragment/app/FragmentStatePagerAdapter#FragmentStatePagerAdapter(androidx.fragment.app.FragmentManager,%20int)
Then all the Fragments will only be brought up to "Started" when created or re-created and only the current one on screen with "Resumed" and then "Paused" when moved off screen.
The exact mechanics depends on how the Fragments recyclerviews get their data and what triggers the update but then general idea is for the Fragments onCreateView to create a lightweight "shell" of a layout, I basically have the static buttons/text and create the recyclerview with an empty dataset.
Then in the onResume method of the Fragment which only gets called for the Fragment currently on Screen it calls your update method to replace the two recyclerview's empty datasets with the actual dataset and does a notifyDataSetChanged() on the recyclerviews.
Therefore when the viewpager is initially created X number of Fragments gets created and there static content is laid out plus 1 Fragment (the current one on screen) gets the recyclerview populated with actual data.
You might also then want to put in some optimisation checks in onResume of the Fragment to check the recyclerview views data has actually changed (a simple size check or using Recyclerview's DiffUtils) otherwise as you move between the Fragment's in the viewpager each Fragment will be paused/resumed.
This really only delays the cost of the two recyclerviews in the Fragment until it is really needed (when it is about to be displayed), it's a form of "Lazy loading"
With moving the "dynamic" data to this "lazy loading" is could be possible to remove the need to notifyDataSetChanged on the Fragments BUT the code snippets don't show enough about how and why the recyclerviews content changes.
With this method when the data is drawn is changed and you might not like how it looks.
This is really at a high level an inversion of the Fragments update logic, instead of saying "The data has changed redraw the data in all the Fragments" it is "This fragment is been shown, redraw the data IF it has been changed since the last time I drew it"
What I want:
I have been trying to implement two directional Endless viewpager in Android, Left to Right & Right to Left
What I did:
I have implemented Endless viewpager adapter, it works fine for right to left direction, I have set current item position by viewPager.setCurrentItem(Integer.MAX_VALUE/2);.
Reference:
Help would be appreciate.
Try to check below FragmentPagerAdapter to get endless viewpager adapter :
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return Integer.MAX_VALUE;
}
#Override
public Fragment getItem(int position) {
return getFragmentBasedOnPosition(position);
}
private Fragment getFragmentBasedOnPosition(int position) {
int fragmentPos = position % 3; // Assuming you have 3 fragments
switch(fragmentPos) {
case 0:
return Fragment1.newInstance();
case 1:
return Fragment2.newInstance();
case 2:
return Fragment3.newInstance();
}
}
}
I found solution here.
I hope its helps you.
I have made my own solution. I created a ViewPager that supports infinite looping effect, smart auto-scroll, compatible with any indicators and easy to use. It especially uses it as banners of application with a simple item page.
My custom ViewPager can:
Plug and play, easy to use
Infinite Looping items
Auto-scroll items, allow config, auto-resume/pause when activity/fragment resume/pause
Won't scroll or loop if it has only 1 item
Compatible with many indicators
Github link: https://github.com/kenilt/LoopingViewPager
Hope it helps!
One simple way to achieve this for ViewPager2 is with 3 basic ideas:
Add the first and last items of your data model collection to the end and start, respectively, of that same collection. E.g. listOf(1, 2, 3, 4, 5) should become listOf(5, 1, 2, 3, 4, 5, 1).
When setting up the pager, set it to start with index 1.
When the user scrolls to index 0, have the pager scroll instantly to the penultimate index. When the user scrolls to the last index, have the pager scroll instantly to index 1.
Some sample code to do this is as follows:
1.
private fun <T> List<T>.prepareForTwoWayPaging(): List<T> {
val first = first()
val last = last()
return toMutableList().apply {
add(0, last)
add(first)
}
}
pager.setCurrentItem(1, false)
pager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageScrolled(
position: Int,
positionOffset: Float,
positionOffsetPixels: Int
) {
// We're only interested when the pager offset is exactly centered. This
// will help create a convincing illusion of two-way paging.
if (positionOffsetPixels != 0) {
return
}
when (position) {
0 -> pager.setCurrentItem(adapter.itemCount - 2, false)
adapter.itemCount - 1 -> pager.setCurrentItem(1, false)
}
}
})
Caveat: this code does not reconcile any TabLayout or an empty data model collection.
The proposed solutions are correct but to achieve the result you need to set the initial value of your viewpager to Integer.MAX_VALUE/2.
Anyway, I don't really like this solution, setting getCount to return Integer.MAX_VALUE can have huge impact on application performance.
I figured out a solution in order to avoid this problem using the:
onPageScrollStateChanged Listener
I simply reorder the fragment list, update the viewPager and move to the new page without animation, the result is an endless loop in both directions:
mainViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
Boolean first = false;
Boolean last = false;
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{}
#Override
public void onPageSelected(int position)
{
if (position == 0)
{
first = true;
last = false;
}
else if (position == mainFragmentList.size() -1)
{
first = false;
last = true;
}
else
{
first = false;
last = false;
}
}
#Override
public void onPageScrollStateChanged(int state)
{
if (first && state == ViewPager.SCROLL_STATE_IDLE)
{
// Jump without animation
Fragment fragment = mainFragmentList.get(mainFragmentList.size() -1);
mainFragmentList.remove(mainFragmentList.size() -1 );
mainFragmentList.add(0,fragment);
mainPagerAdapter.setData(mainFragmentList);
mainPagerAdapter.notifyDataSetChanged();
Log.e(TAG,mainFragmentList.toString());
mainViewPager.setCurrentItem(1,false);
}
if(last && state == ViewPager.SCROLL_STATE_IDLE)
{
// Jump without animation
Fragment fragment = mainFragmentList.get(0);
mainFragmentList.remove(0);
mainFragmentList.add(fragment);
mainPagerAdapter.setData(mainFragmentList);
mainPagerAdapter.notifyDataSetChanged();
Log.e(TAG,mainFragmentList.toString());
mainViewPager.setCurrentItem(mainFragmentList.size()-2,false);
}
}
});
This is what happens here:
in this example, we have 4 fragments A-B-C-D
if the user is on fragment A (first), the new List will become: D-A-B-C
[remove the last and push as first]
I update the ViewPager and move (without animation) again to fragment A so index 1.
Now the user can continue to scroll left and will find fragment D.
Same thing with the last fragment:
starting again with A-B-C-D
if the user is on fragment D (last), the new List will become: B-C-D-A
[remove the first and push as last]
I update the ViewPager and move (without animation) again to fragment D so index mainFragmentList.size()-2.
Now the user can continue to scroll right and will find fragment A.
Remember to implement FragmentStatePagerAdapter NOT FragmentPagerAdapter
I seem to be having a strange problem.
My ViewPager is working perfectly fine when I'm swiping it "slowly" (as in, there's a gap between my swipes - even half a second) but if I swipe fast enough (2-3 pages a second), after 3-4 pages, I observe one of these behaviors:
Sometimes it seems to jump back a couple of pages
Or simply show the same page again
Or show a white page (I imagine that's an empty page since my background is white)
Or pins itself between two pages (half of each is shown).
This is my adapter:
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private SparseArray<String> _fragmentHeaders = new SparseArray<String>();
private SparseArray<Fragment> _fragments = new SparseArray<Fragment>();
private Integer _previousCount = null;
private boolean _isDatasetBeingChanged;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
registerDataSetObserver(new DataSetObserver() {
#Override
public void onChanged() {
_previousCount = getContentCount();
super.onChanged();
}
});
}
public Fragment getExistingItem(int position) {
Fragment fragment = _fragments.get(position, null);
if (fragment != null) {
return fragment;
}
return null;
}
#Override
public Fragment getItem(int position) {
TType content = findContentWithPosition(position);
if (_fragmentHeaders.get(position, null) == null) {
_fragmentHeaders.put(position, content.getTitle());
}
Fragment fragment = createFragmentFromType(content);
_fragments.put(position, fragment);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
RoboFragment fragment = (RoboFragment) object;
if (fragment != null) {
fragment.onDestroy();
}
if (_fragmentHeaders.get(position, null) != null) {
_fragmentHeaders.remove(position);
}
if (_fragments.get(position, null) != null) {
_fragments.remove(position);
}
super.destroyItem(container, position, object);
}
#Override
public int getCount() {
int count = getContentCount();
if (_isDatasetBeingChanged) {
return count;
}
if (_previousCount != null && _previousCount != count) {
_isDatasetBeingChanged = true;
notifyDataSetChanged();
}
_previousCount = count;
_isDatasetBeingChanged = false;
return _previousCount;
}
#Override
public CharSequence getPageTitle(int position) {
return _fragmentHeaders.get(position);
}
}
which as I mentioned works if I swipe slowly. And I simply add the adapter like this:
_sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
_viewPager.setAdapter(_sectionsPagerAdapter);
Has anyone seen this before? I'm using rev 22 of the support libraries and everything is up to date in terms of the SDK.
I've tested this behavior on both 5.0 and 4.4 versions and both exhibit this problem.
Many thanks in advance,
p.s. the problem is not with calling notifyDataSetChanged(). Removing it had no effects and this happens even if the underlying page count has not changed.
Update 1:
I came across this Google page that seems to describe the problem I've mentioned here:
http://code.google.com/p/android-developer-preview/issues/detail?id=1190
The only difference is, with or without the CardView I get the same error so I don't think the problem is limited to the card view.
No resolution to this problem though so if anyone can help, I will be more than grateful.
I found out the problem after many hours of debugging into the source code of ViewPager. The issue was the my fragment contained a view that was also listening to touch events. When scrolling on a normal speed, the two would not interfere with each other (there's a Runnable() in the ViewPager I recommend having a look at and to understand its interaction with scrolling and also to understand how the velocity and "page jumping in VP operates) but when I was scrolling fact, the combination of the runnable and the velocity in the VP's onTouch would not play nicely with my view.
The solution was to remove the inner view that was listening to onTouch. While this was an extreme measure to take and made change some rather significant parts of my code and layout, I was simply too afraid to copy the entire VP's source code and change the part that was causing problems.
in your getItem method, do you mean to create a new fragment each time? what is _fragments? Instead try :
#Override
public Fragment getItem(int position) {
Fragment fragment = _fragments.get(position);
if (fragment == null) {
fragment = createFragmentFromType(content);
_fragments.put(position, fragment);
//save your fragment names, or better, create a custom fragment with a getTitle method.
}
return fragment;
}
for your getCount() method try:
#Override
public int getCount() {
return _fragments.size();
}
get rid of your override for destroyItem(), seems overly complicated and unnecessary.
I'm using android.support.v4.view.ViewPager and noticed, that after setting my android.support.v4.app.FragmentStatePagerAdapter on the ViewPager in the onStart()-method of my activity I can't retrieve a view of the currently displayed ViewPager page, because it doesn't seem to be initialized/attached/drawn to the activity yet. I always get a NPE.
Is there an event which is fired, when my ViewPager is fully initialized including its fragment pages and their views?
My code in the onStart()-method of the main activity looks something like this:
ViewPager vp = findViewById(R.id.viewPager);
vp.setAdapter(new FlashCardPagerAdapter(getSupportFragmentManager()));
vp.getChildCount(); //this is still 0 because the child fragments have not been added yet, why? When are they added?
My Adapter looks like this:
public class FlashCardPagerAdapter extends FragmentStatePagerAdapter {
private List<FlashCard> flashCards;
private FlashCardFragment[] flashCardFragments;
public FlashCardPagerAdapter(FragmentManager fm) {
super(fm);
flashCards = FlashCardApp.getInstance().getFlashCards();
flashCardFragments = new FlashCardFragment[flashCards.size()];
}
#Override
public Fragment getItem(int i) {
if (flashCardFragments[i] != null) {
return flashCardFragments[i];
} else {
flashCardFragments[i] = new FlashCardFragment(flashCards.get(i));
return flashCardFragments[i];
}
}
public FlashCardView getFlashCardViewAt(int i) {
return ((FlashCardFragment) getItem(i)).getFlashCardView();
}
#Override
public int getCount() {
return 25;
}
}
Thanks for any help.
EDIT
Thing is, all this works when I call it after the onStart() method, for example in one of my custom button views.
I'm guessing vp.setAdapter(new MyAdapter()); is just a typo and you meant to say vp.setAdapter(new FlashCardPagerAdapter(getSupportFragmentManager()));
If that doesn't work, then it's returning 0 pages for some reason. I would check the adapter's getCount() method to make sure it's returning something more than 0, because that's how the viewpager decides how many pages to show.
I'm using the following example to impliment my viewPager:
http://code.google.com/p/viewpagerexample/issues/list
The problem with this example is that I can't figure out how to set my starting position, the default starting position is 0. Basically I wan't to be able to control if there is an available view on its left or the right.
Is there any way to control center's View current position? is there a better way to do it?
is it possible to make it circular?
I've found a way to set it's position, which is done outside of the class:
awesomePager = (ViewPager) findViewById(R.id.awesomepager);
awesomePager.setAdapter(awesomeAdapter);
awesomePager.setCurrentItem(CurrentPosition);
and it can be limited by calculating the amount of items I want to fit in to it
I have noticed that if you recreate Activity (orientation change) with ViewPager having FragmentStatePagerAdapter, then the Adapter will reuse it's Fragments. The way to stop it is:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
if (viewPager != null) {
// before screen rotation it's better to detach pagerAdapter from the ViewPager, so
// pagerAdapter can remove all old fragments, so they're not reused after rotation.
viewPager.setAdapter(null);
}
super.onSaveInstanceState(savedInstanceState);
}
but then after Activity recreation ViewPager alwayes opens page 0 first and setCurrentItem(CurrentPosition); doesn't work. Fix for that is changing page after delay:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
viewPager.setCurrentItem(newPosition);
}
}, 100);
To start with the last fragment I did this:
PagerAdapter pagerAdapter = new PagerAdapter();
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(pagerAdapter.getCount() - 1);
I came across a problem whereby if I set the current item before I set the adapter, the first item I get back will always be the one at position 0.
Make sure you do:
awesomePager.setAdapter(awesomeAdapter);
awesomePager.setCurrentItem(CurrentPosition);
and not:
awesomePager.setCurrentItem(CurrentPosition);
awesomePager.setAdapter(awesomeAdapter);
I have an array of size more than 1000 and in dymanic viewpager I was facing leftswipe stuck on firstload. The below code solved this and resulted in smooth scroll:
#Override
onResume(){
super.onResume();
viewPager.setCurrentItem(newPosition);
}
I'm using 3 fragments and on starting my app, the second (middle) fragment will be shown by default. Just I'm using the onResume function and all works great.
#Override
protected void onResume() {
super.onResume();
m_viewPager.setCurrentItem(1);
}
Using viewPager.setCurrentItem(newPosition); shows to the user the transition from the starting page to the newPosition, to prevent that from happening and show the newPosition directly as if it was the starting point, I added false to the second parameter something like this:
int newPosition = pages.size()-1; // Get last page position
viewPager.setCurrentItem(newPosition, false); // 2nd parameter (false) stands for "smoothScroll"
I encountered same problem.
When I initialize ViewPager, the indicator position was 0.
This may depends on amount of calculating for Pager contents. I use ViewTreeObserver like below.
mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mViewPager.setCurrentItem(newPosition);
removeOnGlobalLayoutListener(mSlidingTabLayout.getViewTreeObserver(), mGlobalLayoutListener);
}
};
mSlidingLayout.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);
and,
private void removeOnGlobalLayoutListener(ViewTreeObserver observer, ViewTreeObserver.OnGlobalLayoutListener listener) {
if (observer == null) return;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
observer.removeGlobalOnLayoutListener(listener);
} else {
observer.removeOnGlobalLayoutListener(listener);
}
}
In this way, also never to bother with time setting of delay.
#Override
public Object instantiateItem(ViewGroup container, int position) {
View currentPage = null;
switch(position){
case 0:
currentPage = LayoutInflater.from(context).inflate(R.layout.page0, null)
break;
case 1:
currentPage = LayoutInflater.from(context).inflate(R.layout.page1, null)
///////////// This page will be default ////////////////////
((ViewPager)container).setCurrentItem(position);
////////////////////////////////////////////////////////////
break;
case 2:
currentPage = LayoutInflater.from(context).inflate(R.layout.page2, null)
break;
return currentPage;
}