//------------------------------------布局文件----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
//-----------------------------------------大类标题---------------------------------------------
public class Frag_02 extends Fragment {
private FragmentTransaction transaction;
private ArrayList<String> title;
private ViewPager pager;
private TabLayout table;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_02,container,false);
//控件
table = view.findViewById(R.id.table);
pager = view.findViewById(R.id.pager);
//页面的集合
final ArrayList<Fragment> fragments = new ArrayList<>();
//标题的集合
title = new ArrayList<>();
final Tab_frag_01 tab_frag_01 = new Tab_frag_01();
final Tab_frag_02 tab_frag_02 = new Tab_frag_02();
final Tab_frag_03 tab_frag_03 = new Tab_frag_03();
fragments.add(tab_frag_01);
fragments.add(tab_frag_02);
fragments.add(tab_frag_03);
title.add("山东");
title.add("小韩系");
title.add("河南");
pager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public Fragment getItem(int i) {
if(i==0){
return tab_frag_01;
}else if(i==1){
return tab_frag_02;
}else{
return tab_frag_03;
}
}
@Override
public int getCount() {
return fragments.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return title.get(position);
}
});
pager.setOffscreenPageLimit(fragments.size());
table.setupWithViewPager(pager);
return view;
}
}
//-------------------------------------小类内容--------------------------------------------------
public class Tab_frag_01 extends Fragment {
private String path = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
private List<Beans.DataBean> list = new ArrayList<>();
private PullToRefreshListView plistview;
private int num = 1;
private Handler handler = new Handler();
private MyAdapter myAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_frag_01,container,false);
//控件
plistview = view.findViewById(R.id.plistview1);
//支持上拉加载 下拉刷新
plistview.setMode(PullToRefreshListView.Mode.BOTH);
//上拉加载 下拉刷新的方法
initjias();
//解析的方法
RequstData(1);
return view;
}
private void initjias() {
plistview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
num = 1;
list.clear();
RequstData(1);
}
},3000);
myAdapter.notifyDataSetChanged();
plistview.onRefreshComplete(); //停止刷新
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
num++;
RequstData(num);
}
},3000);
myAdapter.notifyDataSetChanged();
plistview.onRefreshComplete(); //停止刷新
}
});
}
//调用
private void RequstData(int num) {
NetUtils.getInstsance().RequestData(path+num, Beans.class, new NetUtils.CallBacks<Beans>() {
@Override
public void onSuccess(Beans beans) {
list.addAll(beans.getData());
myAdapter = new MyAdapter(getActivity(), list);
plistview.setAdapter(myAdapter);
}
});
}
}
TabLayout配合Viewpager+Fragment结合使用
最新推荐文章于 2020-05-04 10:56:01 发布