RadioButton实现Fragment切换

来源:互联网 发布:iphone倒计时软件 编辑:程序博客网 时间:2024/05/18 19:21

需要实现效果大致如下:




RadioButton切换Fragment最大的好处莫过于界面效果变换非常稳定了。


布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"                android:layout_width="match_parent"                android:layout_height="match_parent">    <!--RadioButton实现点击切换Fragment-->    <FrameLayout        android:id="@+id/fl_info_framlayout"        android:layout_width="200dp"        android:layout_height="200dp"        android:layout_centerInParent="true"        android:background="#4400ffff"/>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="110dp">        <RadioGroup            android:id="@+id/rg_user_radiogroup1"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:orientation="vertical"            android:paddingLeft="5dp"            android:paddingRight="5dp">            <RadioButton                android:id="@+id/rb_userinfo1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:button="@null"                android:text="我的"                android:textSize="14sp"/>            <RadioButton                android:id="@+id/rb_userinfo2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:background="#00000000"                android:button="@null"                android:text="路线"                android:textSize="14sp"/>            <RadioButton                android:id="@+id/rb_userinfo3"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:background="#00000000"                android:button="@null"                android:text="心率"                android:textSize="14sp"/>            <RadioButton                android:id="@+id/rb_userinfo4"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:background="#00000000"                android:button="@null"                android:text="积分"                android:textSize="14sp"/>        </RadioGroup>        <RadioGroup            android:layout_alignParentRight="true"            android:id="@+id/rg_user_radiogroup2"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:orientation="vertical"            android:paddingLeft="5dp"            android:paddingRight="5dp">            <RadioButton                android:id="@+id/rb_userinfo5"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:button="@null"                android:text="卡路里"                android:textSize="14sp"/>            <RadioButton                android:id="@+id/rb_userinfo6"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:background="#00000000"                android:button="@null"                android:text="周报"                android:textSize="14sp"/>            <RadioButton                android:id="@+id/rb_userinfo7"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:background="#00000000"                android:button="@null"                android:text="月报"                android:textSize="14sp"/>            <RadioButton                android:id="@+id/rb_userinfo8"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:background="#00000000"                android:button="@null"                android:text="..."                android:textSize="14sp"/>        </RadioGroup>    </RelativeLayout></RelativeLayout>



MainActivity:

package com.ut.radiobuttondynamicadd;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.widget.FrameLayout;import android.widget.RadioButton;import android.widget.RadioGroup;import com.ut.radiobuttondynamicadd.factory.UserFragmentFactory;import butterknife.ButterKnife;import butterknife.InjectView;public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {    @InjectView(R.id.rb_userinfo1)    RadioButton mRbUserinfo1;    @InjectView(R.id.rg_user_radiogroup1)    RadioGroup mRgUserRadiogroup1;    @InjectView(R.id.rg_user_radiogroup2)    RadioGroup mRgUserRadiogroup2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.inject(this);        initView();    }    private void initView() {        mRgUserRadiogroup1.setOnCheckedChangeListener(this);        mRgUserRadiogroup2.setOnCheckedChangeListener(this);        mRbUserinfo1.setChecked(true);    }    @Override    public void onCheckedChanged(RadioGroup group, int checkedId) {        switch (checkedId) {            case R.id.rb_userinfo1:                changeFragment(UserFragmentFactory.createFragment(0));                break;            case R.id.rb_userinfo2:                changeFragment(UserFragmentFactory.createFragment(1));                break;            case R.id.rb_userinfo3:                changeFragment(UserFragmentFactory.createFragment(2));                break;            case R.id.rb_userinfo4:                changeFragment(UserFragmentFactory.createFragment(3));                break;            case R.id.rb_userinfo5:                changeFragment(UserFragmentFactory.createFragment(4));                break;            case R.id.rb_userinfo6:                changeFragment(UserFragmentFactory.createFragment(5));                break;            case R.id.rb_userinfo7:                changeFragment(UserFragmentFactory.createFragment(6));                break;            case R.id.rb_userinfo8:                changeFragment(UserFragmentFactory.createFragment(7));                break;        }    }    private void changeFragment(Fragment targetFragment) {        getSupportFragmentManager().beginTransaction()                .replace(R.id.fl_info_framlayout, targetFragment, "fragment")                .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)                .commit();    }}

~下载Demo链接~






0 0
原创粉丝点击