刚接触fragment,之前在书上看的时候,由于它当时是加上了背景颜色,所以在切换的时候我没有去注意到重叠的问题。然后最近在做一个项目的时候用到fragment,才发现了这个问题。
我用RadioGroup里的RadioButton来做切换
package com.moke.activity;
public class CJD_CardPackageActivity extends Activity implements OnCheckedChangeListener {
private RadioGroup radipGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.cjd_cardpackageactivity);
init();
}
private void init() {
radipGroup = (RadioGroup)findViewById(R.id.cjd_CardPackageActivity_rgp);
bindClick();
}
private void bindClick(){
radipGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
CJD_Fragment_CardPackageUnused unusedFragment = new CJD_Fragment_CardPackageUnused();
CJD_Fragment_CardPackageUsed usedFragment = new CJD_Fragment_CardPackageUsed();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (checkedId) {
//切换到未使用页面
case R.id.cjd_CardPackageActivity_rbtn_unused:
transaction.replace(R.id.cjd_CardPackageActivity_fl_switch, unusedFragment);
transaction.commit();
break;
//切换到使用页面
case R.id.cjd_CardPackageActivity_rbtn_used:
transaction.replace(R.id.cjd_CardPackageActivity_fl_switch, usedFragment);
transaction.commit();
break;
default:
break;
}
}
}
布局文件的代码cjd_cardpackageactivity.xml:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/cjd_CardPackageActivity_rgp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:id="@+id/cjd_CardPackageActivity_rbtn_unused"
style="@style/radioButtonInTop"
android:text="@string/unused"
android:checked="true"/>
android:id="@+id/cjd_CardPackageActivity_rbtn_used"
style="@style/radioButtonInTop"
android:text="@string/used"/>
android:id="@+id/cjd_CardPackageActivity_fl_switch"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.moke.fragment.CJD_Fragment_CardPackageUnused"/>
一开始“未使用”的界面:
切换到“已使用”界面
切换回“未使用”界面并向下活动:
这样就发现新的重叠在了原来的Fragment上,当然原来的Fragment是固定不动的,还保留着当时的位置。
这是修改后的代码,目前从效果上看已经没有了重叠的现象了,非常感谢“li21”!!
public class CJD_CardPackageActivity extends Activity implements OnCheckedChangeListener {
private RadioGroup radipGroup;
private FragmentTransaction transaction;
private CJD_Fragment_CardPackageUnused unusedFragment;
private CJD_Fragment_CardPackageUsed usedFragment;
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.cjd_cardpackageactivity);
init();
}
private void init() {
radipGroup = (RadioGroup)findViewById(R.id.cjd_CardPackageActivity_rgp);
unusedFragment = new CJD_Fragment_CardPackageUnused();
usedFragment = new CJD_Fragment_CardPackageUsed();
fragmentManager = getFragmentManager();
// unusedFragment = (CJD_Fragment_CardPackageUnused) getFragmentManager().findFragmentByTag("unused");
// usedFragment = (CJD_Fragment_CardPackageUsed) getFragmentManager().findFragmentByTag("used");
transaction = fragmentManager.beginTransaction();
transaction.add(R.id.cjd_CardPackageActivity_fl_switch, unusedFragment).commit();
bindClick();
}
private void bindClick(){
radipGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
//切换到未使用页面
case R.id.cjd_CardPackageActivity_rbtn_unused:
transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.cjd_CardPackageActivity_fl_switch, unusedFragment);
transaction.commit();
break;
//切换到使用页面
case R.id.cjd_CardPackageActivity_rbtn_used:
transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.cjd_CardPackageActivity_fl_switch, usedFragment);
transaction.commit();
break;
default:
break;
}
}
}
我把布局里的fragment删掉了,然后在一开始的时候把unusedFragment add上去。接下来的工作应该是我要去试一下fragment的切换过程中会不会重新加载这样的问题,找到了一些有关hide()和show()的东西。