我们在使用TabLayout时首先添加依赖,依赖的版本号要与自己的版本号一致
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
xx
先看效果(动态效果不会,就弄个图片看吧)
1:这是从网络中获取数据
因为是从网络获取数据,所以要加网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
在使用xutils访问网络时,在用之前要写xutils的帮助类
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG);
}
}
还要在AndroidManifest的application中注册自己的帮助类
android:name=".BaseApplication"
2:如果不是从网络获取数据,上面的这一点就不用写
添加的是从网络获取的数据还是固定数据,我在下面代码里已经重点标出
在布局文件中 tablayout与viewpager连用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tab_FindFragment_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
/>
<android.support.v4.view.ViewPager
android:id="@+id/vp_FindFragment_pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
MainActivity中 分两种情况:固定数据和网络数据,自己选择
添加固定参数
public class MainActivity extends AppCompatActivity {
private TabLayout tab;
private ViewPager vp;
//装fragment的集合
private List<Fragment> fragments=new ArrayList<>();
//添加固定数据的集合
private List<String> lists=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
//添加固定数据
addDate();
}
private void addDate() {
lists.add("关注");
lists.add("推荐");
lists.add("视频");
lists.add("段优秀");
lists.add("直播");
lists.add("图片");
lists.add("段子");
lists.add("精华");
lists.add("通常");
lists.add("游戏");
for (int i = 0; i < lists.size(); i++) {
BaseFragment fragment = new BaseFragment();
fragments.add(fragment);
}
//给viewpager设置适配器
PagerFragment adapter = new PagerFragment(getSupportFragmentManager(), lists, fragments);
vp.setAdapter(adapter);
//最后一步:tablayout与viewpager关联
tab.setupWithViewPager(vp);
}
private void initView() {
tab = (TabLayout) findViewById(R.id.tab);
vp = (ViewPager) findViewById(R.id.vp);
}
}
添加网络数据
public class MainActivity extends AppCompatActivity {
private TabLayout tab;
private ViewPager vp;
//装fragment的集合
private List<Fragment> fragments=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
//添加数据网络
addXutils();
}
//添加数据网络
private void addXutils() {
{ //数据url
String url = "http://lf.snssdk.com/neihan/service/tabs/?essence=1&iid=3216590132&device_id=32613520945&ac=wifi&channel=" +
"360&aid=7&app_name=joke_essay&version_code=612&version_name=6.1.2&device_platform=android&ssmix=a&device_type=" +
"sansung&device_brand=xiaomi&os_api=28&os_version=6.10.1&uuid=326135942187625&openudid=3dg6s95rhg2a3dg5&manifest_version_code" +
"=612&resolution=1450*2800&dpi=620&update_version_code=6120";
RequestParams params = new RequestParams();
params.setUri(url);
//xutils数据请求
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
//解析 获取数据
Gson gson = new Gson();
News news = gson.fromJson(result, News.class);
final List<News.DataBean> list = news.getData();
//循环添加fragment fragment复用
for (int i = 0; i < list.size(); i++) {
BaseFragment fragment = new BaseFragment();
fragments.add(fragment);
}
//给viewpager设置适配器
PagerFragment adapter = new PagerFragment(getSupportFragmentManager(), list, fragments);
vp.setAdapter(adapter);
//最后一步:tablayout与viewpager关联
tab.setupWithViewPager(vp);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
}
private void initView() {
tab = (TabLayout) findViewById(R.id.tab);
vp = (ViewPager) findViewById(R.id.vp);
}
}
复用的fragment
public class BaseFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.basefragment,container,false);
return view;
}
}
viewpager适配器
public class PagerFragment extends FragmentPagerAdapter {
//装网络数据的集合
private List<News.DataBean> list;
//装固定数据的集合
private List<String> lists;
//装fragment的集合
private List<Fragment> fragments;
public PagerFragment(FragmentManager fm,List<String> lists, List<Fragment> fragments) {
super(fm);
this.lists=lists;
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
//网络数据
return fragments.size();
//固定数据
return lists.size();
}
@Override
public CharSequence getPageTitle(int position) {
//网络数据
return list.get(position).getName();
//固定数据
return lists.get(position%lists.size());
}
}
数据解析实体类自己写
这就是所有代码