gpt4 book ai didi

java - fragment 未按预期显示/隐藏

转载 作者:行者123 更新时间:2023-12-01 04:37:41 25 4
gpt4 key购买 nike

所以我希望隐藏一个 fragment ,同时按下按钮显示另一个 fragment 。由于我的应用程序的复杂性,我无法在这里使用 tabHost,因此我制作了两个按钮,它们将充当选项卡,显示和隐藏两个 fragment 。其中一个按钮可以工作,但另一个按钮隐藏当前 fragment ,但无法显示另一个按钮。有时,此按钮会等待一秒钟左右,然后显示刚刚隐藏的 fragment 的一部分。知道发生了什么吗?

这是我的 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>

这是我的 fragment 中的代码

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 中使用两个 FrameLayout 来存储 fragment 有关。我不确定,但找不到其他人遇到这个特定问题。

最佳答案

您的 xml 未正确设置。如果指定android:layout_height="match_parent"对于 fragment A,它将占据垂直方向的所有空间。然后 fragment 2 向下/离开屏幕。您可以通过在FragmentA中指定wrap_content或设置 android:layout_above="@id/dynamic_fragment_B" 来设置它在 fragment A 中。

<?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"
android:layout_above="@id/dynamic_fragment_B"
/>

<FrameLayout
android:id="@+id/dynamic_fragment_B"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</LinearLayout>

关于java - fragment 未按预期显示/隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17100198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com