android viewpager 底部tabhost,Android--FragmentTabHost+ViewPager+Fragment实现底部tab菜单栏

activity_main.xml

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"

android:fitsSystemWindows="true"

tools:context="com.app.gaolonglong.fragmenttabhost.MainActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:theme="@style/AppTheme.AppBarOverlay">

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="?attr/colorPrimary"

app:popupTheme="@style/AppTheme.PopupOverlay" />

content_main.xml

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="com.app.gaolonglong.fragmenttabhost.MainActivity"

tools:showIn="@layout/activity_main">

android:id="@android:id/tabhost"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@android:id/tabcontent"

android:layout_weight="0"

android:layout_width="0dp"

android:layout_height="0dp"/>

android:id="@+id/view_pager"

android:layout_weight="1"

android:layout_width="match_parent"

android:layout_height="0dp"/>

android:id="@android:id/tabs"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"/>

tab_item.xml

android:orientation="vertical"

android:paddingTop="4dp"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/image"

android:src="@drawable/tab_report"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:id="@+id/title"

android:text="首页"

android:gravity="center"

android:textSize="14sp"

android:textColor="@drawable/tab_title_color"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

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

}

}

2503775298c9e9ec6d4b14486c49439a.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值