Skip to content

Commit

Permalink
Use NoScrollViewPager instead of FragmentChangManager, add function t…
Browse files Browse the repository at this point in the history
…o update fragment with position
  • Loading branch information
chenshixin committed Aug 17, 2016
1 parent e54fa35 commit a07282f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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<Fragment>

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<Fragment>) {
fragmentChangeManager = FragmentChangManager(fragmentManager, R.id.bottom_navigation_bar_content, fragments)
fun setFragments(fragmentManager: FragmentManager, fragmentList: ArrayList<Fragment>) {
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<BottomNavigationItem>) {
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import android.view.View
/**
* Created by chenshixin on 7/5/16.
*/
class BottomNavigationBehavior: CoordinatorLayout.Behavior<BottomNavigationBar>() {
class BottomNavigationBehavior : CoordinatorLayout.Behavior<BottomNavigationBar>() {

companion object {
val SCROLL_DIRECTION_UP = 1
Expand All @@ -18,7 +18,7 @@ class BottomNavigationBehavior: CoordinatorLayout.Behavior<BottomNavigationBar>(
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 {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
}
}
6 changes: 3 additions & 3 deletions bottom-navigation/src/main/res/layout/bottom_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:id="@+id/bottom_navigation_bar_content"
<com.chenshixin.bottomnavigation.NoScrollViewPager
android:id="@+id/bottom_navigation_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">

</FrameLayout>
</com.chenshixin.bottomnavigation.NoScrollViewPager>

<com.chenshixin.bottomnavigation.BottomNavigationBar
android:id="@+id/bottom_navigation_bar"
Expand Down

0 comments on commit a07282f

Please sign in to comment.