1. 程式人生 > >Android : 頻道管理

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'

需要的許可權

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
<!--此處寫好上面那一行許可權直接alt+enter就可以出來-->
    tools:ignore="ProtectedPermissions" />
<!-- 往SDCard寫入資料許可權 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 震動許可權 -->
<uses-permission android:name="android.permission.VIBRATE"/>

主頁面上的xml的程式碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabLayout
            android:id="@+id/tl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable" />

        <TextView
            android:id="@+id/Add_Fragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:text="+"
            android:textSize="40dp" />

    </RelativeLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/View_Page"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v4.view.ViewPager>


    <ImageButton
        android:id="@+id/imgbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="92dp"
        android:layout_marginRight="250dp"
        android:src="@mipmap/ic_launcher" />


</LinearLayout>

Activity裡的主程式碼

package com.bwie.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.bwie.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<ChannelBean> channelBeens;
    private ViewPager View_Page;
    private ArrayList<Fragment> 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<ChannelBean>();
        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<ArrayList<ChannelBean>>() {
            }.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的聯動,就不上程式碼了,另外有些小坑都已經填了

在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
效果圖就這麼多吧,第三方挺全 的,就是會出現小坑,需注意

以上;