您的位置:首页 > 移动开发 > Android开发

【问题记录】使用fregment实现底部菜单,旋转屏幕时,fregment会增加

2017-09-24 23:11 471 查看
【效果描述】

一个Acticity,4个fregment,每个fregment都有一个布局文件。

Acticity的布局文件是a_activity.xml

fregment的布局文件分别是a_fregment.xml , b_fregment.xml , c_fregment.xml , d_fregment.xml

A.xml中设置一个底部菜单,RadioGroup ,其中有RadioButton四个,底部菜单前面是一个SrollView,用于放置fregment。

想实现的效果是,每点一个底部菜单项,SrollView内对应地显示其对应的fregment布局。

【实现方式】

在Acticity.java文件中的分别设置四个RadioButton的setOnCheckedChangeListener事件,

如下,其中一个RadioButton的checkedChange事件的监听

package com.daemon.min.kaoyanchushi;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.EditText;
import android.widget.TextView;
import android.view.inputmethod.EditorInfo;
import android.content.res.Configuration;

public class MainActivity extends FragmentActivity {
private RadioButton HomeRadioButton;
private RadioButton newsRadioButton;
private RadioButton mineRadioButton;
private RadioButton studyRadioButton;
private HomeFragment homeFragment;
private NewsFragment newsFragment;
private StudyFragment studyFragment;
private MineFragment mineFragment;
private EditText mEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

HomeRadioButton =(RadioButton)findViewById(R.id.home_rbtn);
newsRadioButton =(RadioButton)findViewById(R.id.news_rbtn);
mineRadioButton =(RadioButton)findViewById(R.id.mine_rbtn);
studyRadioButton =(RadioButton)findViewById(R.id.study_rbtn);
mEditText =(EditText)findViewById(R.id.bar_edit);

homeFragment = new HomeFragment();
newsFragment = new NewsFragment();
studyFragment=new StudyFragment();
mineFragment=new MineFragment();

//初始化主页,默认选中首页
HomeRadioButton.setChecked(true);
HomeRadioButton.setTextColor(getResources().getColor(R.color.colorWhite));
getSupportFragmentManager().beginTransaction().add(R.id.home_content, homeFragment).commit();
System.out.println("主页初始化");

//监听主页被选择事件
HomeRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
FragmentTransaction f=getSupportFragmentManager().beginTransaction();
if(isChecked){
HomeRadioButton.setTextColor(getResources().getColor(R.color.colorWhite));
f.add(R.id.home_content,homeFragment);
System.out.println("主页create");
}else{
HomeRadioButton.setTextColor(getResources().getColor(R.color.colorText));
f.remove(homeFragment);
System.out.println("主页remove");
}
f.commit();
}
});

//监听发现按钮被选择事件
newsRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
FragmentTransaction f=getSupportFragmentManager().beginTransaction();
if(isChecked){
newsRadioButton.setTextColor(getResources().getColor(R.color.colorWhite));
f.add(R.id.home_content,newsFragment);
System.out.println("发现create");
}else{
newsRadioButton.setTextColor(getResources().getColor(R.color.colorText));
f.remove(newsFragment);
System.out.println("发现remove");
}
f.commit();
}
});

//监听我的被选择事件
mineRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
FragmentTransaction f=getSupportFragmentManager().beginTransaction();
if(isChecked){
mineRadioButton.setTextColor(getResources().getColor(R.color.colorWhite));
f.add(R.id.home_content,mineFragment);
System.out.println("我的create");
}else{
mineRadioButton.setTextColor(getResources().getColor(R.color.colorText));
f.remove(mineFragment);
System.out.println("我的remove");
}
f.commit();
}
});

//监听学习被选择事件
studyRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
FragmentTransaction f=getSupportFragmentManager().beginTransaction();
if(isChecked){
studyRadioButton.setTextColor(getResources().getColor(R.color.colorWhite));
f.add(R.id.home_content,studyFragment);
System.out.println("学习create");
}else{
studyRadioButton.setTextColor(getResources().getColor(R.color.colorText));
f.remove(studyFragment);
System.out.println("学习remove");
}
f.commit();
}
});

}

/**
* 屏幕旋转时调用此方法
*/
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){
System.out.println("竖屏");
}
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
System.out.println("横屏");
}
}

}

我试过,将
FragmentTransaction f 定义为全局变量,但如果每个事件里面都commit一次又是不允许的,所以在每个事件里面都new 一个FragmentTransaction出来

【问题探究】

(1)旋转屏幕的时候,不知为何会增加一个新的fregment界面,参考文章和其评论,进行下面的操作:

1.在配置文件里面的Activity属性中添加,android:configChanges="orientation|screenSize";

此时监听横屏竖屏的函数可以用,此时旋转不会增加新的fregment界面了。

控制台输出如下图:



可见,旋转的时候,监听了该Activity的旋转,而且,不会重新执行Activity的onCreate。

2. 不在配置文件里面添加android:configChanges="orientation|screenSize";



这里一共旋转了3次,可见执行了三次Activity的onCreate,有三个fregment对象,都赋值给全局变量HomeFragement. 为什么会有三个fragement界面出来,还是想不通。

(2)不旋转屏幕,能实现效果。我不是很理解,一开始添加的home_fregment,为什么在我点击选择其他的RadioButton的时候,会移除掉这个初试化的home_fregment界面。我在别的RadioButton的setOnCheckedChangeListener事件中移除的所用的FragmentTransaction对象(假设这个对象是fx)只是一个局部变量,fx.remove会使得前面出现的fregment被移除。

后来想,是我没理解透下面的点:

1、setOnCheckedChangeListener是checked状态改变就会触发。

2、FragmentTransaction对象应该理解成一个工具,把fragment对象添加,移除的工具。我混淆了它和fragment对象。也不是某个FragmentTransaction对象添加了某个fragment对象,别的fragmentTransaction对象就不能操作该fragment对象。

(3)setOnCheckedChangeListener的unchecked没有remove语句,则在第二次触发该RadioButton的checkedChange的checked状态,重复执行add语句,则会奔溃。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android
相关文章推荐