android - fragment 隐藏在 Android 中不起作用

标签 android android-activity android-fragments fragment android-fragmentactivity

我的要求是,我有一个主要 Activity ,我想在单独的 fragment 中显示图像和文本而不是进度条。 我有一个单独的 fragment ,我想在用户单击 MainActivity 中的按钮时显示该 fragment 。

我的问题是,当我启动应用程序时,默认情况下会显示 fragment 。我希望仅当用户单击按钮时才显示 fragment 。当我启动应用程序时,我希望显示 Activity 。

我有一个如下的activity_main xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background" >

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >

            <TableLayout
                android:id="@+id/tableLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="*" >

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dip" >

                    <TextView
                        android:id="@+id/text1"
                        style="@style/textForLabel"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dip"
                        android:text="@string/text1" />

                    <Spinner
                        android:id="@+id/spinner1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="20dip"
                        android:layout_weight="1"
                        android:drawSelectorOnTop="true"
                        android:prompt="@string/spinner1"
                        android:spinnerMode="dialog" >
                    </Spinner>
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/text2"
                        style="@style/textForLabel"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dip"
                        android:text="@string/label2" />

                    <Spinner
                        android:id="@+id/spinner2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="20dip"
                        android:layout_span="6"
                        android:layout_weight="1"
                        android:drawSelectorOnTop="true"
                        android:prompt="@string/spinner2"
                        android:spinnerMode="dropdown" />
                </TableRow>
            </TableLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/text3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textColor="@color/red"
                    android:textSize="@dimen/text_medium"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <fragment
        android:id="@+id/progress_bar_fragment"
        android:name="com.test.pushnotificationeclipse.ProgressBarFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

fragemnt_progress_bar.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/NoActionBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_all" >

    <ImageView
        android:id="@+id/imageView_frag_pgbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text_frag_pgbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView_frag_pgbar"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="74dp"
        android:textColor="@color/basic_color"
        android:textSize="@dimen/text_medium_large" />

</RelativeLayout>

我的ProgressBarFragment.java如下

public class ProgressBarFragment extends Fragment {

    @InjectView(R.id.text_frag_pgbar)
    TextView textView;

    @InjectView(R.id.imageView_frag_pgbar)
    ImageView imageView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_progress_bar, container,
                false);
        ButterKnife.inject(this, view);

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}

我的MainActivity.java如下:当我将 true 传递给hideAndShowFragment() 时,应该隐藏 fragment ,当我传递 false 时应该显示 fragment 。

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        context = getApplicationContext();
        hideAndShowFragment(true);
    }

    public void hideAndShowFragment(boolean hide) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ProgressBarFragment pf = new ProgressBarFragment();
        if (hide) {
            ft.hide(pf).commit();
        } else {
            ft.show(pf).commit();
        }
    }
}

最佳答案

这里的问题是您的 hideAndShowFragment 方法。您正在创建 ProgressBarFragment 的新实例,但 FragmentTransaction hide 和 show 方法仅适用于附加 fragment 。

相反,您应该通过 id 获取已在 XML 中定义的 fragment 。

public void hideAndShowFragment(boolean hide) {
    FragmentManager fm = getFragmentManager();
    ProgressBarFragment pf = 
        (ProgressBarFragment) fm.findFragmentById(R.id.progress_bar_fragment);
    FragmentTransaction ft = fm.beginTransaction();
    if (hide) {
        ft.hide(pf).commit();
    } else {
        ft.show(pf).commit();
    }
}

此外,这一行涉及:

context = getApplicationContext();

您的Activity也是一个Context对象,因此除非您需要特定于应用程序的上下文,否则您通常希望使用this您的 Activity 中需要上下文。

关于android - fragment 隐藏在 Android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27905242/

相关文章:

android - 使用 phonegap 运行 android 命令时创建 android 子项目时出错

android - Android 中的 FFmpeg 命令以编程方式

java - fragment 未在家庭 Activity 中显示

android - 如何使用 Activity 和 Fragment 之间的接口(interface)双向发送数据

java - 由于奇怪的原因无法读取文件

安卓:DialogFragment.dismissInternal 处的 NullPointerException 处 DialogFragment.dismissAllow

android - RelativeLayout 不显示所有按钮

javascript - Google Play 商店安全警报说您的应用程序包含易受攻击的 JavaScript 库如何删除安全警告?

android - 收听 android 中光标的任何变化

android - 使用 3 个 fragment 创建 Activity