Android : 频道管理

需要再项目的build.gradle里面写上maven {url “https://jitpack.io”},否则依赖类会报错. 效果图在最后.

allprojects {
    repositories {
        google()
        jcenter()
        maven {url "https://jitpack.io"}
    }
}

第三方框架需要的依赖

implementation 'com.github.andyoom:draggrid:v1.0.1'

其他jar包

    implementation 'com.android.support:design:28.0.0'
    implementation 'com.google.code.gson:gson:2.8.5'

需要的权限


    tools:ignore="ProtectedPermissions" />




主页面上的xml的代码




    

        

        

    

    


    



Activity里的主代码

package com.example.administrator.channel;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.andy.library.ChannelActivity;
import com.andy.library.ChannelBean;
import com.example.administrator.channel.frament.aaaFragment;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TabLayout tl;
    private TextView imgbtn;
    private String jsonStr;
    private ArrayList channelBeens;
    private ViewPager View_Page;
    private ArrayList datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        initData();

    }

    private void initView() {
        tl = findViewById(R.id.tl);
        imgbtn = (TextView) findViewById(R.id.Add_Fragment);
        View_Page = (ViewPager) findViewById(R.id.View_Page);
        imgbtn.setOnClickListener(this);
    }

    private void initData() {
        //准备栏目数据
        channelBeens = new ArrayList();
        channelBeens.add(new ChannelBean("热点", true));
        channelBeens.add(new ChannelBean("军事", true));
        channelBeens.add(new ChannelBean("八卦", false));
        channelBeens.add(new ChannelBean("666", false));
        datas.add(new aaaFragment());
        datas.add(new bbbFragment());
        datas.add(new cccFragment());
        datas.add(new aaaFragment());

        //把选择的栏目(true)数据配置给tablayout
        for (int i = 0; i < channelBeens.size(); i++) {
            //进行判断
            if (channelBeens.get(i).isSelect()) {
                tl.addTab(tl.newTab().setText(channelBeens.get(i).getName()));
            }
        }
        View_Page.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
            //此处加入if判断,进行和fragment绑定,多少个条目就绑定多少次
            if (channelBeens.get(i).getName().equals("热点")){
                    return datas.get(0);
                }else if (channelBeens.get(i).getName().equals("军事")){
                    return datas.get(1);
                }else if (channelBeens.get(i).getName().equals("八卦")){
                    return datas.get(2);
                }else {
                    return datas.get(3);
                }
         }

            @Override
            public int getCount() {
                int count = 0;
                //循环
                for (int i = 0; i < channelBeens.size(); i++) {
                    //进行判断
                    if (channelBeens.get(i).isSelect()) {
                        count++;
                    }
                }

                return count;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return channelBeens.get(position).getName();
            }
        });
        tl.setupWithViewPager(View_Page);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Add_Fragment:
                ChannelActivity.startChannelActivity(MainActivity.this, channelBeens);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //为true表示是频道管理回调回来的
        if (requestCode == ChannelActivity.REQUEST_CODE && resultCode == ChannelActivity.RESULT_CODE) {
            //得到栏目管理的结果
            jsonStr = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);
            //吐司查看
            Toast.makeText(this, jsonStr, Toast.LENGTH_SHORT).show();
            //打印查看
            Log.i("main", jsonStr);
            //清空之前的项目
            tl.removeAllTabs();
            //把新选择的栏目结果更新到tablayout上
            Gson gson = new Gson();
            //进行json解析
            Type type = new TypeToken>() {
            }.getType();
            channelBeens = gson.fromJson(jsonStr, type);
            //遍历结果,更新TabLayout
            for (int i = 0; i < channelBeens.size(); i++) {
            //进行判断
                if (channelBeens.get(i).isSelect()) {
                    try {
                    	//此处是第三方框架的一个小坑,会报IllegalStateException非法语句异常所以直接try了,然后就可以解决异常
                        tl.addTab(tl.newTab().setText(channelBeens.get(i).getName()));
                    }catch (Exception e){
                    }
            }
            View_Page.getAdapter().notifyDataSetChanged();
        }
    }
}

Fragment是空的 只是为了验证与fragment的联动,就不上代码了,另外有些小坑都已经填了

Android : 频道管理_第1张图片
Android : 频道管理_第2张图片
Android : 频道管理_第3张图片
Android : 频道管理_第4张图片
效果图就这么多吧,第三方挺全 的,就是会出现小坑,需注意

以上;

你可能感兴趣的:(Android : 频道管理)