TabLayout tab selection

If you know the index of the tab you want to select, you can do it like so: TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); TabLayout.Tab tab = tabLayout.getTabAt(someIndex); tab.select(); This technique works even if you’re using the TabLayout by itself without a ViewPager (which is atypical, and probably bad practice, but I’ve seen it done).

How to do circular scrolling on ViewPager?

This is a solution without fake pages and works like a charm: public class CircularViewPagerHandler implements ViewPager.OnPageChangeListener { private ViewPager mViewPager; private int mCurrentPosition; private int mScrollState; public CircularViewPagerHandler(final ViewPager viewPager) { mViewPager = viewPager; } @Override public void onPageSelected(final int position) { mCurrentPosition = position; } @Override public void onPageScrollStateChanged(final int state) { handleScrollState(state); … Read more

IllegalStateException: The application’s PagerAdapter changed the adapter’s content without calling PagerAdapter#notifyDataSetChanged

I had a hard time making my ViewPager working. At the end, it seems that the example in the documentation is wrong. The addTab method should be as follows: public void addTab(Tab tab, Class<?> clss, Bundle args) { TabInfo info = new TabInfo(clss, args); tab.setTag(info); tab.setTabListener(this); mTabs.add(info); notifyDataSetChanged(); mActionBar.addTab(tab); } Notice the order of the … Read more

Android ViewPager setCurrentItem not working after onResume

i can’t really answer WHY exactly this happens, but if you delay the setCurrentItem call for a few milliseconds it should work. My guess is that because during onResume there hasn’t been a rendering pass yet, and the ViewPager needs one or something like that. private ViewPager viewPager; @Override public void onResume() { final int … Read more

onClick on ViewPager not triggered

I solved a similar problem by using a GestureDetector Sending the MotionEvent to the GestureDetector tapGestureDetector = new GestureDetector(this, new TapGestureListener()); viewPager.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { tapGestureDetector.onTouchEvent(event); return false; } }); It you are using the compatibility library, you can change the first line to: tapGestureDetector = new GestureDetectorCompat(this, new … Read more

How to test if a fragment view is visible to the user?

This is what I use to determine the visibility of a fragment. private static boolean m_iAmVisible; @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); m_iAmVisible = isVisibleToUser; if (m_iAmVisible) { Log.d(localTAG, “this fragment is now visible”); } else { Log.d(localTAG, “this fragment is now invisible”); } }

tech