简单的频道管理

//1导channelmanagement的Module
//2导入清单文件的权限
   <!-- 在SDCard中创建与删除文件权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <!-- 往SDCard写入数据权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <!-- 震动权限 -->
    <uses-permission android:name="android.permission.VIBRATE"/>



//3继承ChannelApplication 
public class MyApp extends ChannelApplication {

    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(this).build();
        ImageLoader.getInstance().init(config);

    }
}


	

//4写标题的Frag  
public class Frag1 extends BaseFragment {


    private ViewPager pager;
    private TabLayout tab;
    private TextView text;
    private ArrayList<String> tabs;

    @Override
    protected int layoutResID() {
        return R.layout.frag1;
    }

    @Override
    protected void initView(View view) {
        tab = view.findViewById(R.id.tab);
        pager = view.findViewById(R.id.pager);
        text = view.findViewById(R.id.text);

    }

    @Override
    protected void initData() {
        //获取数据库中添加的数据的集合
        ArrayList<ChannelItem> channelItems = DBUtil.getTabData(getActivity());
        //创建标签集合
        tabs = new ArrayList<>();
        for (int i = 0; i < channelItems.size(); i++) {
            String tabname = channelItems.get(i).getName();
            tabs.add(tabname);
        }

        //适配器
        pager.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                ChildFragment fragment = new ChildFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("position",i);
                fragment.setArguments(bundle);



                return fragment;
            }

            @Override
            public int getCount() {
                return tabs.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabs.get(position);
            }
        });

        //关联适配器
        tab.setupWithViewPager(pager);



        //点击跳转
        text.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);
        //获取数据库添加的频道的集合
        ArrayList<ChannelItem> channelItems  = DBUtil.getTabData(getActivity());
        //清空集合
       tabs.clear();
       //添加集合
        for (int i = 0; i < channelItems.size(); i++) {
            String tabname = channelItems.get(i).getName();
            tabs.add(tabname);
        }

        //适配器
        pager.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                ChildFragment fragment = new ChildFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("position",i);
                fragment.setArguments(bundle);
                return fragment;
            }

            @Override
            public int getCount() {
                return tabs.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabs.get(position);
            }
        });

        //关联适配器
        tab.setupWithViewPager(pager);


    }
}

    


相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页