build.gradle project
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
//添加一个远程仓库;
maven {url "https://jitpack.io"}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
builder
implementation 'com.github.andyoom:draggrid:v1.0.1'
MainActivity
package com.bw.zsd.pin;
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.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.andy.library.ChannelActivity;
import com.andy.library.ChannelBean;
import com.bw.zsd.pin.R;
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 {
TabLayout tb;
ViewPager vp;
Button btn;
ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
ArrayList<ChannelBean> channelBeanList = new ArrayList<ChannelBean>();
private MPagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initVeiw();
initData();
}
private void initVeiw() {
tb = (TabLayout) findViewById(R.id.tb);
vp = (ViewPager) findViewById(R.id.vp);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ChannelActivity.startChannelActivity(MainActivity.this, channelBeanList);
}
});
}
private void initData() {
channelBeanList.add(new ChannelBean("热点", true));
channelBeanList.add(new ChannelBean("北京", true));
channelBeanList.add(new ChannelBean("房价", true));
channelBeanList.add(new ChannelBean("旅游", false));
channelBeanList.add(new ChannelBean("条目1", false));
channelBeanList.add(new ChannelBean("条目2", false));
for (int i = 0; i < channelBeanList.size(); i++) {
ChannelBean channelBean = channelBeanList.get(i);
if (channelBean.isSelect()) {
//tb:是findViewById找到的tab;
tb.addTab(tb.newTab().setText(channelBean.getName()));
if (i == 0) {
fragmentList.add(Fragment1.getInstances(channelBean.getName()));
}else if (i == 1){
fragmentList.add(Fragment2.getInstances(channelBean.getName()));
}else {
fragmentList.add(BlankFragment.getInstances(channelBean.getName()));
}
}
}
mPagerAdapter = new MPagerAdapter(getSupportFragmentManager());
vp.setAdapter(mPagerAdapter);
tb.setupWithViewPager(vp);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//得到 频道管理页返回的数据,是sjon形式
String stringExtra = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<ChannelBean>>() {
}.getType(); // 把json直接转换成集合;
channelBeanList = gson.fromJson(stringExtra, type); // 吧json转换成集合
tb.removeAllTabs(); //清空 tab
fragmentList.clear(); //清空 所有Fragment
// 重新生成新的tab, 重新生成新的Fragment;
for (int i = 0; i < channelBeanList.size(); i++) {
ChannelBean channelBean = channelBeanList.get(i);
if (channelBean.isSelect()) {
//tb:是findViewById找到的tab;
tb.addTab(tb.newTab().setText(channelBean.getName()));
if ("热点".equals(channelBean.getName())) {
fragmentList.add(Fragment1.getInstances(channelBean.getName()));
}else if ("北京".equals(channelBean.getName())){
fragmentList.add(Fragment2.getInstances(channelBean.getName()));
}else {
fragmentList.add(BlankFragment.getInstances(channelBean.getName()));
}
}
}
mPagerAdapter.notifyDataSetChanged();
}
private class MPagerAdapter extends FragmentPagerAdapter {
public MPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return channelBeanList.get(position).getName();
}
}
}
activity_main.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"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<android.support.design.widget.TabLayout
android:id="@+id/tb"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>