频道管理
1. 在app的gradle里面
implementation 'com.github.andyoom:draggrid:v1.0.1'
2.在项目的build.gradle中添加
maven {url "https://jitpack.io"}
3.权限
<!-- 在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"/>
4.在功能清单中配置ChannelActivity
<activity android:name=“com.andy.library.ChannelActivity”…>
布局
<?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=".activity.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="0dp"
app:tabSelectedTextColor="@color/colorAccent"
android:layout_height="match_parent"
android:layout_weight="1"></android.support.design.widget.TabLayout>
<ImageView
android:id="@+id/iv_channel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/channel" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></android.support.v4.view.ViewPager>
</LinearLayout…>
主页面
public class MainActivity extends AppCompatActivity {
private TabLayout tab;
private ImageView iv_channel;
private ViewPager vp;
private List<String> tabs = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
tab = findViewById(R.id.tab);
vp = findViewById(R.id.vp);
iv_channel = findViewById(R.id.iv_channel);
tabs.add("tab1");
tabs.add("tab2");
tabs.add("tab3");
//滑动模式
tab.setTabMode(TabLayout.MODE_SCROLLABLE);
//添加标签
tab.addTab(tab.newTab().setText(tabs.get(0)));
tab.addTab(tab.newTab().setText(tabs.get(1)));
tab.addTab(tab.newTab().setText(tabs.get(2)));
MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager(), tabs);
vp.setAdapter(adapter);
//关联
tab.setupWithViewPager(vp);
//点击跳转到频道管理页面
iv_channel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//初始十条数据
List<ChannelBean> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
ChannelBean bean1 = new ChannelBean("item-" + i, i < 5 ? true : false);
list.add(bean1);
}
//相当于startActivityForResult() 跳转到频道管理页面
ChannelActivity.startChannelActivity(MainActivity.this, list);
}
});
}
//回传数据
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("xxx", data.getStringExtra("json"));
String json = data.getStringExtra("json");
try {
//数据最外层是集合
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
String isSelect = jsonObject.getString("isSelect");
if (isSelect.equals("true")) {
tabs.add(name);
}
}
MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager(), tabs);
vp.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
适配器
public class MyViewPagerAdapter extends FragmentPagerAdapter {
List tabs;
public MyViewPagerAdapter(FragmentManager supportFragmentManager, List<String> tabs) {
super(supportFragmentManager);
this.tabs = tabs;
}
@Override
public Fragment getItem(int i) {
MyFragment fragment = MyFragment.newInstance(i);
return fragment;
}
@Override
public int getCount() {
return tabs.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return tabs.get(position);
}
}
Fragment页面
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment" />
</android.support.constraint.ConstraintLayout>
//----------------------------------------------------------------------------------------------
public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getContext(), R.layout.fragment_my, null);
return view;
}
public static MyFragment newInstance(int index) {
MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putInt("index", index);
fragment.setArguments(bundle);
return fragment;
}
}