1. 程式人生 > >頻道管理+TabLayout+ViewPager聯動

頻道管理+TabLayout+ViewPager聯動

效果圖
在這裡插入圖片描述
在這裡插入圖片描述
1.佈局頁面

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">
    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/imagebtn"
        app:layout_constraintTop_toTopOf="parent"
        app:tabMode="scrollable"
        />
    <ImageButton
        android:id="@+id/imagebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/tab_layout"
        app:layout_constraintRight_toRightOf="parent"
        android:src="@drawable/ic_action_name"
        app:layout_constraintBottom_toBottomOf="@id/tab_layout"
        />
    <android.support.v4.view.ViewPager
        android:id="@+id/v_pager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imagebtn"
        />
</android.support.constraint.ConstraintLayout>

2.MainActivity頁面

package com.example.pindaoguanli_demo;

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

import com.andy.library.ChannelActivity;
import com.andy.library.ChannelBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

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

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ImageButton imageButton;
    private ViewPager viewPager;
    private List<ChannelBean> list;
    private List<ChannelBean> channelBeans;
    private Type type;
    private Gson gson;
    private String jsonStr;
    private TabAdaper tabAdaper;
    private List<ChannelBean> date = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //獲取資源id
        tabLayout = findViewById(R.id.tab_layout);
        imageButton = findViewById(R.id.imagebtn);
        viewPager = findViewById(R.id.v_pager);
        //建立介面卡
        tabAdaper = new TabAdaper(getSupportFragmentManager());
        viewPager.setAdapter(tabAdaper);
        tabLayout.setupWithViewPager(viewPager);
        initData();
    }

    private void initData() {
        //例項化
        list = new ArrayList<>();
        list.add(new ChannelBean("關注",true));
        list.add(new ChannelBean("推薦",true));
        list.add(new ChannelBean("科技",true));
        list.add(new ChannelBean("美食",true));
        list.add(new ChannelBean("養生",true));
        list.add(new ChannelBean("電影",true));
        list.add(new ChannelBean("生活",true));
        list.add(new ChannelBean("搞笑",true));
        list.add(new ChannelBean("懂車帝",true));
        list.add(new ChannelBean("軍事",true));
        list.add(new ChannelBean("黨媒推薦",true));
        list.add(new ChannelBean("歷史",true));
        list.add(new ChannelBean("體育",true));

        list.add(new ChannelBean("寵物",false));
        list.add(new ChannelBean("娛樂",false));
        list.add(new ChannelBean("財經",false));
        list.add(new ChannelBean("直播",false));
        list.add(new ChannelBean("特賣",false));
        list.add(new ChannelBean("房產",false));
        list.add(new ChannelBean("精品課",false));
        list.add(new ChannelBean("小說",false));
        list.add(new ChannelBean("時尚",false));
        list.add(new ChannelBean("音訊",false));
        list.add(new ChannelBean("育兒",false));
        list.add(new ChannelBean("數碼",false));
        list.add(new ChannelBean("健康",false));
        //把選中的欄目為true的資料設定給TabLayout
       for (int i = 0; i <list.size() ; i++) {
            if(list.get(i).isSelect()){
                //tabLayout.addTab(tabLayout.newTab().setText(list.get(i).getName()));
                date.add(list.get(i));
            }
        }
        tabAdaper.setList(date);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChannelActivity.startChannelActivity(MainActivity.this,list);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == ChannelActivity.REQUEST_CODE && resultCode == ChannelActivity.RESULT_CODE){
            //得到欄目管理的結果
            jsonStr = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);
            Log.i("TEXT",jsonStr);
             //清空之前的欄目
            tabLayout.removeAllTabs();
            //把新選擇的欄目更新的tablayout上
            gson = new Gson();
            //進行解析
            type = new TypeToken<List<ChannelBean>>() {}.getType();
            channelBeans = gson.fromJson(jsonStr, type);
            date.removeAll(date);
            //遍歷結果更新tablayout
            for (int i = 0;i<channelBeans.size();i++){
                if(channelBeans.get(i).isSelect()){
                    //tabLayout.addTab(tabLayout.newTab().setText(channelBeans.get(i).getName()));
                    date.add(channelBeans.get(i));
                }
            }
            ChannelDao.getIntance(this).delAll();
            ChannelDao.getIntance(this).addAll(date);
            tabAdaper.setList(ChannelDao.getIntance(this).select());
        }
    }
}

3.介面卡頁面

package com.example.pindaoguanli_demo;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.andy.library.ChannelBean;

import java.util.ArrayList;
import java.util.List;

public class TabAdaper extends FragmentPagerAdapter {
    private List<ChannelBean> list;

    public TabAdaper(FragmentManager fm) {
        super(fm);
        list = new ArrayList<>();
    }

    public void setList(List<ChannelBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int i) {
        switch (i){
            default: return new ShowFragment();
        }
        //return null;
    }

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

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

4.fragment頁面

package com.example.pindaoguanli_demo;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ShowFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        textView.setText("頁面");
        return textView;
    }
}

5.匯入的依賴

5.1在整個工程的build.gradle需要匯入的依賴
maven {url "https://jitpack.io"}

5.2在相應的module中的build.gradle需要匯入的依賴
implementation 'com.github.andyoom:draggrid:v1.0.1'

6.在清單檔案中加的許可權

<uses-permission android:name="android.permission.VIBRATE"/>
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>