简体   繁体   中英

How to create dynamic Bottom Navigation bar or Bottom bar

I created an activity in this activity there's a specific tabs I just want to create these Bottom bar programmatically as to insert data something like this

gridArray.add(new Item(homeIcon, rs.getString("Name"), OpenIn, rs.getString("Covers"), rs.getString("Count_Checks")));

and this is my TabsActivity I just want to add this bottom navigation in this activity

package abtech.waiteriano.com.waitrer;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

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

import abtech.waiteriano.com.waitrer.fragments.TablesFragment;

public class TabsActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

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


        viewPager = (ViewPager) findViewById(R.id.viewpager);

        /**
         * Initializing the viewpager with fragments
         */
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());


        adapter.addFragment(new TablesFragment(), "Progress");
        adapter.addFragment(MyFragment.newInstance("Example1"), "Example1");
        adapter.addFragment(MyFragment.newInstance("Example2"), "Example2");
        adapter.addFragment(MyFragment.newInstance("Example3"), "Example3");

        viewPager.setAdapter(adapter);

        tabLayout = (TabLayout) findViewById(R.id.tabs);

        //adding viewpager to the tablayout
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }
    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

You can use official google BottomNavigationView class available in support library which has been added recently.Just add the following dependency in your app build.gradle file

compile ‘com.android.support:design:25.0.0’

You can find the documentation here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM