博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android--FragmentTabHost+ViewPager+Fragment实现底部tab菜单栏
阅读量:5931 次
发布时间:2019-06-19

本文共 4680 字,大约阅读时间需要 15 分钟。

版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/79001632

activity_main.xml

content_main.xml

tab_item.xml

HomeFragment
public class HomeFragment extends Fragment {    private View mRootView;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        if (mRootView == null){            mRootView = inflater.inflate(R.layout.home_fragment,container,false);        }        ViewGroup parent = (ViewGroup) mRootView.getParent();        if (parent != null){            parent.removeView(mRootView);        }        return mRootView;    }}
 
MainActivity
public class MainActivity extends AppCompatActivity {    private FragmentTabHost mTabHost;    private ViewPager mViewPager;    private List
mFragmentList; private Class mClass[] = {HomeFragment.class,ReportFragment.class,MessageFragment.class,MineFragment.class}; private Fragment mFragment[] = {new HomeFragment(),new ReportFragment(),new MessageFragment(),new MineFragment()}; private String mTitles[] = {"首页","报表","消息","我的"}; private int mImages[] = { R.drawable.tab_home, R.drawable.tab_report, R.drawable.tab_message, R.drawable.tab_mine }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); init(); } private void init() { initView(); initEvent(); } private void initView() { mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mViewPager = (ViewPager) findViewById(R.id.view_pager); mFragmentList = new ArrayList
(); mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); mTabHost.getTabWidget().setDividerDrawable(null); for (int i = 0;i < mFragment.length;i++){ TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTitles[i]).setIndicator(getTabView(i)); mTabHost.addTab(tabSpec,mClass[i],null); mFragmentList.add(mFragment[i]); mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE); } mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } }); } private View getTabView(int index) { View view = LayoutInflater.from(this).inflate(R.layout.tab_item, null); ImageView image = (ImageView) view.findViewById(R.id.image); TextView title = (TextView) view.findViewById(R.id.title); image.setImageResource(mImages[index]); title.setText(mTitles[index]); return view; } private void initEvent() { mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String tabId) { mViewPager.setCurrentItem(mTabHost.getCurrentTab()); } }); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { mTabHost.setCurrentTab(position); } @Override public void onPageScrollStateChanged(int state) { } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}

你可能感兴趣的文章
样式调整
查看>>
linux 并发 RCU
查看>>
Clang
查看>>
sql union和union all的用法及效率
查看>>
编程之美初赛-焦距
查看>>
Django Http请求生命周期
查看>>
(原創) 如何建立一个thread? (OS) (Linux) (C/C++) (C)
查看>>
Python语音合成
查看>>
转]Windows与VMware下的Linux文件共享方式总结
查看>>
ASP“.NET研究”.NET中的认证与授权
查看>>
JAVA的枚举类
查看>>
10件在PHP7中不要做的事情
查看>>
Windows下Hook API技术
查看>>
Int32.Parse, Convert.ToInt32,Int32.TryParse三者的区别
查看>>
开发工具MyEclipse如何支持可视化设计HTML和JSP页面
查看>>
<转>Bitmap Buttons
查看>>
jQuery EasyUI
查看>>
<气场>读书笔记
查看>>
代码设计的几个基础技巧
查看>>
[转]C#调用SSIS包(DTS)
查看>>