如何创建动态底部导航栏或底栏

时间:2017-02-26 16:05:56

标签: java android

我在此活动中创建了一个活动,有一个特定的标签我只想以编程方式创建这些底栏以插入类似这样的数据

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

这是我的TabsActivity我只想在此活动中添加此底部导航

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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用最近添加的支持库中提供的官方Google BottomNavigationView类。只需在您的app build.gradle文件中添加以下依赖项

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

您可以找到文档here