1. 程式人生 > >第三方頻道管理

第三方頻道管理

在工程中匯入依賴

implementation 'com.github.andyoom:draggrid:v1.0.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.android.support:design:28.0.0'

在專案中的build.gradle中新增

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

activity_main.xml//佈局不唯一 可根據自己的想法建立

<?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">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:textSize="40dp"
        android:layout_marginRight="5dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/viewpager"
        />
    <android.support.design.widget.TabLayout
        android:id="@+id/tab"
        android:layout_width="0dp"
        android:layout_height="60dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/text"
        app:tabMode="scrollable"
        />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        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/tab"
        />

</android.support.constraint.ConstraintLayout>

MainAcitivity頁面

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<ChannelBean> channelBeans=new ArrayList<>();
    private List<Fragment> fragments=new ArrayList<>();
    private String jsonStr="";
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text);
        tabLayout = findViewById(R.id.tab);
        viewPager = findViewById(R.id.viewpager);



        initData();
    }

    private void initData() {
        channelBeans.add(new ChannelBean("熱點", true));
        channelBeans.add(new ChannelBean("軍事", true));
        channelBeans.add(new ChannelBean("八卦", true));
        channelBeans.add(new ChannelBean("遊戲", true));
        channelBeans.add(new ChannelBean("寵物", true));
        channelBeans.add(new ChannelBean("汽車", true));
        channelBeans.add(new ChannelBean("熱賣", true));
        channelBeans.add(new ChannelBean("外賣", true));
        channelBeans.add(new ChannelBean("條目1", true));
        channelBeans.add(new ChannelBean("條目2", true));
        channelBeans.add(new ChannelBean("條目3", false));
        channelBeans.add(new ChannelBean("條目4", false));
        channelBeans.add(new ChannelBean("條目5", false));
        channelBeans.add(new ChannelBean("條目6", false));
        channelBeans.add(new ChannelBean("條目7", false));
        channelBeans.add(new ChannelBean("條目8", false));
        fragments.add(new AFragment());
        fragments.add(new BFragment());
        fragments.add(new CFragment());
        fragments.add(new BFragment());
        fragments.add(new AFragment());
        fragments.add(new BFragment());
        fragments.add(new CFragment());
        fragments.add(new BFragment());
        fragments.add(new CFragment());
        fragments.add(new AFragment());
        fragments.add(new BFragment());
        fragments.add(new AFragment());
        fragments.add(new AFragment());
        fragments.add(new AFragment());
        fragments.add(new AFragment());
        fragments.add(new AFragment());

        for(int i=0;i<channelBeans.size();i++){
            if(channelBeans.get(i).isSelect()){
                tabLayout.addTab(tabLayout.newTab().setText(channelBeans.get(i).getName()));
            }
        }
        //viewpager設定介面卡
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                int count=0;//返回選中的條目數量
                for(int i=0;i<channelBeans.size();i++){
                    if(channelBeans.get(i).isSelect()){
                        count++;
                    }
                }
                return count;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return channelBeans.get(position).getName();
            }
        });
        tabLayout.setupWithViewPager(viewPager);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String jsonArray = gson.toJson(channelBeans);
                Intent intent = new Intent(MainActivity.this,ChannelActivity.class);
                intent.putExtra(RESULT_JSON_KEY, jsonArray);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
        
    }

    @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);
            tabLayout.removeAllTabs();//清空之前所有的欄目
            gson=new Gson();//進行gson解析
            Type type=new TypeToken<List<ChannelBean>>(){}.getType();
            channelBeans=gson.fromJson(jsonStr,type);
	        //迴圈存入
            for(int i=0;i<channelBeans.size();i++){
                if(channelBeans.get(i).isSelect()){
                    try {
                        tabLayout.addTab(tabLayout.newTab().setText(channelBeans.get(i).getName()));
                    }catch (Exception e){
                    }
                }
            }
            //viewpager介面卡重新整理
            viewPager.getAdapter().notifyDataSetChanged();
        }
    }
}