//布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.bwei.test_19.ShowActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="40dp"
app:tabMode="scrollable"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_marginTop="40dp"
android:layout_height="match_parent"
android:background="#ffffff"
/>
</LinearLayout>
<ListView
android:layout_width="240sp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#f00"
android:dividerHeight="1dp"
android:background="#fff"
android:id="@+id/lv">
</ListView>
</android.support.v4.widget.DrawerLayout>
//main方法中的代码
//初始化组件
private TabLayout tabLayout;
private ViewPager viewPager;
private MyFragment myFragment;
private ListView listView;
private DrawerLayout db;
private String[] menu = {"收藏","设置","钱包","反馈"};//设置侧拉菜单的条目
//创建标题数组
private String[] channels = new String[]{"美女","首页","推荐","财经","娱乐","福利","安卓","贴吧","房产","新闻"};
//创建viewPager集合
private List<MyFragment> list = new ArrayList<>();
//创建路径集合
private String[]urls = new String[]{
"https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10",
"https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10",
"https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10",
"https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10",
"https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10",
"https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10",
"https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10",
"https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10",
"https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10",
"https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
//初始化组件
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
listView = (ListView) findViewById(R.id.lv);
db = (DrawerLayout) findViewById(R.id.activity_show);
init();
ArrayAdapter<String> lvLeftAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
listView.setAdapter(lvLeftAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
db.closeDrawer(listView);
}
});
}
public void init(){
//根据标题的多少设置fragment
for(int i = 0;i<channels.length;i++){
MyFragment myFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("title",channels[i]);
bundle.putString("url",urls[i]);
myFragment.setArguments(bundle);
//将fragment添加到fragment集合中
list.add(myFragment);
//给tab设置标题卡
tabLayout.addTab(tabLayout.newTab().setText(channels[i]));
}
FragmentManager fm = getSupportFragmentManager();
MyAdapter mAdapter = new MyAdapter(fm,list);
viewPager.setAdapter(mAdapter);//给ViewPager设置适配器
tabLayout.setupWithViewPager(viewPager);//将TabLayout和ViewPager关联起来。
tabLayout.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器
}
class MyAdapter extends FragmentPagerAdapter{
private final List<MyFragment> list;
public MyAdapter(FragmentManager fm,List<MyFragment> list) {
super(fm);
this.list = list;
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
@Override
public CharSequence getPageTitle(int position) {
return channels[position];
}
}