//xml代码中
//导入design依赖
//导入channelmanagement项目
//关联channelmanagement项目
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="实时票房"
android:textSize="25sp"
/>
<TextView
android:id="@+id/tiao"
android:text="三"
android:textSize="40sp"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
//--------------------------------imageLoader继承ChannelApplication---------------------
//---------------------------------------tabLayout对应的页面----------------------------------------------
public class Frag_02 extends BaseFragment {
private ViewPager viewpager;
private ArrayList<Fragment> fragments;
private TabLayout tab;
private TextView tiao;
private ArrayList<ChannelItem> channelItems;
private ChannoAdapter adapter;
@Override
protected int RequestId() {
return R.layout.frag_02;
}
@Override
protected void initView(View view) {
//初始化控件
tab = view.findViewById(R.id.table);
viewpager = view.findViewById(R.id.viewpager);
tiao = view.findViewById(R.id.tiao);
}
@Override
protected void initDta() {
//获取数据库中添加的数据
channelItems = DBUtil.getTabData(getActivity());
//添加tablayout页面
fragments = new ArrayList<>();
for (int i = 0;i<channelItems.size();i++){
String tabname = channelItems.get(i).getName();
if ("头条".equals(tabname)){
fragments.add(new Tab_frag_01());
}else {
fragments.add(new Tab_frag_02());
}
}
//绑定适配器
adapter = new ChannoAdapter(getActivity().getSupportFragmentManager(),channelItems,fragments);
viewpager.setAdapter(adapter);
//绑定控件
tab.setupWithViewPager(viewpager);
//跳转的点击事件---跳转到选择频道页面
tiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),ChannelActivity.class);
startActivityForResult(intent,0);
}
});
}
//回传值方法里接受数据
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 0){
//获取数据库添加的频道
channelItems = DBUtil.getTabData(getActivity());
for (int i = 0;i<channelItems.size();i++){
String tabname = channelItems.get(i).getName(); //获取频道的名字
if ("头条".equals(tabname)){
fragments.add(new Tab_frag_01()); //根据频道名称去创建对应的fragment
} else {
//如果没有对应的就创建一个默认的fragment
fragments.add(new Tab_frag_02());
}
}
//绑定适配器
adapter = new ChannoAdapter(getActivity().getSupportFragmentManager(),channelItems,fragments);
viewpager.setAdapter(adapter);
//绑定控件
tab.setupWithViewPager(viewpager);
}
}
}
//-----------------------------适配器----------------------------------
public class ChannoAdapter extends FragmentPagerAdapter {
// private FragmentManager fragmentManager;
// List<ChannelBean> list;
private ArrayList<ChannelItem> channelItems;
private ArrayList<Fragment> fragments;
public ChannoAdapter(FragmentManager fm, ArrayList<ChannelItem> channelItems, ArrayList<Fragment> fragments) {
super(fm);
this.channelItems = channelItems;
this.fragments = fragments;
}
@Override
public Fragment getItem(int i) {
return fragments.get(i);
}
@Override
public int getCount() {
return channelItems.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return channelItems.get(position).getName();
}
}
//基本思路