そのため、ボタンを押すだけでフラグメントを非表示にし、別のフラグメントを表示したいと考えています。私のアプリは複雑なため、ここでは tabHost を使用できません。そのため、2 つのフラグメントを表示および非表示にするタブとして機能する 2 つのボタンを作成しました。ボタンの 1 つが機能しますが、もう 1 つのボタンは現在のフラグメントを非表示にしますが、他のボタンは表示できません。このボタンは 1 秒ほど待ってから、非表示にしたフラグメントの一部を表示することがあります。何が起こっているかについて何か考えはありますか?
ここに私の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" >
<LinearLayout
android:id="@+id/top_buttons"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView
android:id="@+id/text_personal_A"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:onClick="true"
android:text="@string/my_account"/>
<TextView
android:id="@+id/text_personal_B"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:onClick="true"
android:text="@string/my_messages"/>
</LinearLayout>
<FrameLayout
android:id="@+id/dynamic_fragment_A"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<FrameLayout
android:id="@+id/dynamic_fragment_B"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
これが私のフラグメントのコードです
package com.kofsoft.mobile.fragment;
import com.kofsoft.mobile.R;
import com.kofsoft.mobile.constants.Constants;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
public class PersonalCenterFragment extends Fragment implements OnClickListener{
private TextView accountTab, employeeTab;
AccountInformationFragment accountInfo;
EmployeeInformationFragment employeeInfo;
FragmentManager fragMan;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_personal, container, false);
accountInfo = new AccountInformationFragment();
employeeInfo = new EmployeeInformationFragment();
accountTab = (TextView) v.findViewById(R.id.text_personal_A);
accountTab.setOnClickListener(this);
employeeTab = (TextView) v.findViewById(R.id.text_personal_B);
employeeTab.setOnClickListener(this);
fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
fragTrans.replace(R.id.dynamic_fragment_A, accountInfo,
Constants.ACCOUNT_INFO_TAG);
fragTrans.replace(R.id.dynamic_fragment_B, employeeInfo,
Constants.EMPLOYEE_INFO_TAG);
fragTrans.hide(employeeInfo);
fragTrans.commit();
return v;
}
@Override
public void onClick(View tab) {
FragmentTransaction fragTrans = fragMan.beginTransaction();
if(tab.getId() == R.id.text_personal_A){
fragTrans.hide(employeeInfo);
fragTrans.show(accountInfo);
}else if(tab.getId() == R.id.text_personal_B){
fragTrans.hide(accountInfo);
fragTrans.show(employeeInfo);
}
fragTrans.commit();
}
}
おそらくこれは、XML で 2 つの FrameLayout を使用してフラグメントを格納しているという事実と関係があります。よくわかりませんが、この特定の問題を抱えている人を他に見つけることができませんでした。