RadioGroup与Fragment使用

RadioGroup与Fragment使用

RadioGroup中使用类组单选按钮。如果我们选中一个单选按钮属于一个单选按钮组,它会自动取消选中同一组内的任何先前检查的单选按钮。

RadioGroup 属性



属性 描述
android:checkedButton 这是子单选按钮应该在默认情况下此单选组内进行检查的ID

继承自android.view.View类:

属性 描述
android:background 可拉伸作为背景
android:contentDescription 定义文本简要描述了视图内容
android:id 对此视图提供一个标识符名称
android:onClick 在本视图的上下文视图被点击时调用的方法的名称
android:visibility 控制视图的初始可视性
故可以用于app底部导航栏,在主类上在套上相应的fragment,实现导航功能。看例子:

先看布局文件:底部添加了4个RadioButton。FrameLayout中切换fragment

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/layFrame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        
    </FrameLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/_0.5dp"
        android:background="@color/line_1"/>
    <RadioGroup
        android:id="@+id/main_tab_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_49dp"
        android:gravity="center"
        android:background="@color/white"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/main_tab_item_jingxuan"
            style="@style/main_tab_item"
            android:checked="true"
            android:drawableTop="@drawable/main_tab_activity_bg"
            android:drawablePadding="@dimen/_3dp"
            android:text="@string/home" />

        <RadioButton
            android:id="@+id/main_tab_item_zhuanti"
            style="@style/main_tab_item"
            android:drawableTop="@drawable/main_tab_account_bg"
            android:drawablePadding="@dimen/_3dp"
            android:text="@string/invest" />

        <RadioButton
            android:id="@+id/main_tab_item_hongdong"
            style="@style/main_tab_item"
            android:drawableTop="@drawable/main_tab_home_bg"
            android:drawablePadding="@dimen/_3dp"
            android:text="@string/activity" />

        <RadioButton
            android:id="@+id/main_tab_item_article"
            style="@style/main_tab_item"
            android:drawableTop="@drawable/main_tab_investment_bg"
            android:drawablePadding="@dimen/_3dp"
            android:text="@string/account" />
    </RadioGroup>
</LinearLayout>
因为4个RadioButton中的大部分属性是一样的,所以抽出一个样式style="@style/main_tab_item"如下

<style name="main_tab_item">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_weight">1</item>
        <item name="android:button">@color/transparent</item>
        <item name="android:background">@color/transparent</item>
        <item name="android:gravity">center</item>
        <item name="android:textSize">11sp</item>
        <item name="android:textColor">@color/main_tab_item_text_color</item>
    </style>

再看住类中添加RadioGroup与Fragment的联动就行了:


package com.xxxx.xxxx.ui.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.ganqianwang.ganqianwang.R;
import com.ganqianwang.ganqianwang.listener.HttpProgressOnNextListener;
import com.ganqianwang.ganqianwang.model.DownInfo;
import com.ganqianwang.ganqianwang.model.HttpResult;
import com.ganqianwang.ganqianwang.model.OpenadEntity;
import com.ganqianwang.ganqianwang.network.api.HttpApi;
import com.ganqianwang.ganqianwang.network.download.manager.HttpDownManager;
import com.ganqianwang.ganqianwang.service.DownloadService;
import com.ganqianwang.ganqianwang.subscribers.ProgressSubscriber;
import com.ganqianwang.ganqianwang.subscribers.SubscriberOnNextListener;
import com.ganqianwang.ganqianwang.ui.fragment.AccountFragment;
import com.ganqianwang.ganqianwang.ui.fragment.ActivityFragment;
import com.ganqianwang.ganqianwang.ui.fragment.HomeFragment;
import com.ganqianwang.ganqianwang.ui.fragment.InvestFragment;
import com.ganqianwang.ganqianwang.utils.ActivityCollector;
import com.ganqianwang.ganqianwang.utils.PasswordHelp;
import com.ganqianwang.ganqianwang.utils.PreferenceUtils;
import com.ganqianwang.ganqianwang.utils.Utils;
import com.ganqianwang.ganqianwang.widget.UpdataDialog;
import com.ganqianwang.ganqianwang.widget.UpdataDialog.SettingDialogCallBack;
import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.ganqianwang.ganqianwang.ui.MyApplication.islogin;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener,HomeFragment.OnFragmentInteractionListener{

    @BindView(R.id.layFrame)
    FrameLayout layFrame;
    @BindView(R.id.main_tab_item_jingxuan)
    RadioButton mainTabItemJingxuan;
    @BindView(R.id.main_tab_item_zhuanti)
    RadioButton mainTabItemZhuanti;
    @BindView(R.id.main_tab_item_hongdong)
    RadioButton mainTabItemHongdong;
    @BindView(R.id.main_tab_item_article)
    RadioButton mainTabItemArticle;
    @BindView(R.id.main_tab_bar)
    RadioGroup mainTabBar;

    public int getTab() {
        return tab;
    }
    private int tab;

    public RadioGroup getMainTabBar() {
        return mainTabBar;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public LinearLayout getLlWindowToolbar() {
        return llWindowToolbar;
    }


    private void initview() {
        StatusBarUtil.setTransparentForImageViewInFragment(MainActivity.this, null);
   	//设置监听
        mainTabBar.setOnCheckedChangeListener(this);

        fragments = getFragmnets();
        setDefaultFragment();

    }
	//添加fragment的集合
    private ArrayList<Fragment> getFragmnets() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(HomeFragment.newInstance("首页"));
        fragments.add(InvestFragment.newInstance("投资"));
        fragments.add(ActivityFragment.newInstance("活动"));
        fragments.add(AccountFragment.newInstance("账户"));
        return fragments;
    }


    /**
     * 设置默认的
     */
    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.layFrame, HomeFragment.newInstance("Home"));
        transaction.commit();
    }

	//4个按钮的监听
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int childCount = group.getChildCount();
        RadioButton btnButton = null;
        for (int i = 0; i < childCount; i++) {
            btnButton = (RadioButton) group.getChildAt(i);
            if (btnButton.isChecked()) {
                checkedIndex = i;
                break;
            }
        }


        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment fragment = fragments.get(checkedIndex);
        if (checkedIndex==1) {
            fragment =new InvestFragment();
            Bundle args = new Bundle();
            args.putInt("position", position);
            fragment.setArguments(args);
        }
        ft.replace(R.id.layFrame, fragment);
        ft.commit();
        switch (checkedIndex) {
            case 0:
                StatusBarUtil.setTransparentForImageViewInFragment(MainActivity.this, null);
                mTitle.setText("");
                llWindowToolbar.setVisibility(View.GONE);
                toolbar.setVisibility(View.GONE);
                break;
            case 1:
                StatusBarUtil.setTransparentForImageViewInFragment(MainActivity.this, toolbar);
                StatusBarUtil.setColor(MainActivity.this, getResources().getColor(R.color.colorPrimary), 0);
                mTitle.setText("投资列表");
                llWindowToolbar.setVisibility(View.GONE);
                toolbar.setVisibility(View.VISIBLE);
                break;
            case 2:
                StatusBarUtil.setTransparentForImageViewInFragment(MainActivity.this, toolbar);
                StatusBarUtil.setColor(MainActivity.this, getResources().getColor(R.color.colorPrimary), 0);
                mTitle.setText("活动中心");
                llWindowToolbar.setVisibility(View.GONE);
                toolbar.setVisibility(View.VISIBLE);
                break;
            case 3:
                StatusBarUtil.setTransparentForImageViewInFragment(MainActivity.this, toolbar);
                StatusBarUtil.setColor(MainActivity.this, getResources().getColor(R.color.colorPrimary), 0);
                mTitle.setText("我的账户");
                toolbar.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }


    }


  

    @Override
    public void onFragmentInteraction(int position) {
        HomeFragment articleFrag = (HomeFragment)
                getSupportFragmentManager().findFragmentById(R.id.layFrame);
        if (articleFrag != null) {
            this.position=position;
            mainTabItemZhuanti.setChecked(true);
        }

    }
}

在写出对应的4个fragment就行了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android底部导航栏的实现可以通过使用RadioGroupFragment结合使用来实现。首先,在布局文件中添加一个RadioGroup,然后在其中添加多个RadioButton,每个RadioButton代表一个导航项。接着,使用Fragment来实现每个导航项对应的页面内容。在RadioGroup中设置监听器,当用户点击某个RadioButton时,切换到对应的Fragment页面。这样就可以实现一个简单的底部导航栏了。 ### 回答2: Android底部导航栏的实现一般可以通过RadioGroupFragment结合使用来完成。首先,在XML布局文件中定义一个RadioGroup,并在其中添加多个RadioButton,每个RadioButton对应导航栏中的一个选项。 接下来,在Activity中,我们需要定义一个Fragment的容器,用于加载不同的Fragment页面。在RadioGroup的选择监听器中,根据所选中的RadioButton的id,切换对应的Fragment页面。 在切换Fragment页面时,可以使用FragmentManager和FragmentTransaction来实现。通过FragmentManager的beginTransaction()方法获取一个FragmentTransaction的实例,然后使用replace()方法将容器中的Fragment替换为选中的目标Fragment,最后通过commit()方法提交事务即可完成页面切换。 为了方便管理和切换Fragment,可以定义一个Fragment的集合,用于存放所有的Fragment实例,并在选择监听器中根据RadioButton的顺序获取对应的Fragment。 另外,为了保持Fragment的状态,在切换Fragment时可以使用hide()和show()方法而不是replace()来隐藏和显示Fragment,这样可以避免Fragment被重复实例化。 总结来说,通过RadioGroupFragment的结合使用,我们可以实现Android底部导航栏的功能。通过监听RadioGroup中RadioButton的选择事件,来切换不同的Fragment页面。这样可以实现底部导航栏的选项切换,同时保持Fragment的状态。 ### 回答3: Android底部导航栏的实现可以通过使用RadioGroupFragment来实现。 首先,我们可以在布局文件中创建一个包含多个RadioButton的RadioGroup,这些RadioButton将作为底部导航栏的按钮。我们可以为每个RadioButton设置图标和文本,以表示不同的导航选项。通过设置RadioGroup的布局属性可以将其放置在屏幕的底部。 接下来,我们需要创建对应的Fragment,并在Activity中使用FragmentTransaction来进行Fragment的切换。在FragmentTransaction中,我们可以通过调用replace方法来替换Activity中的显示内容,将选中的RadioButton对应的Fragment显示出来。 当用户点击底部导航栏的RadioButton时,我们可以通过设置RadioGroup的OnCheckedChangeListener来监听选中项的变化。当选中项发生变化时,我们可以获取选中的RadioButton对应的Fragment,并使用FragmentTransaction来进行Fragment的切换,从而显示选中的Fragment。 此外,为了更好地控制底部导航栏的切换效果,我们还可以使用fragment的缓存机制以提高切换的效率,避免每次切换都重新创建Fragment对象。 总之,通过结合RadioGroupFragment使用,我们可以方便地实现Android底部导航栏的功能,使用户可以方便地在不同的导航选项之间进行切换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值