Skip to content

Commit

Permalink
Make fragmentPagerAdapter as param so this controller do not need to …
Browse files Browse the repository at this point in the history
…manager it
  • Loading branch information
chenshixin committed Aug 17, 2016
1 parent a07282f commit 5e06f53
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 50 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,24 @@ dependencies {

BottomNavigation bottomNavigation = (BottomNavigation) findViewById(R.id.bottom_navigation_bar_with_content);
bottomNavigation.setTabItems(tabItems);
bottomNavigation.setFragments(getSupportFragmentManager(), fragments);
bottomNavigation.setFragmentPagerAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return SimpleTextFragment.newInstance("Explore");
case 1:
return SimpleTextFragment.newInstance("News");
default:
return SimpleTextFragment.newInstance("Mine");
}
}

@Override
public int getCount() {
return 3;
}
});
bottomNavigation.setTitleColorActive(Color.BLUE);
bottomNavigation.setTitleColorInactive(Color.RED);
bottomNavigation.setOnTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ package com.chenshixin.bottomnavigation

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
Expand Down Expand Up @@ -41,39 +38,16 @@ class BottomNavigation(context: Context?, attrs: AttributeSet?) : CoordinatorLay
get() = bottom_navigation_bar.selectedPosition
set(value) {
bottom_navigation_bar.setCurrentTab(value)
bottom_navigation_view_pager.currentItem = value
bottom_navigation_view_pager.setCurrentItem(value, false)
}

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, 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 setFragmentPagerAdapter(fragmentPagerAdapter: FragmentPagerAdapter) {
bottom_navigation_view_pager.adapter = fragmentPagerAdapter
}

fun setTabItems(tabs: List<BottomNavigationItem>) {
Expand All @@ -83,6 +57,9 @@ class BottomNavigation(context: Context?, attrs: AttributeSet?) : CoordinatorLay
}

fun initialise() {
if (bottom_navigation_view_pager.adapter == null) {
throw IllegalStateException("adapter not initialised")
}
bottom_navigation_bar.initialise()
bottom_navigation_bar.onTabSelectedListener = object : BottomNavigationBar.OnTabSelectedListener {
override fun onTabWillBeSelected(position: Int): Boolean {
Expand All @@ -99,10 +76,6 @@ class BottomNavigation(context: Context?, attrs: AttributeSet?) : CoordinatorLay
}

override fun onTabReselected(position: Int) {
val fragment = fragments[position]
if (fragment is BottomNavigationBar.DoubleTapToScrollTop) {
fragment.scrollToTop()
}
onTabSelectedListener?.onTabReselected(position)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ class BottomNavigationBar(context: Context, attrs: AttributeSet) : FrameLayout(c
fun onTabReselected(position: Int)
}

interface DoubleTapToScrollTop {
fun scrollToTop()
}

internal fun show() {
isHidden = false
animateOffset(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.chenshixin.bottomnavigation.BottomNavigationBar;
import com.chenshixin.bottomnavigation.BottomNavigationItem;
import com.chenshixin.bottomnavigation.BottomNavigation;

Expand All @@ -28,14 +27,27 @@ protected void onCreate(Bundle savedInstanceState) {
tabItems.add(new BottomNavigationItem("News", 2, R.drawable.ic_tab_news_b, R.drawable.ic_tab_news_a));
tabItems.add(new BottomNavigationItem("Mine", 3, R.drawable.ic_tab_mine_b, R.drawable.ic_tab_mine_a));

final List<Fragment> fragments = new ArrayList<>();
fragments.add(SimpleTextFragment.newInstance("Explore"));
fragments.add(SimpleTextFragment.newInstance("News"));
fragments.add(SimpleTextFragment.newInstance("Mine"));

BottomNavigation bottomNavigation = (BottomNavigation) findViewById(R.id.bottom_navigation_bar_with_content);
bottomNavigation.setTabItems(tabItems);
bottomNavigation.setFragments(getSupportFragmentManager(), fragments);
bottomNavigation.setFragmentPagerAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return SimpleTextFragment.newInstance("Explore");
case 1:
return SimpleTextFragment.newInstance("News");
default:
return SimpleTextFragment.newInstance("Mine");
}
}

@Override
public int getCount() {
return 3;
}
});
bottomNavigation.setTitleColorActive(Color.BLACK);
bottomNavigation.setTitleColorInactive(Color.GRAY);
bottomNavigation.initialise();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
import android.view.ViewGroup;
import android.widget.TextView;

import com.chenshixin.bottomnavigation.BottomNavigationBar;

import java.util.ArrayList;
import java.util.List;

/**
* Created by chenshixin on 7/5/16.
*/
public class SimpleTextFragment extends Fragment implements BottomNavigationBar.DoubleTapToScrollTop {
public class SimpleTextFragment extends Fragment{

public static final String KEY_TEXT = "key_text";

Expand Down Expand Up @@ -73,7 +71,7 @@ private class ViewHolder extends RecyclerView.ViewHolder {

TextView textView;

public ViewHolder(View itemView) {
ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.simpleText);
}
Expand All @@ -88,7 +86,6 @@ private List<String> getData() {
return data;
}

@Override
public void scrollToTop() {
if (mRecyclerView != null) {
LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
Expand Down

0 comments on commit 5e06f53

Please sign in to comment.