清单文件中配置
android:layout_height="50dp"
android:id="@+id/tab_layout"
app:tabTextColor="@color/colorAccent"
app:tabIndicatorColor="@color/colorPrimary"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabMode="scrollable"
app:tabBackground="@color/tablayout_bg"
/>
android:layout_height="match_parent"
android:id="@+id/viewpager"
/>
//代码中的设置
private void pager() {
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return TabFragment.newInstance(api.get(position));
}
//返回fragment的个数
@Override
public int getCount() {
return api.size();
}
//设置title标题
@Override
public CharSequence getPageTitle(int position) {
return list1.get(position);
}
});
//tabLayout和viewPager进行关联
tabLayout.setupWithViewPager(viewPager);
}
//fragment当中所写的类
@ContentView(R.layout.tab_fragment)
public class TabFragment extends Fragment {
@ViewInject(R.id.listView)
private ListView lv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return x.view().inject(this, inflater,container);
}
public TabFragment(){
}
public static TabFragment newInstance(String text){
Bundle bundle = new Bundle();
bundle.putString("text",text);
TabFragment tabFragment = new TabFragment();
tabFragment.setArguments(bundle);
return tabFragment;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
RequestParams requestParams = new RequestParams(getArguments().getString("text"));
x.http().get(requestParams, new Callback.CommonCallback
private List
@Override
public void onSuccess(String result) {
Gson gson = new Gson();
News news = gson.fromJson(result, News.class);
list = news.getData();
MyAdapter adapter = new MyAdapter(list, getActivity());
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
}