静态频道管理
频道管理
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
布局
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout 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:id="@+id/dl"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/sele_r"
android:gravity="center"
android:text="首页"
android:textSize="20sp" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/sele_r"
android:gravity="center"
android:text="自选"
android:textSize="20sp" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/sele_r"
android:gravity="center"
android:text="资讯"
android:textSize="20sp" />
<RadioButton
android:id="@+id/radio4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/sele_r"
android:gravity="center"
android:text="我的"
android:textSize="20sp" />
</RadioGroup>
</LinearLayout>
<!--第二子布局是侧滑界面-->
<LinearLayout
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#507BB2"
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="30dp"
android:src="@mipmap/p1" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="150dp"
android:text="QQ钱包"
android:textColor="#ddd"
android:textSize="22sp" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:text="我的装扮"
android:textColor="#ddd"
android:textSize="22sp" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:text="我的设置"
android:textColor="#ddd"
android:textSize="22sp" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
主页面
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup;
private FragmentManager manager;
private FragmentTransaction transaction;
private Fragment01 f1;
private Fragment02 f2;
private Fragment03 f3;
private Fragment04 f4;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
radioGroup = findViewById(R.id.radiogroup);
//获取管理者
manager = getSupportFragmentManager();
//开启事务
transaction = manager.beginTransaction();
//碎片
f1 = new Fragment01();
//提交事务
transaction.add(R.id.framelayout, f1).commit();
//点击事件
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//一定要记得添加
hideFrag();
//开启事务
transaction = manager.beginTransaction();
switch (checkedId) {
case R.id.radio1:
transaction.show(f1).commit();
break;
case R.id.radio2:
if (f2 == null) {
//f2一定要提成全局
f2 = new Fragment02();
transaction.add(R.id.framelayout, f2).commit();
} else {
//有的话就显示
transaction.show(f2).commit();
}
break;
case R.id.radio3:
if (f3 == null) {
//f2一定要提成全局
f3 = new Fragment03();
transaction.add(R.id.framelayout, f3).commit();
} else {
//有的话就显示
transaction.show(f3).commit();
}
break;
case R.id.radio4:
if (f4 == null) {
//f2一定要提成全局
f4 = new Fragment04();
transaction.add(R.id.framelayout, f4).commit();
} else {
//有的话就显示
transaction.show(f4).commit();
}
break;
}
}
});
}
private void hideFrag() {
//再重新开启一个事务
transaction = manager.beginTransaction();
//不等于空或添加的时候
if (f1 != null && f1.isAdded()) {
transaction.hide(f1);
}
if (f2!= null && f2.isAdded()) {
transaction.hide(f2);
}
if (f3 != null && f3.isAdded()) {
transaction.hide(f3);
}
if (f4 != null && f4.isAdded()) {
transaction.hide(f4);
}
//一定要记得添加
transaction.commit();
}
}
顶部布局
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.TabLayout
android:id="@+id/tab1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</android.support.design.widget.TabLayout>
<ImageView
android:id="@+id/channel_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/channel" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
//-------------------------------------------------------------------------------------------------
### 横向滑动与频道管理
public class Fragment01 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment01,container,false);
//找控件
ViewPager pager = view.findViewById(R.id.pager1);
TabLayout tab = view.findViewById(R.id.tab1);
ImageView channel = view.findViewById(R.id.channel_tv);
//页卡数据
ArrayList tabs = new ArrayList<>();
tabs.add("推荐");
tabs.add("热门");
tabs.add("科技");
tabs.add("创投");
tabs.add("数码");
//设置模式
tab.setTabMode(TabLayout.MODE_FIXED);
//给TabLayout添加数据
tab.addTab(tab.newTab().setText(tabs.get(0)));
tab.addTab(tab.newTab().setText(tabs.get(1)));
tab.addTab(tab.newTab().setText(tabs.get(2)));
tab.addTab(tab.newTab().setText(tabs.get(3)));
tab.addTab(tab.newTab().setText(tabs.get(4)));
//ViewPager数据
ArrayList<Fragment> fragments = new ArrayList<>();
fragments.add(new Frag01());
fragments.add(new Frag02());
fragments.add(new Frag03());
fragments.add(new Frag04());
fragments.add(new Frag05());
//适配器
MyViewPagerAdapter myViewPagerAdapter = new MyViewPagerAdapter(getActivity().getSupportFragmentManager(), tabs, fragments);
pager.setAdapter(myViewPagerAdapter);
//关联
tab.setupWithViewPager(pager);
//点击跳转到频道管理页面
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((AppCompatActivity) getActivity(), list);
}
});
return view;
}
}