我最近使用的AHBottomNavigation在底栏中有5个项目,例如 我点击Home Button它转到homeActivty,我有3个标签栏。直到这里的一切都还可以,但我知道当我们使用AHBottomNavigation时我们应该使用片段但是 我需要使用条形图的活动并在所有条形图中显示AHBottomNavigation。 我在我的所有活动中使用AHBottomNavigation来处理这个问题并且它可以工作,我的所有活动都有AHBottomNavigation但是当我改变活动时, AHBottomNavigation没有正常工作,似乎有一个延迟。 有没有更好的解决方案,或者如果我做错了什么请通知我?
HomeActivity.class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
if (drawer != null) {
drawer.setDrawerListener(toggle);
}
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//[START Tab ]
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
//[*End Tab]
//[Start Bottom N]
bottomNavigation();
}
private void setupViewPager(ViewPager viewPager){
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Fragment1(),"Tab1");
adapter.addFragment(new Fragment2() , "Tab2");
adapter.addFragment(new Fragment3() , "Tab3");
viewPager.setAdapter(adapter);
}
public void bottomNavigation(){
// [START BottomNavigation]
// TODO add other activitys to setOnTabSelectedListener (HOME , SEARCH , ...)
AHBottomNavigation bottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation);
// Create items
AHBottomNavigationAdapter navigationAdapter = new AHBottomNavigationAdapter(this, R.menu.bottom_navigation);
navigationAdapter.setupWithBottomNavigation(bottomNavigation);
bottomNavigation.setBehaviorTranslationEnabled(true);
bottomNavigation.setAccentColor(Color.parseColor("#3498db"));
bottomNavigation.setCurrentItem(0);
bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
@Override
public boolean onTabSelected(int position, boolean wasSelected) {
switch (position){
case 0 :
break;
case 1 :
// Add
Intent goToAddStuff = new Intent(HomeActivity.this , AddStuffActivity.class);
startActivity(goToAddStuff);
finish();
break;
case 2 :
// Search
break;
case 3:
// Activity's
//My AONActivity have 2 tab too
Intent goToAON = new Intent(HomeActivity.this , AONActivity.class);
startActivity(goToAON);
finish();
break;
case 4:
//Account
break;
}
return true;
}
});
// [*END BottomNavigation]
}
content_home.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".HomeActivity"
android:id="@+id/content_id"
tools:showIn="@layout/activity_home">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".HomeActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
<FrameLayout
android:id="@+id/contentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomBar" />
<include layout="@layout/bottom_navigation" />
</android.support.design.widget.CoordinatorLayout>
bottom_navigation.xml
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="@+id/bottom_navigation"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_gravity="bottom">
</com.aurelhubert.ahbottomnavigation.AHBottomNavigation>
答案 0 :(得分:1)
我也使用AHBottomNavigation,但是由于Google Material Design,您需要使用Fragments,否则您的实现方式不正确。
这是怎么回事:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="@+id/framelayout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
这是一些代码,只是为了表明如果你不知道如何更好地处理碎片,只是我不知道你的背景,也许你不使用它,因为你感到困惑。
@Override
public void onTabSelected(int position, boolean wasSelected) {
Log.d(TAG, "Position " + position + " was selected" + wasSelected);
if(!wasSelected) {
getSupportFragmentManager()
.beginTransaction()
.replace(FRAME_LAYOUT,
createFragment(position),
createItemDescription(position))
.addToBackStack(null)
.commit();
}
}
private void createBottomNavigationMenu(int defaultCurrentItem){
for(int i = 0; i<5; i++){
// Create bottom navigation item
AHBottomNavigationItem item =
new AHBottomNavigationItem(createItemDescription(i),
createPicture(i));
// Add bottom navigation item
mBottomNavigation.addItem(item);
}
// Set current item programmatically
mBottomNavigation.setCurrentItem(defaultCurrentItem, true);
// Set background color
mBottomNavigation.setDefaultBackgroundColor(Color.parseColor("#56ABF8"));
// Disable the translation inside the CoordinatorLayout
mBottomNavigation.setBehaviorTranslationEnabled(false);
// Set listener
mBottomNavigation.setOnTabSelectedListener(this);
}
// Factory-method patterns
private Fragment createFragment(int position){
switch (position) {
case 1:
return new SettingsFragment_();
case 2:
return new StatisticsFragment_();
case 3:
return new MainFragment_();
case 4:
return new ForthFragment_();
case 5:
return new ReminderFragment_();
}
throw new IllegalArgumentException();
}
private String createItemDescription(int position){
switch (position){
case 1:
return BOT_NAV_ITEM_SETTINGS;
case 2:
return BOT_NAV_ITEM_STATISTICS;
case 3:
return BOT_NAV_ITEM_MAIN;
case 4:
return BOT_NAV_ITEM_FORTH;
case 5:
return BOT_NAV_ITEM_REMINDERS;
}
throw new IllegalArgumentException();
}
private int createPicture(int position){
switch (position){
case 1:
return R.mipmap.ic_settings;
case 2:
return R.mipmap.ic_statistics;
case 3:
return R.mipmap.ic_water;
case 4:
return R.mipmap.ic_launcher;
case 5:
return R.mipmap.ic_reminders;
}
throw new IllegalArgumentException();
}
答案 1 :(得分:0)
我找到了解决方案:
我创建HomeFragment,其范围如下:
HomeFragment.class
public class HomeFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
private static int currentItem = 0;
AdapterFragment adapter;
public static HomeFragment newInstance(int someInt) {
HomeFragment frag = new HomeFragment();
Bundle args = new Bundle();
args.putInt("tab", someInt);
frag.setArguments(args);
return frag;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("currentItem", viewPager.getCurrentItem());
}
public static HomeFragment newInstance() {
HomeFragment frag = new HomeFragment();
return frag;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter= new AdapterFragment(getChildFragmentManager());
currentItem=getArguments() != null ? getArguments().getInt("tab") : 0;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View rootView = inflater.inflate(R.layout.fragment_home,container,false);
tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
/**
*Set an Adapter for the View Pager
*/
if (adapter==null) adapter=new AdapterFragment(getChildFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setCurrentItem( currentItem);
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return rootView;
}
public class AdapterFragment extends FragmentPagerAdapter {
public AdapterFragment(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new Tab1Fragment();
case 1 : return new Tab2Fragment();
case 2 : return new Tab3Fragment();
}
return null;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Tab1";
case 1 :
return "Tab2";
case 2 :
return "Tab3";
}
return null;
}
}
在我的
中MainActivity
bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
@Override
public boolean onTabSelected(int position, boolean wasSelected) {
switch (position){
case 0 :
// Home
HomeFragment homeFragment = new HomeFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_id, homeFragment)
.commit();
break;
case 1:
break;
}
return true;
}
});
我希望这会对你有所帮助