**
在工程中导入依赖
**
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.android.support:design:27.1.1'
implementation 'com.github.andyoom:draggrid:v1.0.1'
**
在build.gradle里添加
**
allprojects {
repositories {
google()
jcenter()
maven {url "https://jitpack.io"}
}
}
**
activity_main页面 布局可自定义
**
<?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_constraintBottom_toTopOf="@id/viewpager"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<android.support.design.widget.TabLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/tab"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@+id/text"
app:tabMode="scrollable"
></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/viewpager"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tab"
></android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
**
MainActivity页面
**
package com.example.lx_pdgl;
import android.content.Intent;
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.os.Bundle;
import android.view.View;
import android.widget.TextView;
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;
import static com.andy.library.ChannelActivity.REQUEST_CODE;
import static com.andy.library.ChannelActivity.RESULT_JSON_KEY;
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);
gson = new Gson();
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",true));
channelBeans.add(new ChannelBean("条目4",true));
channelBeans.add(new ChannelBean("条目5",true));
channelBeans.add(new ChannelBean("条目6",true));
channelBeans.add(new ChannelBean("条目7",true));
channelBeans.add(new ChannelBean("条目8",true));
fragments.add(new AFragment());
fragments.add(new BFragment());
fragments.add(new CFragment());
fragments.add(new AFragment());
fragments.add(new BFragment());
fragments.add(new CFragment());
fragments.add(new AFragment());
fragments.add(new BFragment());
fragments.add(new CFragment());
fragments.add(new AFragment());
fragments.add(new BFragment());
fragments.add(new CFragment());
fragments.add(new AFragment());
fragments.add(new BFragment());
fragments.add(new CFragment());
fragments.add(new AFragment());
fragments.add(new BFragment());
fragments.add(new CFragment());
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 position) {
return fragments.get(position);
}
@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, 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解析
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();
}
}
}
}