ncapdevi / FragNav

An Android library for managing multiple stacks of fragments
1.5k stars 220 forks source link

BottomBar Version 2 #25

Closed FilipeOS closed 7 years ago

FilipeOS commented 8 years ago

Is this working properly on the version 2 of bottombar?

hauthorn commented 8 years ago

I haven't put anything in production yet, but I'm running this with version 2 just fine. Here's a short snippet from my own code.

    mBottomBar.setOnTabSelectListener(new OnTabSelectListener() {
        @Override
        public void onTabSelected(@IdRes int tabId) {
            switch (tabId) {
                case R.id.tab_start:
                    mNavController.switchTab(INDEX_HOME);
                    break;
                case R.id.tab_messages:
                    mNavController.switchTab(INDEX_MESSAGES);
                    break;
                case R.id.tab_shops:
                    mNavController.switchTab(INDEX_SHOPS);
                    break;
            }
        }
    });
FilipeOS commented 8 years ago

Hi there,

I've made that changes but it's not working :(

I can't display the fragments with the nested scroll, please help me.... Now the activity shows every tabs blank

hauthorn commented 8 years ago

Can you paste your full layout file(s)?

I'm new to this library, but I can try to help.

FilipeOS commented 8 years ago

Hi @hauthorn thank you.

MainActivity:

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //FragNav
        //list of fragments
        List<Fragment> fragments = new ArrayList<>(4);

        //add fragments to list
        fragments.add(ParkFragment.newInstance(0));
        fragments.add(LocationFragment.newInstance(0));
        fragments.add(NewsFragment.newInstance(0));
        fragments.add(InformationFragment.newInstance(0));

        //link fragments to container
        fragNavController = new FragNavController(getSupportFragmentManager(), R.id.container, fragments);

        mBottomBar = (BottomBar) findViewById(R.id.bottomBar);

        mBottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                //switch between tabs
                switch (tabId) {
                    case R.id.bottomBarPark:
                        fragNavController.switchTab(TAB_PARK);
                        break;
                    case R.id.bottomBarLocation:
                        fragNavController.switchTab(TAB_LOCATION);
                        break;
                    case R.id.bottomBarNews:
                        fragNavController.switchTab(TAB_NEWS);
                        break;
                    case R.id.bottomBarInformation:
                        fragNavController.switchTab(TAB_INFORMATION);
                        break;
                }
            }

        });

        mBottomBar.setOnTabReselectListener(new OnTabReselectListener() {
            @Override
            public void onTabReSelected(@IdRes int tabId) {
                fragNavController.clearStack();
            }
        });

    }

    @Override
    public void onBackPressed() {
        if (fragNavController.getCurrentStack().size() > 1) {
            fragNavController.pop();
        } else {
            super.onBackPressed();
        }
    }```

activity_main.xml

<android.support.design.widget.CoordinatorLayout 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="match_parent">

<android.support.v4.widget.NestedScrollView
    android:id="@+id/myScrollingContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottomBar" />

</android.support.v4.widget.NestedScrollView>

<com.roughike.bottombar.BottomBar
    android:id="@+id/bottomBar"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_gravity="bottom"
    app:bb_tabXmlResource="@xml/bottom_nav"
    app:bb_behavior="shy"/>

</android.support.design.widget.CoordinatorLayout>```

I've the correct gradle import and the "bottom_nav" (tabs /working)

Basically no content is displaying and when I press back (to close the app, it crash)

PS: Sorry I can't format this tags properly..............

hauthorn commented 8 years ago

Do you have a CoordinatorLayout as the outermost view in activity_main?

FilipeOS commented 8 years ago

@hauthorn For what I can see the fragments are not being instantiated because everything is blank and when I press the back button it crashes:

`java.lang.ArrayIndexOutOfBoundsException: length=4; index=-1

at java.util.ArrayList.get(ArrayList.java:310)

at com.ncapdevi.fragnav.FragNavController.getCurrentStack(FragNavController.java:261)

at com.generictec.hierparktbonn.MainActivity.onBackPressed(MainActivity.java:106)`

Can you re-check my code please? It was working before the v2 changes I did. Without this I can't test...

hauthorn commented 8 years ago

The stuff you have posted (now) seems fine. You are not posting everything relevant though. Are you using coordinator layout?

FilipeOS commented 8 years ago

@hauthorn Yes, this:

<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"
    tools:context="com.xxx.xxx.MainActivity">
<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<com.roughike.bottombar.BottomBar
    android:id="@+id/bottomBar"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_gravity="bottom"
    app:bb_activeTabAlpha="1"
    app:bb_activeTabColor="@color/blue"
    app:bb_behavior="shy"
    app:bb_inActiveTabAlpha="0.9"
    app:bb_inActiveTabColor="@color/white"
    app:bb_tabXmlResource="@xml/bottom_nav"
    app:bb_tabletMode="false"
    app:bb_titleTypeFace="Bauer.ttf" />

</android.support.design.widget.CoordinatorLayout>

hauthorn commented 8 years ago

How about the xml resource?

FilipeOS commented 8 years ago

@hauthorn The Fragment? The same like it was (working before v2):

Example the NewsFragment

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.xxx.xxx.NewsFragment">
<ImageView
    android:id="@+id/logo"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_marginTop="7dp"
    android:contentDescription="@string/stg_text"
    android:src="@drawable/logo" />

<View style="@style/DividerLogo" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

</LinearLayout>

FilipeOS commented 8 years ago

Can you show me your MainActivity.java (bottomBar v2) that handles the activity_main.xml and the fragments please?

hauthorn commented 8 years ago

I was thinking the xml resource file where you define the tabs. Maybe the layout requirements has changed.

Here is the onCreate for the MainActivity class.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Setup base 4 fragments for the tab navigation
    List<Fragment> fragments = new ArrayList<>(4);
    fragments.add(HomeFragment.newInstance());
    fragments.add(MessageFragment.newInstance(1));
    fragments.add(InfoFragment.newInstance());
    fragments.add(NavigationFragment.newInstance());

    mNavController = new FragNavController(savedInstanceState, getSupportFragmentManager(), R.id.main_fragment_container, fragments, FragmentTransaction.TRANSIT_FRAGMENT_FADE);

    // Setup tab selection
    mBottomBar = (BottomBar) findViewById(R.id.bottom_bar);
    mBottomBar.setOnTabSelectListener(new OnTabSelectListener() {
        @Override
        public void onTabSelected(@IdRes int tabId) {
            switch (tabId) {
                case R.id.tab_home:
                    setTitle(R.string.home_tab_title);
                    mNavController.switchTab(INDEX_HOME);
                    break;
                case R.id.tab_messages:
                    setTitle(R.string.inbox_tab_title);
                    mNavController.switchTab(INDEX_MESSAGES);
                    break;
                case R.id.tab_info:
                    setTitle(R.string.info_tab_title);
                    mNavController.switchTab(INDEX_INFO);
                    break;
                case R.id.tab_navigation:
                    setTitle(R.string.find_route_tab_title);
                    mNavController.switchTab(INDEX_NAVIGATION);
                    break;
                default:
                    throw new RuntimeException("Tab with selected id doesn't exist.");
            }
        }
    });
}
FilipeOS commented 8 years ago

@hauthorn I can't put this working...

Your fragNavController = new FragNavController(savedInstanceState, getSupportFragmentManager(), R.id.container, fragments, FragmentTransaction.TRANSIT_FRAGMENT_FADE); is throwing error, constructor expects this fragNavController = new FragNavController(getSupportFragmentManager(), R.id.container, fragments);

If I change to the expected code the app crash java.lang.RuntimeException: Tab with selected id doesn't exist.

I understand that you are trying to help and I thank you but alone I can't put this working...

hauthorn commented 8 years ago

I am using the source code version of fragnav. Not the one on gradle. Remove the last parameter from the constructor, and you should be good to go.

You should change your ID's to whatever you tabs are called. That exception is one I throw manually if the ID is not in the switch statement.

FilipeOS commented 8 years ago

On your default: I've placed the 1st fragment, it shows the 1st fragment but when I press the tabs nothing happens... Is there any way for I share my code with you?

I've even made the BaseFragment but nothing changes.

rupak-cribello commented 8 years ago

Hi, Showing this error.. screenshot from 2016-09-20 16-15-34 How can I solve it? Ref. https://github.com/ncapdevi/FragNav/issues/25#issuecomment-241411722 , https://github.com/ncapdevi/FragNav/issues/25#issuecomment-241399403

FilipeOS commented 8 years ago

Like you can see, you should remove the savedInstanceState

rupak-cribello commented 8 years ago

Ok, and for this error.. mark wiht '**'

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mNavController.onSaveInstanceState(outState); //**
    }

I did remove 'mNavController.onSaveInstanceState(outState); ' for two times.. and its working. Can you tell me, How to set tab bar at bottom position?

alouanemed commented 7 years ago

@hauthorn Can you make your gist files that contain fragment xml files public ?

ncapdevi commented 7 years ago

The library is now up to date and I'm reviving this project. It definitely does work fine with Bottombar v2 or with Google's new support library version of a bottom navigation. I'll update the sample app for the latest version of bottombar or Google's support library shortly, but it shouldn't be an issue for your own code.