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

标签 java android hide show

所以我希望隐藏一个 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/

相关文章:

javascript - 如何通过单击此选择器上的 anchor 按钮来隐藏 div

html - 如何将此表格和按钮放在图像旁边(图像右侧)

css - 可以隐藏第 5 个元素 w/css

java - Java中的声明和定义有什么区别?

java - java 中的抽象类 getter 和 setter

java - Spring - 如何调整 SoapEnvelopeLoggingInterceptor 比标准输出更正式地记录数据到日志文件

android - 辅助功能服务无法通过通知工作

android - Android 上的袖珍狮身人面像

java - 如何调用与 Scala 关键字同名的 Java 方法?

android - 使应用程序警报对话框看起来像 Play Services Location Enable 对话框?