1、首先在项目的build下注册
allprojects {
repositories {
jcenter()
//下面这句才是加上的,上面的自带 的
maven {url "https://jitpack.io"}
}
}
2、然后在app下的build中导入依赖
compile 'com.github.andyoom:draggrid:v1.0.1'
xml文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.bawei.pin.MainActivity"> <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMode="scrollable" android:id="@+id/tab"/> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:src="@drawable/ems_pulltorefresh_down_arrow" android:id="@+id/btn"/> </RelativeLayout>
java 代码
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
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;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TabLayout tablayout;
private ImageButton imgBtn;
private List<ChannelBean> channelBeans;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
//准备栏目数据
channelBeans = new ArrayList<ChannelBean>();
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));
//把选择的栏目(true)数据配置给tablayout
for (int i = 0; i < channelBeans.size(); i++) {
if (channelBeans.get(i).isSelect()) {
tablayout.addTab(tablayout.newTab().setText(channelBeans.get(i).getName()));
}
}
}
private void initView() {
tablayout = (TabLayout) findViewById(R.id.tablayout);
imgBtn = (ImageButton) findViewById(R.id.imgBtn);
imgBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.imgBtn:
ChannelActivity.startChannelActivity(MainActivity.this,channelBeans);
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==ChannelActivity.REQUEST_CODE && resultCode==ChannelActivity.RESULT_CODE){
//为true表示是频道管理回调回来的
String s = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);//得到栏目管理的结果
tablayout.removeAllTabs();//清空之前的栏目
//把新选择的栏目结果更新到tablayout上
Gson gson = new Gson();
//进行json解析
Type type = new TypeToken<ArrayList<ChannelBean>>() {}.getType();
channelBeans=gson.fromJson(s,type);
//遍历结果,更新tablayout
for (int i = 0; i < channelBeans.size(); i++) {
if (channelBeans.get(i).isSelect()){
tablayout.addTab(tablayout.newTab().setText(channelBeans.get(i).getName()));
}
}
}
}
}