//private一个Tablayout剩下看代码吧。
package com.bwie.tablayoutdemo;
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.widget.Toast;
import com.bwie.fragments.FragmentForVp;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager vp;
String[] tabs = {"推荐","热点","体育","娱乐","社会","汽车","教育","财经","科技","游戏"};
private String[] urlS = {
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/2",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/3",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/4",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/5",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/6",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/7",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/8",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/9",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/10"
};
private ArrayList<FragmentForVp> fragments;
MyVpAdapter myVpAdapter;
FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到组件
tabLayout = (TabLayout) findViewById(R.id.tablayout);
vp = (ViewPager) findViewById(R.id.vp);
fm = getSupportFragmentManager();
//创建集合,存储vp的page页(fragment)
fragments = new ArrayList<FragmentForVp>();
//给tablayout设置tab页
for(int i=0;i<tabs.length;i++){
FragmentForVp fragmentForVp = new FragmentForVp();
Bundle b = new Bundle();
b.putString("data",tabs[i]);
b.putString("dataUrl",urlS[i]);
fragmentForVp.setArguments(b);
fragments.add(fragmentForVp);
tabLayout.addTab(tabLayout.newTab().setText(tabs[i]));
}
//给vp配置page页
myVpAdapter = new MyVpAdapter(fm);
vp.setAdapter(myVpAdapter);
//tablayout和vp的关联
tabLayout.setupWithViewPager(vp);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Toast.makeText(MainActivity.this,tab.getText(),Toast.LENGTH_SHORT).show();
if(tab.getText().equals("推荐")){
//请求推荐的网络数据
}else if(tab.getText().equals("热点")){
//请求热点的网络数据
}else{
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
class MyVpAdapter extends FragmentPagerAdapter{
public MyVpAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
}
}
//FragmentForVp的代码
package com.bwie.fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.bwie.tablayoutdemo.R;
/**
* Created by Administrator on 2017/11/14.
*/
public class FragmentForVp extends Fragment {
private TextView tv;
private String dataUrl;
private ListView newsLv;
public FragmentForVp() {
dataUrl = getArguments().getString("dataUrl");//得到数据url
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout,container,false);
tv = (TextView) view.findViewById(R.id.tv);
newsLv = (ListView) view.findViewById(R.id.newsLv);
Bundle arguments = getArguments();//获得通过setArgument传的值
tv.setText(arguments.getString("data"));
//通过dataUrl请求网络数据,解析展示在newsLv上,扩展:实现下拉上拉效果
return view;
}
}
//以下是XML文件
//MainXML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bwie.tablayoutdemo.MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabSelectedTextColor="@android:color/holo_red_light"
app:tabIndicatorColor="@android:color/holo_red_light"
app:tabTextAppearance="@android:style/TextAppearance.Large"
/>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tablayout"/>
</RelativeLayout>
//Fragment XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv"
android:textSize="48dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ListView
android:id="@+id/newsLv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>