RadioGroup+Fragment实现页面之间的切换

来源:互联网 发布:传奇盗取管理权限软件 编辑:程序博客网 时间:2024/05/31 18:48

1.现在好多APP都用的这种方法,自己开发的时候也写了一个这样的框架,点击是可以实现切换的.先上效果图



2.参考代码

MainActivity 

package com.ythl.shouyoufanli;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import com.ythl.fragment.FragmentFactory;/** * @author Administrator * 主界面的活动界面 */public class MainActivity extends FragmentActivity {private FragmentManager mFragmentManager;    private RadioGroup radioGroup;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity);        //获取FragmentManager        mFragmentManager = getSupportFragmentManager();                //获取radioGroup控件        radioGroup = (RadioGroup) findViewById(R.id.rg_tab);                  //监听点击按钮事件,实现不同Fragment之间的切换        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {               @Override       public void onCheckedChanged(RadioGroup group, int checkedId) {                FragmentTransaction transaction = mFragmentManager.beginTransaction();                Fragment fragment = FragmentFactory.getInstanceByIndex(checkedId);                transaction.replace(R.id.fl_content, fragment);                transaction.commit();            }        });    }}


activity.xml


<?xml version="1.0" encoding="utf-8"?><LinearLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    xmlns:android="http://schemas.android.com/apk/res/android">    <FrameLayout        android:id="@+id/fl_content"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="1" />    <RadioGroup        android:id="@+id/rg_tab"        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@drawable/bottom_tab_bg">        <RadioButton            android:id="@+id/rb_gamecenter"            style="@style/main_radiobutton_icon"            android:drawableTop="@drawable/ic_launcher"            android:text="游戏中心" />        <RadioButton            android:id="@+id/rb_giftcenter"            style="@style/main_radiobutton_icon"            android:drawableTop="@drawable/ic_launcher"            android:text="礼包中心" />        <RadioButton            android:id="@+id/rb_tradecenter"            style="@style/main_radiobutton_icon"            android:drawableTop="@drawable/ic_launcher"            android:text="交易中心" />        <RadioButton            android:id="@+id/rb_accountscenter"            style="@style/main_radiobutton_icon"            android:drawableTop="@drawable/ic_launcher"            android:text="帐号中心" />    </RadioGroup>    </LinearLayout>

FragmentFactory

package com.ythl.fragment;import com.ythl.shouyoufanli.R;import android.support.v4.app.Fragment;/** * @author Administrator * 定义一个工厂模式, * 来实现根据下标的位置返回相应的Fragment */public class FragmentFactory {    public static Fragment getInstanceByIndex(int index) {        Fragment fragment = null;        switch (index) {            case R.id.rb_gamecenter:                fragment = new GameCenterFragment();                break;            case R.id.rb_giftcenter:                fragment = new GiftCenterFragment();                break;            case R.id.rb_tradecenter:                fragment = new TradeCenterFragment();                break;            case R.id.rb_accountscenter:                fragment = new AccountsCenterFragment();                break;        }        return fragment;    }}


BaseFragment


package com.ythl.fragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.ythl.shouyoufanli.MainActivity;/** * @author Administrator * fragment的基类 * 抽取fragment的共性,复用代码 */public abstract class BaseFragment extends Fragment {//主页面的上下文public  MainActivity mContext;@Overridepublic void onCreate(Bundle savedInstanceState) {//获取上下文mContext = (MainActivity) getActivity();super.onCreate(savedInstanceState);}//一定要实现的方法@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return initView();}/** * 定义为抽象方法,子类必须实现 * @return */public abstract View initView();/** * 初始化数据 */public void initData() {}/** * 初始化事件 */public void initEvent() {}//此方法只执行一次@Overridepublic void onActivityCreated(Bundle savedInstanceState) {//只要activity 不销毁,此方法 就只调用一次initData();initEvent();super.onActivityCreated(savedInstanceState);}@Overridepublic void onStart() {super.onStart();}}

每一个Fragment中的代码
AccountsCenterFragment

package com.ythl.fragment;import android.graphics.Color;import android.view.View;import android.widget.TextView;/** * @author Administrator * 帐号中心的Fragment */public class AccountsCenterFragment extends BaseFragment{@Overridepublic View initView() {TextView tv = new TextView(mContext);tv.setText("帐号中心");tv.setTextColor(Color.BLACK);tv.setTextSize(30);return tv;}}


GameCenterFragment

package com.ythl.fragment;import android.graphics.Color;import android.view.View;import android.widget.TextView;/** * @author Administrator * 游戏中心的Fragment */public class GameCenterFragment extends BaseFragment{@Overridepublic View initView() {TextView tv = new TextView(mContext);tv.setText("游戏中心");tv.setTextColor(Color.BLACK);tv.setTextSize(30);return tv;}}


GiftCenterFragment

package com.ythl.fragment;import android.graphics.Color;import android.view.View;import android.widget.TextView;/** * @author Administrator * 礼包中心的Fragment */public class GiftCenterFragment extends BaseFragment{@Overridepublic View initView() {TextView tv = new TextView(mContext);tv.setText("礼包中心");tv.setTextColor(Color.BLACK);tv.setTextSize(30);return tv;}}


TradeCenterFragment

package com.ythl.fragment;import android.graphics.Color;import android.view.View;import android.widget.TextView;/** * @author Administrator * 交易中心的Fragment */public class TradeCenterFragment extends BaseFragment{@Overridepublic View initView() {TextView tv = new TextView(mContext);tv.setText("交易中心");tv.setTextColor(Color.BLACK);tv.setTextSize(30);return tv;}}

主页的底部的图标


    <!-- 主页的底部的图标 -->    <style name="main_radiobutton_icon">        <item name="android:background">#00000000</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:gravity">center_horizontal</item>        <item name="android:textSize">12sp</item>        <item name="android:textColor">@color/rb_text_selector</item>        <item name="android:button">@null</item>    </style>





3 0
原创粉丝点击