依赖包:
清单文件 权限:
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 震动权限 -->
<uses-permission android:name="android.permission.VIBRATE"/>
导入依赖到自己工程下的gradle :
implementation 'com.github.andyoom:draggrid:v1.0.1'
在项目的build.gradle中添加:
allprojects {
repositories {
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">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@id/jia"
></android.support.design.widget.TabLayout>
<ImageButton
android:id="@+id/jia"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintRight_toRightOf="parent"
android:src="@drawable/jia"
/>
</android.support.constraint.ConstraintLayout>
MainActivity:
public class MainActivity extends AppCompatActivity {
private TabLayout tab;
private ImageButton jia;
private List<ChannelBean> list;
private String jsonStr="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tab = findViewById(R.id.tab);
jia = findViewById(R.id.jia);
initData();
}
private void initData() {
list=new ArrayList<>();
list.add(new ChannelBean("热点", true));
list.add(new ChannelBean("军事", true));
list.add(new ChannelBean("八卦", true));
list.add(new ChannelBean("游戏", true));
list.add(new ChannelBean("宠物", true));
list.add(new ChannelBean("汽车", true));
list.add(new ChannelBean("热卖", true));
list.add(new ChannelBean("外卖", true));
list.add(new ChannelBean("条目1", true));
list.add(new ChannelBean("条目2", true));
list.add(new ChannelBean("条目3", false));
list.add(new ChannelBean("条目4", false));
list.add(new ChannelBean("条目5", false));
list.add(new ChannelBean("条目6", false));
list.add(new ChannelBean("条目7", false));
list.add(new ChannelBean("条目8", false));
for (int i=0;i<list.size();i++){
if (list.get(i).isSelect()){
tab.addTab(tab.newTab().setText(list.get(i).getName()));
}
}
jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ChannelActivity.startChannelActivity(MainActivity.this,list);
}
});
}
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上
//清空之前的栏目
tab.removeAllTabs();
Type type=new TypeToken<ArrayList<ChannelBean>>(){}.getType();
//进行json解析
list = new Gson().fromJson(jsonStr, type);
//遍历结果,更新tablayout
for (int i=0;i<list.size();i++){
if (list.get(i).isSelect()){
tab.addTab(tab.newTab().setText(list.get(i).getName()));
}
}
}
}
}