1

So I wish to hide a fragment while showing another with the push of a button. Because of the complexities of my app, I can't use a tabHost here, so I made two buttons that will act as tabs that will show and hide two fragments. One of the buttons works, but the other button hides the current fragment, but fails to show the other. Sometime this button waits a second or so and then shows part of the fragment that was just hidden. Any idea on what's going on?

Here is the code in my 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>

Here is the code in my 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();

    }

}

Maybe this has something to do with the fact that I'm using two FrameLayouts in my XML to store my fragments. I'm not sure, but wasn't able to find anyone else with this particular problem.

4

2 回答 2

0

尝试从 android:layout_width 更改为 android:layout_weight 因为有时第一个 framelayout 会占用所有可用空间,所以您看不到第二个。

于 2013-06-14T04:50:03.387 回答
0

您的 xml 设置不正确。如果您android:layout_height="match_parent"为片段 A 指定,它将垂直占用所有空间。然后片段二会向下/离开屏幕。您可以通过在 FragmentA 中指定 wrap_content 或在 Fragment android:layout_above="@id/dynamic_fragment_B"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>
于 2014-07-16T11:05:26.690 回答