T getField(Class targetClass, Object instance, String fieldName, Cla
* change the field value
*
* @param targetClass
- * @param instance the filed owner
+ * @param instance the filed owner
* @param fieldName
* @param value
*/
private void setField(Class targetClass, Object instance, String fieldName, Object value) {
try {
- Field field = field = targetClass.getDeclaredField(fieldName);
+ Field field = targetClass.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(instance, value);
} catch (NoSuchFieldException e) {
@@ -420,4 +538,142 @@ private void setField(Class targetClass, Object instance, String fieldName, Obje
}
}
+ /**
+ * This method will link the given ViewPager and this BottomNavigationViewEx together so that
+ * changes in one are automatically reflected in the other. This includes scroll state changes
+ * and clicks.
+ *
+ * @param viewPager
+ */
+ public void setupWithViewPager(@Nullable final ViewPager viewPager) {
+ setupWithViewPager(viewPager, false);
+ }
+
+ /**
+ * This method will link the given ViewPager and this BottomNavigationViewEx together so that
+ * changes in one are automatically reflected in the other. This includes scroll state changes
+ * and clicks.
+ *
+ * @param viewPager
+ * @param smoothScroll whether ViewPager changed with smooth scroll animation
+ */
+ public void setupWithViewPager(@Nullable final ViewPager viewPager, boolean smoothScroll) {
+ if (mViewPager != null) {
+ // If we've already been setup with a ViewPager, remove us from it
+ if (mPageChangeListener != null) {
+ mViewPager.removeOnPageChangeListener(mPageChangeListener);
+ }
+ }
+
+ if (null == viewPager) {
+ mViewPager = null;
+ super.setOnNavigationItemSelectedListener(null);
+ return;
+ }
+
+ mViewPager = viewPager;
+
+ // Add our custom OnPageChangeListener to the ViewPager
+ if (mPageChangeListener == null) {
+ mPageChangeListener = new BottomNavigationViewExOnPageChangeListener(this);
+ }
+ viewPager.addOnPageChangeListener(mPageChangeListener);
+
+ // Now we'll add a navigation item selected listener to set ViewPager's current item
+ OnNavigationItemSelectedListener listener = getOnNavigationItemSelectedListener();
+ mMyOnNavigationItemSelectedListener = new MyOnNavigationItemSelectedListener(viewPager, this, smoothScroll, listener);
+ super.setOnNavigationItemSelectedListener(mMyOnNavigationItemSelectedListener);
+ }
+
+ /**
+ * A {@link ViewPager.OnPageChangeListener} class which contains the
+ * necessary calls back to the provided {@link BottomNavigationViewEx} so that the tab position is
+ * kept in sync.
+ *
+ *
This class stores the provided BottomNavigationViewEx weakly, meaning that you can use
+ * {@link ViewPager#addOnPageChangeListener(ViewPager.OnPageChangeListener)
+ * addOnPageChangeListener(OnPageChangeListener)} without removing the listener and
+ * not cause a leak.
+ */
+ private static class BottomNavigationViewExOnPageChangeListener implements ViewPager.OnPageChangeListener {
+ private final WeakReference mBnveRef;
+
+ public BottomNavigationViewExOnPageChangeListener(BottomNavigationViewEx bnve) {
+ mBnveRef = new WeakReference<>(bnve);
+ }
+
+ @Override
+ public void onPageScrollStateChanged(final int state) {
+ }
+
+ @Override
+ public void onPageScrolled(final int position, final float positionOffset,
+ final int positionOffsetPixels) {
+ }
+
+ @Override
+ public void onPageSelected(final int position) {
+ final BottomNavigationViewEx bnve = mBnveRef.get();
+ if (null != bnve)
+ bnve.setCurrentItem(position);
+// Log.d("onPageSelected", "--------- position " + position + " ------------");
+ }
+ }
+
+ /**
+ * Decorate OnNavigationItemSelectedListener for setupWithViewPager
+ */
+ private static class MyOnNavigationItemSelectedListener implements OnNavigationItemSelectedListener {
+ private OnNavigationItemSelectedListener listener;
+ private ViewPager viewPager;
+ private boolean smoothScroll;
+ private SparseArray items;// used for change ViewPager selected item
+ private int previousPosition = -1;
+
+
+ MyOnNavigationItemSelectedListener(ViewPager viewPager, BottomNavigationViewEx bnve, boolean smoothScroll, OnNavigationItemSelectedListener listener) {
+ this.viewPager = viewPager;
+ this.listener = listener;
+ this.smoothScroll = smoothScroll;
+
+ // create items
+ Menu menu = bnve.getMenu();
+ int size = menu.size();
+ items = new SparseArray<>(size);
+ for (int i = 0; i < size; i++) {
+ int itemId = menu.getItem(i).getItemId();
+ items.put(itemId, i);
+ }
+ }
+
+ public void setOnNavigationItemSelectedListener(OnNavigationItemSelectedListener listener) {
+ this.listener = listener;
+ }
+
+ @Override
+ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
+ int position = items.get(item.getItemId());
+ // only set item when item changed
+ if (previousPosition == position) {
+ return true;
+ }
+
+ // user listener
+ if (null != listener) {
+ boolean bool = listener.onNavigationItemSelected(item);
+ // if the selected is invalid, no need change the view pager
+ if (!bool)
+ return false;
+ }
+ // update previous position
+ previousPosition = position;
+
+ // change view pager
+ viewPager.setCurrentItem(items.get(item.getItemId()), smoothScroll);
+
+ return true;
+ }
+
+ }
+
}