From a07282f5f05c529ea406d23de73f700d80319984 Mon Sep 17 00:00:00 2001 From: chenshixin Date: Wed, 17 Aug 2016 11:32:07 +0800 Subject: [PATCH] Use NoScrollViewPager instead of FragmentChangManager, add function to update fragment with position --- .../bottomnavigation/BottomNavigation.kt | 36 +++++++++++++++---- .../BottomNavigationBehavior.kt | 4 +-- .../bottomnavigation/FragmentChangManager.kt | 34 ------------------ .../bottomnavigation/NoScrollViewPager.kt | 21 +++++++++++ .../src/main/res/layout/bottom_navigation.xml | 6 ++-- 5 files changed, 56 insertions(+), 45 deletions(-) delete mode 100644 bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/FragmentChangManager.kt create mode 100644 bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/NoScrollViewPager.kt diff --git a/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/BottomNavigation.kt b/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/BottomNavigation.kt index 915d1af..eb07090 100644 --- a/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/BottomNavigation.kt +++ b/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/BottomNavigation.kt @@ -4,18 +4,19 @@ import android.content.Context import android.support.design.widget.CoordinatorLayout import android.support.v4.app.Fragment import android.support.v4.app.FragmentManager +import android.support.v4.app.FragmentPagerAdapter import android.util.AttributeSet import android.view.LayoutInflater import android.view.ViewGroup import kotlinx.android.synthetic.main.bottom_navigation.view.* +import java.util.* /** + * BottomNavigation * Created by chenshixin on 7/5/16. */ class BottomNavigation(context: Context?, attrs: AttributeSet?) : CoordinatorLayout(context, attrs) { - var fragmentChangeManager: FragmentChangManager? = null - var onTabSelectedListener: BottomNavigationBar.OnTabSelectedListener? = null /** @@ -40,16 +41,39 @@ class BottomNavigation(context: Context?, attrs: AttributeSet?) : CoordinatorLay get() = bottom_navigation_bar.selectedPosition set(value) { bottom_navigation_bar.setCurrentTab(value) - fragmentChangeManager?.currentTab = value + bottom_navigation_view_pager.currentItem = value } + private lateinit var fragments: MutableList + init { LayoutInflater.from(context).inflate(R.layout.bottom_navigation, this, true) layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) } - fun setFragments(fragmentManager: FragmentManager, fragments: List) { - fragmentChangeManager = FragmentChangManager(fragmentManager, R.id.bottom_navigation_bar_content, fragments) + fun setFragments(fragmentManager: FragmentManager, fragmentList: ArrayList) { + this.fragments = fragmentList + bottom_navigation_view_pager.adapter = object : FragmentPagerAdapter(fragmentManager) { + override fun getItem(position: Int): Fragment { + return fragments[position] + } + + override fun getCount(): Int { + return fragments.size + } + + } + } + + /** + * Set fragment at position + */ + fun updateFragment(position: Int, fragment: Fragment) { + if (position < 0 || position >= fragments.size) { + throw IllegalArgumentException("position not exists") + } + fragments[position] = fragment + bottom_navigation_view_pager.adapter.notifyDataSetChanged() } fun setTabItems(tabs: List) { @@ -75,7 +99,7 @@ class BottomNavigation(context: Context?, attrs: AttributeSet?) : CoordinatorLay } override fun onTabReselected(position: Int) { - val fragment = fragmentChangeManager!!.fragments[position] + val fragment = fragments[position] if (fragment is BottomNavigationBar.DoubleTapToScrollTop) { fragment.scrollToTop() } diff --git a/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/BottomNavigationBehavior.kt b/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/BottomNavigationBehavior.kt index 290edc0..425f1ac 100644 --- a/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/BottomNavigationBehavior.kt +++ b/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/BottomNavigationBehavior.kt @@ -7,7 +7,7 @@ import android.view.View /** * Created by chenshixin on 7/5/16. */ -class BottomNavigationBehavior: CoordinatorLayout.Behavior() { +class BottomNavigationBehavior : CoordinatorLayout.Behavior() { companion object { val SCROLL_DIRECTION_UP = 1 @@ -18,7 +18,7 @@ class BottomNavigationBehavior: CoordinatorLayout.Behavior( private var totalDyConsumed = -1 override fun layoutDependsOn(parent: CoordinatorLayout?, child: BottomNavigationBar, dependency: View?): Boolean { - return dependency?.id == R.id.bottom_navigation_bar_content + return dependency?.id == R.id.bottom_navigation_view_pager } override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout?, child: BottomNavigationBar, directTargetChild: View?, target: View?, nestedScrollAxes: Int): Boolean { diff --git a/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/FragmentChangManager.kt b/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/FragmentChangManager.kt deleted file mode 100644 index f23ad13..0000000 --- a/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/FragmentChangManager.kt +++ /dev/null @@ -1,34 +0,0 @@ -package com.chenshixin.bottomnavigation - -import android.support.v4.app.Fragment -import android.support.v4.app.FragmentManager - -/** - * Created by chenshixin on 7/5/16. - */ -class FragmentChangManager(val fm: FragmentManager, val containerViewId: Int, val fragments: List) { - - private var mCurrentTab: Int = 0 - - var currentTab: Int? = 0 - set(value) { - fragments.mapIndexed { i, fragment -> - val transaction = fm.beginTransaction() - if (i == value) { - transaction.show(fragment) - } else { - transaction.hide(fragment) - } - transaction.commit() - } - this.mCurrentTab = value ?: 0 - } - - init { - fragments.map { fragment -> - fm.beginTransaction().add(containerViewId, fragment).hide(fragment).commit() - } - currentTab = 0 - } - -} \ No newline at end of file diff --git a/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/NoScrollViewPager.kt b/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/NoScrollViewPager.kt new file mode 100644 index 0000000..41597b5 --- /dev/null +++ b/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/NoScrollViewPager.kt @@ -0,0 +1,21 @@ +package com.chenshixin.bottomnavigation + +import android.content.Context +import android.support.v4.view.ViewPager +import android.util.AttributeSet +import android.view.MotionEvent + +/** + * ViewPager without scroll between tabs + * Created by chenshixin on 7/18/16. + */ +class NoScrollViewPager(context: Context, attributeSet: AttributeSet) : ViewPager(context, attributeSet) { + + override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean { + return false + } + + override fun onTouchEvent(ev: MotionEvent?): Boolean { + return false + } +} \ No newline at end of file diff --git a/bottom-navigation/src/main/res/layout/bottom_navigation.xml b/bottom-navigation/src/main/res/layout/bottom_navigation.xml index 1a050e7..39e1276 100644 --- a/bottom-navigation/src/main/res/layout/bottom_navigation.xml +++ b/bottom-navigation/src/main/res/layout/bottom_navigation.xml @@ -3,12 +3,12 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - - +