在工程中导入依赖
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();
}
}
}