频道管理

//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();
    }
}

//基本思路
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值