RadioGroup结合RadioButton使用切换Fragment片段
界面布局activity_lan_qiu,代碼xml設置如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="vertical">
? ? <include layout="@layout/tool_bar"/>
? ? <LinearLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent"
? ? ? ? android:background="@color/white"
? ? ? ? android:orientation="vertical">
? ? ? ? <FrameLayout
? ? ? ? ? ? android:id="@+id/fragment_container"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:layout_weight="1"
? ? ? ? ? ? android:background="@color/white">
? ? ? ? </FrameLayout>
? ? ? ? <RadioGroup
? ? ? ? ? ? android:id="@+id/radioGroup"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="40dp"
? ? ? ? ? ? android:orientation="horizontal"
? ? ? ? ? ? android:background="@color/colorPrimary"
? ? ? ? ? ? >
? ? ? ? ? ? <RadioButton
? ? ? ? ? ? ? ? android:text="首頁"
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:id="@+id/btn_homepage"
? ? ? ? ? ? ? ? android:layout_weight="1"
? ? ? ? ? ? ? ? android:button="@null"
? ? ? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? ? ? android:textColor="@color/selector_font_color"
? ? ? ? ? ? ? ? android:textSize="18sp"
? ? ? ? ? ? ? ? />
? ? ? ? ? ? <RadioButton
? ? ? ? ? ? ? ? android:text="社區"
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:id="@+id/btn_message"
? ? ? ? ? ? ? ? android:layout_weight="1"
? ? ? ? ? ? ? ? android:button="@null"
? ? ? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? ? ? android:textColor="@color/selector_font_color"
? ? ? ? ? ? ? ? android:textSize="18sp"
? ? ? ? ? ? ? ? />
? ? ? ? ? ? <RadioButton
? ? ? ? ? ? ? ? android:text="運動"
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:id="@+id/btn_service"
? ? ? ? ? ? ? ? android:layout_weight="1"
? ? ? ? ? ? ? ? android:button="@null"
? ? ? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? ? ? android:textColor="@color/selector_font_color"
? ? ? ? ? ? ? ? android:textSize="18sp"
? ? ? ? ? ? ? ? />
? ? ? ? ? ? <RadioButton
? ? ? ? ? ? ? ? android:text="個人"
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:id="@+id/btn_my"
? ? ? ? ? ? ? ? android:layout_weight="1"
? ? ? ? ? ? ? ? android:button="@null"
? ? ? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? ? ? android:textColor="@color/selector_font_color"
? ? ? ? ? ? ? ? android:textSize="18sp"
? ? ? ? ? ? ? ? />
? ? ? ? </RadioGroup>
? ? </LinearLayout>
</LinearLayout>
android java 類LanQiuActivity 代碼如下:
public class LanQiuActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {
? ? private RadioGroup radioGroup;
? ? private RadioButton btn_homepage, btn_message, btn_service, btn_my;
? ? public static final String fragment1Tag = "fragment1";
? ? public static final String fragment2Tag = "fragment2";
? ? public static final String fragment3Tag = "fragment3";
? ? public static final String fragment4Tag = "fragment4";
? ? private AMapLocationClient locationClient = null;
? ? private AMapLocationClientOption locationOption = null;
? ? private TextView tvshare;
? ? private TextView iv_back;
? ? String city;
? ? public static boolean aa = true;
? ? private Toolbar toolbar;
? ? /**
? ? ?* 定位監聽
? ? ?*/
? ? AMapLocationListener locationListener = new AMapLocationListener() {
? ? ? ? @Override
? ? ? ? public void onLocationChanged(AMapLocation location) {
? ? ? ? ? ? if (null != location) {
? ? ? ? ? ? ? ? StringBuffer sb = new StringBuffer();
? ? ? ? ? ? ? ? if (location.getErrorCode() == 0) {
? ? ? ? ? ? ? ? ? ? //解析定位結果,
? ? ? ? ? ? ? ? ? ? city = location.getLatitude() + ":" + location.getLongitude();
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? sb.append("定位失敗" + "\n");
? ? ? ? ? ? ? ? ? ? sb.append("錯誤碼:" + location.getErrorCode() + "\n");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? Log.e("解析定位結果", city);
? ? ? ? ? ? ? ? iv_back.setText(location.getCity());
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? iv_back.setText("定位失敗");
? ? ? ? ? ? }
? ? ? ? ? ? if (aa) {
? ? ? ? ? ? ? ? XinzhiTianqiData();
? ? ? ? ? ? ? ? aa = false;
? ? ? ? ? ? }
? ? ? ? }
? ? };
? ? /**
? ? ?* 開始定位
? ? ?*
? ? ?* @author hongming.wang
? ? ?* @since 2.8.0
? ? ?*/
? ? private void startLocation() {
? ? ? ? //根據控件的選擇,重新設置定位參數
// ? ? ? ?resetOption();
? ? ? ? // 設置定位參數
? ? ? ? locationClient.setLocationOption(locationOption);
? ? ? ? // 啟動定位
? ? ? ? locationClient.startLocation();
? ? }
? ? /**
? ? ?* 默認的定位參數
? ? ?*
? ? ?* @author hongming.wang
? ? ?* @since 2.8.0
? ? ?*/
? ? private AMapLocationClientOption getDefaultOption() {
? ? ? ? AMapLocationClientOption mOption = new AMapLocationClientOption();
? ? ? ? mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可選,設置定位模式,可選的模式有高精度、僅設備、僅網絡。默認為高精度模式
? ? ? ? mOption.setGpsFirst(false);//可選,設置是否gps優先,只在高精度模式下有效。默認關閉
? ? ? ? mOption.setHttpTimeOut(30000);//可選,設置網絡請求超時時間。默認為30秒。在僅設備模式下無效
? ? ? ? mOption.setInterval(2000);//可選,設置定位間隔。默認為2秒
? ? ? ? mOption.setNeedAddress(true);//可選,設置是否返回逆地理地址信息。默認是true
? ? ? ? mOption.setOnceLocation(false);//可選,設置是否單次定位。默認是false
? ? ? ? mOption.setOnceLocationLatest(false);//可選,設置是否等待wifi刷新,默認為false.如果設置為true,會自動變為單次定位,持續定位時不要使用
? ? ? ? AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可選, 設置網絡請求的協議。可選HTTP或者HTTPS。默認為HTTP
? ? ? ? mOption.setSensorEnable(false);//可選,設置是否使用傳感器。默認是false
? ? ? ? mOption.setWifiScan(true); //可選,設置是否開啟wifi掃描。默認為true,如果設置為false會同時停止主動刷新,停止以后完全依賴于系統刷新,定位位置可能存在誤差
? ? ? ? mOption.setLocationCacheEnable(true); //可選,設置是否使用緩存定位,默認為true
? ? ? ? return mOption;
? ? }
? ? /**
? ? ?* 初始化定位
? ? ?*
? ? ?* @author hongming.wang
? ? ?* @since 2.8.0
? ? ?*/
? ? private void initLocation() {
? ? ? ? //初始化client
? ? ? ? locationClient = new AMapLocationClient(getApplicationContext());
? ? ? ? locationOption = getDefaultOption();
? ? ? ? //設置定位參數
? ? ? ? locationClient.setLocationOption(locationOption);
? ? ? ? // 設置定位監聽
? ? ? ? locationClient.setLocationListener(locationListener);
// ? ? ? ?XinzhiTianqiData();
? ? }
? ? private void XinzhiTianqiData() {
? ? ? ? RequestParams params = new RequestParams();
? ? ? ? params.put("location", city);
? ? ? ? Log.e("天氣XinzhiTianqiData", "天氣XinzhiTianqiData");
? ? ? ? AsyncHttpClientUtil.getInstance().get(Constants.XinzhiTianqi, params, new AsyncHttpResponseHandler() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
? ? ? ? ? ? ? ? String s = new String(responseBody);
// ? ? ? ? ? ? ? ?Log.e("天氣", ""+s);
? ? ? ? ? ? ? ? XinzhiModle xinzhiModle = GsonUtils.parseJSON(s, XinzhiModle.class);
? ? ? ? ? ? ? ? List<XinzhiModle.ResultsBean> results = xinzhiModle.getResults();
? ? ? ? ? ? ? ? String text = results.get(0).getNow().getText();
? ? ? ? ? ? ? ? tvshare.setText("天氣: " + text);
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? if (savedInstanceState == null) {
? ? ? ? ? ? FragmentManager fragmentManager = getSupportFragmentManager();
? ? ? ? ? ? Fragment fragment = new HomeFragment();
? ? ? ? ? ? fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment, fragment1Tag).commit();
? ? ? ? }
? ? ? ? setContentView(R.layout.activity_lan_qiu);
? ? ? ? toolbar = (Toolbar) findViewById(R.id.tool_bar);
? ? ? ? TextView tool_bar_title = (TextView)findViewById(R.id.tool_bar_title);
? ? ? ? tool_bar_title.setVisibility(View.GONE);
? ? ? ? tvshare = (TextView)findViewById(R.id.tv_share1);
? ? ? ? iv_back = (TextView)findViewById(R.id.iv_back1);
? ? ? ? iv_back.setVisibility(View.VISIBLE);
? ? ? ? tvshare.setVisibility(View.VISIBLE);
? ? ? ? iv_back.setTextSize(22);
? ? ? ? tvshare.setTextSize(22);
? ? ? ? radioGroup = (RadioGroup) this.findViewById(R.id.radioGroup);
? ? ? ? btn_homepage = (RadioButton) this.findViewById(R.id.btn_homepage);
? ? ? ? btn_homepage.setSelected(true);
? ? ? ? btn_message = (RadioButton) this.findViewById(R.id.btn_message);
? ? ? ? btn_service = (RadioButton) this.findViewById(R.id.btn_service);
? ? ? ? btn_my = (RadioButton) this.findViewById(R.id.btn_my);
? ? ? ? initLocation();
? ? ? ? startLocation();
? ? ? ? radioGroup.setOnCheckedChangeListener(this);
? ? }
? ? public static void start(Context context, SzBean szBean) {
? ? ? ? Intent starter = new Intent(context, MainActivity.class);
? ? ? ? starter.putExtra(ParamsKey.EXTRA_SZBEAN, szBean);
? ? ? ? context.startActivity(starter);
? ? }
//選中首頁界面RadioButton處理事件?
? ? private void HomePageSelect() {
? ? ? ? btn_homepage.setSelected(true);
? ? ? ? btn_message.setSelected(false);
? ? ? ? btn_service.setSelected(false);
? ? ? ? btn_my.setSelected(false);
? ? }
//選中信息界面RadioButton處理事件?
? ? private void MessageSelect() {
? ? ? ? btn_homepage.setSelected(false);
? ? ? ? btn_message.setSelected(true);
? ? ? ? btn_service.setSelected(false);
? ? ? ? btn_my.setSelected(false);
? ? }
//選中記錄RadioButton處理事件?
? ? private void RecordSelect() {
? ? ? ? btn_homepage.setSelected(false);
? ? ? ? btn_message.setSelected(false);
? ? ? ? btn_service.setSelected(true);
? ? ? ? btn_my.setSelected(false);
? ? }
//選中我的界面RadioButton處理事件?
? ? private void MySelect() {
? ? ? ? btn_homepage.setSelected(false);
? ? ? ? btn_message.setSelected(false);
? ? ? ? btn_service.setSelected(false);
? ? ? ? btn_my.setSelected(true);
? ? }
//在RadioGroup中切換改變選中RadioButton事件,處理跳轉到相應界面
? ? @Override
? ? public void onCheckedChanged(RadioGroup radioGroup, int i) {
? ? ? ? FragmentManager fm = getSupportFragmentManager();
? ? ? ? FragmentTransaction ft = fm.beginTransaction();
? ? ? ? Fragment fragment1 = fm.findFragmentByTag(fragment1Tag);
? ? ? ? Fragment fragment2 = fm.findFragmentByTag(fragment2Tag);
? ? ? ? Fragment fragment3 = fm.findFragmentByTag(fragment3Tag);
? ? ? ? Fragment fragment4 = fm.findFragmentByTag(fragment4Tag);
? ? ? ? if (fragment1 != null) {
? ? ? ? ? ? ft.hide(fragment1);
? ? ? ? }
? ? ? ? if (fragment2 != null) {
? ? ? ? ? ? ft.hide(fragment2);
? ? ? ? }
? ? ? ? if (fragment3 != null) {
? ? ? ? ? ? ft.hide(fragment3);
? ? ? ? }
? ? ? ? if (fragment4 != null) {
? ? ? ? ? ? ft.hide(fragment4);
? ? ? ? }
? ? ? ? switch (i) {
? ? ? ? ? ? case R.id.btn_homepage:
? ? ? ? ? ? ? ? toolbar.setVisibility(View.VISIBLE);
? ? ? ? ? ? ? ? if (fragment1 == null) {
? ? ? ? ? ? ? ? ? ? fragment1 = new HomeFragment();
? ? ? ? ? ? ? ? ? ? ft.add(R.id.fragment_container, fragment1, fragment1Tag);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ft.show(fragment1);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? HomePageSelect();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn_message:
? ? ? ? ? ? ? ? toolbar.setVisibility(View.VISIBLE);
? ? ? ? ? ? ? ? if (fragment2 == null) {
? ? ? ? ? ? ? ? ? ? fragment2 = new BBsFragment();
? ? ? ? ? ? ? ? ? ? ft.add(R.id.fragment_container, fragment2, fragment2Tag);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ft.show(fragment2);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? MessageSelect();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn_service:
? ? ? ? ? ? ? ? toolbar.setVisibility(View.VISIBLE);
? ? ? ? ? ? ? ? if (fragment3 == null) {
? ? ? ? ? ? ? ? ? ? fragment3 = new SportFragment();
? ? ? ? ? ? ? ? ? ? ft.add(R.id.fragment_container, fragment3, fragment3Tag);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ft.show(fragment3);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? RecordSelect();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn_my:
? ? ? ? ? ? ? ? toolbar.setVisibility(View.GONE);
? ? ? ? ? ? ? ? if (fragment4 == null) {
? ? ? ? ? ? ? ? ? ? fragment4 = new PersonalCenteFragment();
? ? ? ? ? ? ? ? ? ? ft.add(R.id.fragment_container, fragment4, fragment4Tag);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ft.show(fragment4);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? MySelect();
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? ft.commit();
? ? }
? ? //app退到后臺,我們去玩其他的app,過一段時間回來,這個時候我們的app已經被銷毀,我們按多任務鍵切換回來,發現界面上多個Fragment出現了重疊的情況,這是因為多個Fragment同時顯示了,出現了重疊的情況,解決的辦法如下:重寫Activity的onRestoreInstanceState方法
? ? @Override
? ? protected void onRestoreInstanceState(Bundle savedInstanceState) {
? ? ? ? super.onRestoreInstanceState(savedInstanceState);
? ? ? ? for (int i = 0; i < radioGroup.getChildCount(); i++) {
? ? ? ? ? ? RadioButton mTab = (RadioButton) radioGroup.getChildAt(i);
? ? ? ? ? ? FragmentManager fm = getSupportFragmentManager();
? ? ? ? ? ? Fragment fragment = fm.findFragmentByTag((String) mTab.getTag());
? ? ? ? ? ? FragmentTransaction ft = fm.beginTransaction();
? ? ? ? ? ? if (fragment != null) {
? ? ? ? ? ? ? ? if (!mTab.isChecked()) {
? ? ? ? ? ? ? ? ? ? ft.hide(fragment);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? ft.commit();
? ? ? ? }
? ? }
? ? private long mExitTime;
? ? //按back提示再按一次退出
? ? public boolean onKeyDown(int keyCode, KeyEvent event) {
? ? ? ? if (keyCode == KeyEvent.KEYCODE_BACK) {
? ? ? ? ? ? if ((System.currentTimeMillis() - mExitTime) > 2000) {
? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "再按一次退出", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? mExitTime = System.currentTimeMillis();
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? finish();
? ? ? ? ? ? ? ? System.exit(0);
? ? ? ? ? ? }
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? return super.onKeyDown(keyCode, event);
? ? }
}
轉載于:https://blog.51cto.com/1206995290qq/1945137
總結
以上是生活随笔為你收集整理的RadioGroup结合RadioButton使用切换Fragment片段的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ElasticSearch-------
- 下一篇: 和Facebook竞争,社交平台Mico