本文需要添加的依赖有
//底部导航
compile 'com.hjm:BottomTabBar:1.1.1'
//tablayout
compile 'com.android.support:design:26.0.0-alpha1'
activity_main.xml
<?xmlversion="1.0" encoding="utf-8"?>
<!--侧栏-->
<android.support.v4.widget.DrawerLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bwei.fragment.message.MainActivity">
<!--底部导航-->
<com.hjm.bottomtabbar.BottomTabBar
xmlns:hjm="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bottom"
hjm:tab_font_size="12sp"
android:layout_alignParentBottom="true"
hjm:tab_img_font_padding="0dp"
hjm:tab_img_height="40dp"
hjm:tab_img_width="40dp"
hjm:tab_padding_bottom="5dp"
hjm:tab_padding_top="8dp"
hjm:tab_selected_color="#ffbd66"
hjm:tab_unselected_color="#1a0204"
></com.hjm.bottomtabbar.BottomTabBar>
<!--侧栏的frameLayout-->
<FrameLayout
android:id="@+id/drawer"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f79"
></FrameLayout>
</android.support.v4.widget.DrawerLayout>
fragmentone.xml
<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--头部选项卡-->
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/tab"
app:tabMode="scrollable"
></android.support.design.widget.TabLayout>
<!--选项卡的联动-->
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vp"
></android.support.v4.view.ViewPager>
</LinearLayout>
fragmenttwo.xml
<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="match_parent"
android:layout_height="match_parent">
<com.bwei.fragment.message.xlistview.XListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/xlistview"
></com.bwei.fragment.message.xlistview.XListView>
</LinearLayout>
MainActivity.java
packagecom.bwei.fragment.message;
importandroid.os.Bundle;
importandroid.support.v7.app.AppCompatActivity;
importcom.bwei.fragment.message.fragment.FragmentFour;
import com.bwei.fragment.message.fragment.FragmentOne;
importcom.bwei.fragment.message.fragment.FragmentThree;
importcom.bwei.fragment.message.fragment.FragmentTwo;
importcom.hjm.bottomtabbar.BottomTabBar;
public classMainActivity extends AppCompatActivity {
private BottomTabBar bottomTabBar;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomTabBar = (BottomTabBar)findViewById(R.id.bottom);
bottomTabBar.init(getSupportFragmentManager())
.addTabItem("首页",R.drawable.ic_nav_home_press,R.drawable.ic_nav_home, FragmentOne.class)
.addTabItem("自选", R.drawable.ic_normal_class_press,R.drawable.ic_normal_class,FragmentTwo.class)
.addTabItem("行情",R.drawable.ic_nav_user_press,R.drawable.ic_nav_user, FragmentThree.class)
.addTabItem("资讯", R.drawable.ic_nav_cart_press, R.drawable.ic_nav_cart,FragmentFour.class)
.addTabItem("交易", R.drawable.ic_nav_class_press, R.drawable.ic_nav_class,FragmentFour.class)
;
getSupportFragmentManager().beginTransaction().add(R.id.drawer,newFragmentLeft()).commit();
}
}
FragmentOne.java
package com.bwei.fragment.message.fragment;
importandroid.os.Bundle;
importandroid.support.annotation.Nullable;
importandroid.support.design.widget.TabLayout;
importandroid.support.v4.app.Fragment;
importandroid.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
importandroid.support.v4.view.ViewPager;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importcom.bwei.fragment.message.R;
importjava.util.ArrayList;
importjava.util.List;
/**
* Created by User on 2017/10/14.
*/
public classFragmentOne extends Fragment{
private List<Fragment> first=newArrayList<>();
String[] strName=new String[]{"推荐","热点","本地","视频","社会","娱乐","科技","汽车","体育","财经","军事","国际","段子","趣图","健康","美女"};
private TabLayout tabLayout;
private ViewPager viewPager;
private MyAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflaterinflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=View.inflate(getContext(),R.layout.fragmentone,null);
tabLayout =view.findViewById(R.id.tab);
viewPager = view.findViewById(R.id.vp);
for (int i=0;i<16;i++){
tabLayout.addTab(tabLayout.newTab().setText(strName[i]));
}
for (int i=0;i<16;i++){
first.add(new FragmentTwo());
}
adapter = newMyAdapter(getFragmentManager());
viewPager.setAdapter(adapter);
//联合滑动
tabLayout.setupWithViewPager(viewPager);
return view;
}
class MyAdapter extendsFragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return first.get(position);
}
@Override
public int getCount() {
return first.size();
}
@Override
public CharSequence getPageTitle(intposition) {
return strName[position];
}
}
}
FragmentTwo.java
packagecom.bwei.fragment.message.fragment;
importandroid.os.Bundle;
importandroid.support.annotation.Nullable;
importandroid.support.v4.app.Fragment;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importcom.bwei.fragment.message.R;
importcom.bwei.fragment.message.xlistview.XListView;
/**
* Created by User on 2017/10/14.
*/
public classFragmentTwo extends Fragment{
private XListView xListView;
@Nullable
@Override
public View onCreateView(LayoutInflaterinflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Viewview=View.inflate(getContext(),R.layout.fragmenttwo,null);
xListView = view.findViewById(R.id.xlistview);
xListView.setPullRefreshEnable(true);
xListView.setPullLoadEnable(true);
return view;
}
}