1、添加依赖
compile ‘com.github.andyoom:draggrid:v1.0.1’
2、在项目的build.gradle中添加(注,编译会爆错,尝试着添加下面的步骤)
allprojects {
repositories {
jcenter()
//下面这句才是加上的,上面的自带 的
maven {url "https://jitpack.io"}
}
}
3、XML布局
<android.support.design.widget.TabLayout
android:id="@+id/Tab_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<TextView
android:id="@+id/Add_Fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:text="+"
android:textSize="40sp" />
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/View_Pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
4、MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TabLayout Tab_Layout;
private TextView Image_Btn;
private ArrayList<ChannelBean> channelBeens;
String jsonStr = "";
private Gson gson;
private Button test_btn;
private ArrayList<Fragment> mList = new ArrayList<>();
private ViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
Tab_Layout = (TabLayout) findViewById(R.id.Tab_Layout);
Image_Btn = findViewById(R.id.Add_Fragment);
mPager = findViewById(R.id.View_Pager);
Image_Btn.setOnClickListener(this);
}
private void initData() {
//准备栏目数据
channelBeens = new ArrayList<ChannelBean>();
channelBeens.add(new ChannelBean("热点", true));
channelBeens.add(new ChannelBean("军事", true));
channelBeens.add(new ChannelBean("八卦", true));
channelBeens.add(new ChannelBean("游戏", true));
channelBeens.add(new ChannelBean("宠物", true));
channelBeens.add(new ChannelBean("汽车", true));
channelBeens.add(new ChannelBean("热卖", true));
channelBeens.add(new ChannelBean("外卖", true));
channelBeens.add(new ChannelBean("条目1", true));
channelBeens.add(new ChannelBean("条目2", true));
channelBeens.add(new ChannelBean("条目3", false));
channelBeens.add(new ChannelBean("条目4", false));
channelBeens.add(new ChannelBean("条目5", false));
channelBeens.add(new ChannelBean("条目6", false));
channelBeens.add(new ChannelBean("条目7", false));
channelBeens.add(new ChannelBean("条目8", false));
mList.add(new AFragment());
mList.add(new BFragment());
mList.add(new AFragment());
mList.add(new BFragment());
mList.add(new AFragment());
mList.add(new BFragment());
mList.add(new AFragment());
mList.add(new BFragment());
mList.add(new AFragment());
mList.add(new AFragment());
mList.add(new BFragment());
mList.add(new AFragment());
mList.add(new AFragment());
mList.add(new AFragment());
mList.add(new AFragment());
mList.add(new AFragment());
//把选择的栏目(true)数据配置给tablayout
for (int i = 0; i < channelBeens.size(); i++) {
if (channelBeens.get(i).isSelect()) {
Tab_Layout.addTab(Tab_Layout.newTab().setText(channelBeens.get(i).getName()));
}
}
mPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return mList.get(i);
}
@Override
public int getCount() {
int count = 0;
for (int i = 0; i < channelBeens.size(); i++) {
if (channelBeens.get(i).isSelect()) {
count++;
}
}
return count;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return channelBeens.get(position).getName();
}
});
Tab_Layout.setupWithViewPager(mPager);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Add_Fragment:
ChannelActivity.startChannelActivity(MainActivity.this, channelBeens);
break;
}
}
@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) {//为true表示是频道管理回调回来的
jsonStr = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);//得到栏目管理的结果
Toast.makeText(this, jsonStr, Toast.LENGTH_SHORT).show();
Log.i("main", jsonStr);
Tab_Layout.removeAllTabs();//清空之前的栏目
//把新选择的栏目结果更新到tablayout上
gson = new Gson();
//进行json解析
Type type = new TypeToken<ArrayList<ChannelBean>>() {
}.getType();
channelBeens = gson.fromJson(jsonStr, type);
//遍历结果,更新tablayout
for (int i = 0; i < channelBeens.size(); i++) {
if (channelBeens.get(i).isSelect()) {
try {
Tab_Layout.addTab(Tab_Layout.newTab().setText(channelBeens.get(i).getName()));
} catch (Exception e) {
}
}
}
mPager.getAdapter().notifyDataSetChanged();
}
}
}