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不可滑动的更多相关文章

  1. iOS开发——加载、滑动翻阅大量图片解决方案详解

    加载.滑动翻阅大量图片解决方案详解     今天分享一下私人相册中,读取加载.滑动翻阅大量图片解决方案,我想强调的是,编程思想无关乎平台限制. 我要详细说一下,在缩略图界面点击任意小缩略图后,进入高清 ...

  2. Android系列之Fragment(一)----Fragment加载到Activity当中

    Android上 的界面展示都是通过Activity实现的,Activity实在是太常用了.但是Activity也有它的局限性,同样的界面在手机上显示可能很好看, 在平板上就未必了,因为平板的屏幕非常 ...

  3. android开发之Fragment加载到一个Activity中

    Fragments 是android3.0以后添加的.主要是为了方便android平板端的开发.方便适应不同大小的屏幕.此代码是为了最简单的Fragment的使用,往一个Activity中添加Frag ...

  4. js/jquery控制页面动态加载数据 滑动滚动条自动加载事件--转他人的

    js/jquery控制页面动态加载数据 滑动滚动条自动加载事件--转他人的 相信很多人都见过瀑布流图片布局,那些图片是动态加载出来的,效果很好,对服务器的压力相对来说也小了很多 有手机的相信都见过这样 ...

  5. iOS开发之加载、滑动翻阅大量图片优化解决方案

    本文转载至 http://mobile.51cto.com/iphone-413267.htm 今天分享一下私人相册中,读取加载.滑动翻阅大量图片解决方案,我想强调的是,编程思想无关乎平台限制.我要详 ...

  6. 利用纯CSS美化checkbox和radio和滑动按钮的实现

    W3C提供的CheckBox和radio的原始样式非常的丑,而且在不同的额浏览器表现还不一样,使用常规的方法添加样式没法进行修改样式 一, 单选按钮 <html> <head> ...

  7. Fragment加载方式与数据通信

    一.加载方式 1. 静态加载 1.1 加载步骤 (1) 创建fragment:创建自定义Fragment类继承自Fragment类,同时将自定义Fragment类与Fragment视图绑定(将layo ...

  8. Android Demo 下拉刷新+加载更多+滑动删除

    小伙伴们在逛淘宝或者是各种app上,都可以看到这样的功能,下拉刷新和加载更多以及滑动删除,刷新,指刷洗之后使之变新,比喻突破旧的而创造出新的,比如在手机上浏览新闻的时候,使用下拉刷新的功能,我们可以第 ...

  9. Swiper 轮播插件 之 动态加载无法滑动

    1.原因:轮播图未完全动态加载完成,即初始化 2.方法一:ajax链式编程 $.ajax({ type: "get", url: serviceURL + "/listB ...

  10. ViewPage显示Fragment集合实现左右滑动并且出现tab栏--第三方开源--SlidingTabLayout和SlidingTabStrip实现

    注意:有关Fragment的方法和ViewPager的全部是android.support.v4包的,否则会报很多的错误 MainActivity: package com.zzw.fragmentt ...

随机推荐

  1. C++程序设计实验二 数组、指针与C++标准库

    Info.hpp文件源码 #include<iostream> #include<string> #include<iomanip> using namespace ...

  2. C++ MFC学习 (三)

    视窗口覆盖在框架窗口之上 如果有了 OnDraw 不要再用OnPaint  OnPaint会覆盖掉OnDraw所画内容 1 // MFC_Demo1View.cpp : CMFC_Demo1View ...

  3. sparksql的join有哪些及实现原理

    sparksql的3种join实现 1.Broadcast Join (小表对大表) 在数据库的常见模型中(比如星型模型或者雪花模型),表一般分为两种:事实表和维度表. 维度表一般指固定的.变动较少的 ...

  4. window下快速启动mysql,bat脚本

    cls @echo off:设置窗口字体颜色color 0a :设置窗口标题TITLE MySQL管理程序 call :checkAdmin goto menu:菜单:menuclsecho. ech ...

  5. ubuntu配置phpmyadmin

    之前已经把LNMP环境搭建好了 安装: sudo apt-get install phpmyadmin 安装必要依赖 sudo apt-get install php-mbstring sudo ap ...

  6. git remote prune origin删除本地有但在远程库已经不存在的分支

    先调用git remote show origin 该命令能够获取远端分支信息,你可以看到和本地和远端不同步的地方: 过时的就是和本地不同步的分支,本地已过时的表示你需要移除这个分支了. 这个时候你需 ...

  7. node 内存全局配置(--max-old-space-size)

    安装完了node和angular之后,使用powershell 窗口进行 ng build --prod打包,会提示内存溢出:JavaScript heap out of memory. 项目内打包解 ...

  8. 下载接口时出现:Try to run this command from the system terminal. Make sure that you use the correct version of 'pip' installed for your Python interpreter located at 'D:\python\demo\venv\Scripts\...的错误

    下载接口时出现:Try to run this command from the system terminal. Make sure that you use the correct version ...

  9. MacBook + 移动SSD实现三系统(Mac OS、windows、ubuntu)

    Windows系统安装 1.我们先要在windows的电脑上下载好,你需要的windows镜像文件(下载地址:https://msdn.itellyou.cn/,个人建议用迅雷下载),下载好winto ...

  10. Java_类与对象

    类与对象 概念 类是抽象的,概念的,代表一类事物,比如人类.猫类--,即它是数据类型. 对象是具体的,实际的,代表一个具体事物,即使实例. 类是对象的模板,对象是类的一个个体,对应一个实例 对象在内存 ...