fragment加radio不可滑动
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
private ArrayList<Fragment> arrayList;
private RadioGroup rg;
private RadioButton[] radioButtons;
private FragmentManager manager;
private int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
arrayList = new ArrayList<>();
NewsFragment fragment1 = new NewsFragment();
AppraisalFragment fragment2 = new AppraisalFragment();
PictureFragment fragment3 = new PictureFragment();
CommentFragment fragment4 = new CommentFragment();
arrayList.add(fragment1);
arrayList.add(fragment2);
arrayList.add(fragment3);
arrayList.add(fragment4);
radioButtons=new RadioButton[rg.getChildCount()];
for (int i = 0; i < radioButtons.length; i++) {
radioButtons[i]= (RadioButton) rg.getChildAt(i);
}
manager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=manager.beginTransaction();
fragmentTransaction.add(R.id.main_layout,arrayList.get(0));
fragmentTransaction.commit();
radioButtons[0].setChecked(true);
rg.setOnCheckedChangeListener(this);
}
private void initView() {
rg = (RadioGroup) findViewById(R.id.rg);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentTransaction transaction=manager.beginTransaction();
for (int i = 0; i < radioButtons.length; i++) {
if (radioButtons[i].getId()==checkedId){
if (arrayList.get(i).isAdded()){
transaction.show(arrayList.get(i)).hide(arrayList.get(count)).commit();
}else {
transaction.add(R.id.main_layout,arrayList.get(i)).hide(arrayList.get(count)).commit();
}
count=i;
}
}
}
}
//布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.douyu.main.MainActivity">
<FrameLayout
android:layout_weight="1"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<RadioGroup
android:id="@+id/rg"
android:background="@android:color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="56dp">
<RadioButton
android:textColor="@color/textcolor"
android:gravity="center"
android:text="首页"
android:layout_marginTop="2dp"
android:button="@null"
android:drawableTop="@drawable/homepage_item"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:textColor="@color/textcolor"
android:gravity="center"
android:text="分类"
android:layout_marginTop="2dp"
android:drawableTop="@drawable/classify_item"
android:button="@null"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:textColor="@color/textcolor"
android:gravity="center"
android:text="关注"
android:layout_marginTop="2dp"
android:drawableTop="@drawable/attention_item"
android:button="@null"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:textColor="@color/textcolor"
android:gravity="center"
android:text="鱼吧"
android:layout_marginTop="2dp"
android:drawableTop="@drawable/bar_item"
android:button="@null"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:textColor="@color/textcolor"
android:gravity="center"
android:text="我的"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:drawableTop="@drawable/user_item"
android:button="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
//text文字颜色,在res下创建color文件夹
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/black" android:state_checked="false"/>
<item android:color="#ff6600" android:state_checked="true"/>
</selector>
fragment加radio不可滑动的更多相关文章
- iOS开发——加载、滑动翻阅大量图片解决方案详解
加载.滑动翻阅大量图片解决方案详解 今天分享一下私人相册中,读取加载.滑动翻阅大量图片解决方案,我想强调的是,编程思想无关乎平台限制. 我要详细说一下,在缩略图界面点击任意小缩略图后,进入高清 ...
- Android系列之Fragment(一)----Fragment加载到Activity当中
Android上 的界面展示都是通过Activity实现的,Activity实在是太常用了.但是Activity也有它的局限性,同样的界面在手机上显示可能很好看, 在平板上就未必了,因为平板的屏幕非常 ...
- android开发之Fragment加载到一个Activity中
Fragments 是android3.0以后添加的.主要是为了方便android平板端的开发.方便适应不同大小的屏幕.此代码是为了最简单的Fragment的使用,往一个Activity中添加Frag ...
- js/jquery控制页面动态加载数据 滑动滚动条自动加载事件--转他人的
js/jquery控制页面动态加载数据 滑动滚动条自动加载事件--转他人的 相信很多人都见过瀑布流图片布局,那些图片是动态加载出来的,效果很好,对服务器的压力相对来说也小了很多 有手机的相信都见过这样 ...
- iOS开发之加载、滑动翻阅大量图片优化解决方案
本文转载至 http://mobile.51cto.com/iphone-413267.htm 今天分享一下私人相册中,读取加载.滑动翻阅大量图片解决方案,我想强调的是,编程思想无关乎平台限制.我要详 ...
- 利用纯CSS美化checkbox和radio和滑动按钮的实现
W3C提供的CheckBox和radio的原始样式非常的丑,而且在不同的额浏览器表现还不一样,使用常规的方法添加样式没法进行修改样式 一, 单选按钮 <html> <head> ...
- Fragment加载方式与数据通信
一.加载方式 1. 静态加载 1.1 加载步骤 (1) 创建fragment:创建自定义Fragment类继承自Fragment类,同时将自定义Fragment类与Fragment视图绑定(将layo ...
- Android Demo 下拉刷新+加载更多+滑动删除
小伙伴们在逛淘宝或者是各种app上,都可以看到这样的功能,下拉刷新和加载更多以及滑动删除,刷新,指刷洗之后使之变新,比喻突破旧的而创造出新的,比如在手机上浏览新闻的时候,使用下拉刷新的功能,我们可以第 ...
- Swiper 轮播插件 之 动态加载无法滑动
1.原因:轮播图未完全动态加载完成,即初始化 2.方法一:ajax链式编程 $.ajax({ type: "get", url: serviceURL + "/listB ...
- ViewPage显示Fragment集合实现左右滑动并且出现tab栏--第三方开源--SlidingTabLayout和SlidingTabStrip实现
注意:有关Fragment的方法和ViewPager的全部是android.support.v4包的,否则会报很多的错误 MainActivity: package com.zzw.fragmentt ...
随机推荐
- CSS滚动条样式修改::-webkit-scrollbar
修改滚动条样式通过伪元素::-webkit-scrollbar:::-webkit-scrollbar - CSS(层叠样式表) | MDN (mozilla.org) :-webkit-scroll ...
- WDA学习(27):RoadMap使用
1.20 UI Element:RoadMap使用 本实例测试创建RoadMap; 运行结果: 点击2,Input显示输入航班Id 点击3,根据input输入,查询航班信息 1.创建Component ...
- 081_Introducing trigger handler class
案例分析: 我们对一个Object写多个独立得Trigger. 但最终这不是最好的做法. 在Salesforce中,只为每个SObject设置一个触发器总是好的. 原因:每个独立触发器的执行顺序始终未 ...
- 无法识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次
无法识别为 cmdlet.函数.脚本文件或可运行程序的名称.请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次 解决方法: 1.在开始菜单里找到Windows PowerShell.并以管理 ...
- Blog-3
前言 这几周的作业所涉及的知识点有数据的封装和.继承与多态.正则表达式,还有抽象类和接口,另外还有javafx的一些基本知识.题量适中,但是难度对于我来说是比较大的.总的来说就是跟以前的题目差不多,只 ...
- 2023最新版Selenium 4.6.0语法快速入门
简介 Selenium是一款强而有力的前端应用测试工具,也非常适合搭配Python作为网络爬虫的工具:Selenium可以模拟使用者所有浏览器操作的动作,包括输入文本.点击按钮及拖拽进度条等.有鑑于新 ...
- uniapp自定义组件的使用--记录
在 pages.json同级目录下 创建目录和文件: components: videoComp: VideoPlaySelf.vue VideoPlaySelf.vue 内容 <templat ...
- RocketMq 下载安装
下载地址 https://rocketmq.apache.org/zh/download linux安装步骤 启动nameserver bin目录下启动nameserver nohup sh mqna ...
- 树莓派zero W ,连接墨水屏(2.13inch_e-Paper_HAT (V3))
墨水瓶型号: 2.13inch_e-Paper_HAT (V3) https://www.waveshare.net/wiki/2.13inch_e-Paper_HAT 与树莓派 Zero 连接 ...
- 国产低功耗Soc蓝牙语音遥控器芯片HS6621 /OM6621
随着物联网技术不断发展,家用电器往智能化方向持续迭代,使用红外遥控器这种传统的互动方式已经满足不了实际的使用需求,蓝牙语音遥控器作为人机交互新载体,逐渐取代传统红外遥控器成为家居设备的标配.相比于传统 ...