1.首先在工程 build.gradle下添加一条依赖 allprojects -> repositories- >
maven {url "https://jitpack.io"}
2.在此Module的build.gradle - > dependencies ->
implementation 'com.github.andyoom:draggrid:v1.0.1'
3.一定要导入Gson 依赖
4.用到TabLayout 导入design依赖
注:长按可调动位置,需要加入权限(震动权限)
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
android
package jx.com.guoziheng20111121;
import android.content.Intent;
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.os.Bundle;
import android.view.View;
import android.widget.TextView;
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;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TabLayout tabLayout;
private TextView img_but;
private ViewPager viewPager;
private MyFragmentAdapter adapter;
private ArrayList<ChannelBean> channelBeans = new ArrayList<ChannelBean>();
private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
private String stringExtra;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源id
initView();
initData();
}
private void initView() {
//获取资源id
tabLayout = findViewById(R.id.tab);
img_but = findViewById(R.id.text);
viewPager = findViewById(R.id.viewpager);
//点击事件(可以这样写,也可以直接向下面那样这)
img_but.setOnClickListener(this);
/*img_but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ChannelActivity.startChannelActivity(MainActivity.this,channelBeans);
}
});*/
}
private void initData() {
//添加数据 true的话在收藏频道任务栏 false的话在下面的项目栏里
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("头条",true));
channelBeans.add(new ChannelBean("图片",true));
channelBeans.add(new ChannelBean("小说",false));
channelBeans.add(new ChannelBean("体育",false));
channelBeans.add(new ChannelBean("财经",false));
channelBeans.add(new ChannelBean("房产",false));
channelBeans.add(new ChannelBean("时尚",false));
channelBeans.add(new ChannelBean("美食",false));
channelBeans.add(new ChannelBean("养生",false));
channelBeans.add(new ChannelBean("情感",false));
//添加对应的Fragment
fragments.add(new AFragment());
fragments.add(new BFragment());
fragments.add(new CFragment());
fragments.add(new AFragment());
fragments.add(new BFragment());
fragments.add(new CFragment());
fragments.add(new AFragment());
fragments.add(new BFragment());
fragments.add(new CFragment());
fragments.add(new AFragment());
fragments.add(new BFragment());
fragments.add(new CFragment());
fragments.add(new AFragment());
fragments.add(new BFragment());
fragments.add(new CFragment());
fragments.add(new AFragment());
fragments.add(new BFragment());
fragments.add(new CFragment());
//遍历判断是否是选中的标签
for (int i = 0; i < channelBeans.size(); i++) {
if(channelBeans.get(i).isSelect()){
//如果是选中的话添加在TabLayout
String name = channelBeans.get(i).getName();
tabLayout.addTab(tabLayout.newTab().setText(name));
}
}
//设置适配器
adapter = new MyFragmentAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
//绑定
tabLayout.setupWithViewPager(viewPager);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
///为true表示是频道管理回调回来的
if(requestCode == ChannelActivity.REQUEST_CODE&&resultCode == ChannelActivity.RESULT_CODE){
//得到栏目管理的结果
stringExtra = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);
//清空tabLayout原来的数据
tabLayout.removeAllTabs();
//解析
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<ChannelBean>>(){}.getType();
把新选择的栏目结果更新到tablayout上
channelBeans = gson.fromJson(stringExtra,type);
//重新遍历选中的栏目,添加到tabLayout上
for (int i = 0; i < channelBeans.size(); i++) {
if(channelBeans.get(i).isSelect()){
tabLayout.addTab(tabLayout.newTab().setText(channelBeans.get(i).getName()));
}
}
//更新一下适配器
adapter.notifyDataSetChanged();
}
}
@Override
public void onClick(View v) {
//点击事件,判断id,是text的话执行
switch (v.getId()) {
case R.id.text:
ChannelActivity.startChannelActivity(MainActivity.this, channelBeans);
break;
}
}
//适配器
class MyFragmentAdapter extends FragmentPagerAdapter {
public MyFragmentAdapter(FragmentManager fm) {
super(fm);
}
//返回的布局
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
//把当前position
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return channelBeans.get(position).getName();
}
//遍历判断是否是选中的,返回选中的条数
@Override
public int getCount() {
int count = 0;
for (int i = 0; i < channelBeans.size(); i++) {
if(channelBeans.get(i).isSelect()){
count ++ ;
}
}
return count;
}
}
}
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/tab"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
></android.support.design.widget.TabLayout>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="40sp"
/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
></android.support.v4.view.ViewPager>
</LinearLayout>
<!--
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
></android.support.design.widget.TabLayout>
<TextView
android:id="@+id/text_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="+"
android:textSize="26sp"
android:layout_weight="1"
/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
></android.support.v4.view.ViewPager>
</LinearLayout>-->