显示/隐藏片段并以编程方式更改可见性属性

7
这是一个由两个部分组成的问题。我的问题是一个3片段布局,其中第三个片段(FragmentC)在用户点击另一个片段中的按钮时动态添加。然后,在添加第三个片段后,第三个片段有一个按钮可以最大化/最小化它。
更新:请滚动到结尾查看解决方案
问题1:
我试图改变FrameLayout的可见性属性,该属性作为第三个片段(R.id.fragment_C)的容器。
代码的目的是生成另一个片段,最初具有包含android:visibility =“gone”的XML。然后,在点击按钮并添加片段后,可见性应该更改为VISIBLE。
我知道这已经被讨论过了,但是经过4个小时的尝试使其工作后,我决定问一下我做错了什么。
问题2:
在生成第三个片段之后,我有一个最小化/最大化按钮,该按钮应该隐藏前两个片段,并允许第三个片段填充屏幕。
问题在于使用.setVisibility(View.GONE)时,前两个Fragment的Views未被移除。这个问题以前也提到过,但我无法弄清楚为什么在我的代码中不起作用。
到目前为止的代码(如果太冗长,对不起,但我认为为了您们更好地理解,最好包含所有细节):

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:orientation="vertical"
    >

    <FrameLayout
        android:id="@+id/fragment_A"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="#CCCCCC" 
        >
    </FrameLayout>

    <FrameLayout
        android:id="@+id/fragment_B"
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:layout_below="@id/fragment_A"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:background="#B4B4B4"
        >
    </FrameLayout>

    <FrameLayout
        android:id="@+id/fragment_C"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/fragment_B"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp"
        android:background="#A3A3A3"
        android:visibility="gone"
        >
    </FrameLayout>

</RelativeLayout>

land/main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:paddingLeft="0dp"
    android:paddingRight="0dp" >

    <LinearLayout
        android:id="@+id/fragments_container"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:baselineAligned="false" >

        <FrameLayout
            android:id="@+id/fragment_A"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            android:background="#CCCCCC" >
        </FrameLayout>

        <FrameLayout
            android:id="@id/fragment_B"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            android:background="#B4B4B4"
             >
        </FrameLayout>
    </LinearLayout>

    <FrameLayout
        android:id="@+id/fragment_C"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/fragment_container"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp"
        android:background="#A3A3A3"
        android:visibility="gone" >
    </FrameLayout>

</RelativeLayout>

MainActivity.java

package com.example.android.fragments_proto.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.example.android.fragments_proto.R;
import com.example.android.fragments_proto.fragment.GMC_DateSelectionFragment;
import com.example.android.fragments_proto.fragment.GMC_ProdUnitSelectionFragment;

public class MainActivity extends FragmentActivity {

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

        setContentView(R.layout.main_activity);

        FragmentManager fm = getSupportFragmentManager();

        Fragment fragmentA = fm.findFragmentById(R.id.fragment_A);

        Fragment fragmentB = fm.findFragmentById(R.id.fragment_B);

        if (fragmentA == null) {

            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.fragment_A, new FragmentA());
            ft.commit();
        }

        if (fragmentB == null) {

            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.fragment_B, new FragmentB());
            ft.commit();
        }
    }
}

现在是第一个Fragment的XML和.java文件。

fragment_A.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:gravity="center_horizontal"
    >

    <DatePicker
        android:id="@+id/datePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

FragmentA.java

package com.example.android.fragments_proto.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.Toast;

import com.example.android.fragments_proto.R;

public class FragmentA extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_A, container, false);

        DatePicker datePicker = (DatePicker) view.findViewById(R.id.datePicker1);
        datePicker.setCalendarViewShown(true);
        datePicker.setSpinnersShown(false);            

        datePicker.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                 Activity activity = getActivity();

                    if (activity != null) {

                        Toast.makeText(activity, "You Touched ME!", Toast.LENGTH_SHORT).show();
                    }
            return false;
            }
        });

        return view;
    }
}

现在,包含按钮的Fragment的XML和.java文件被添加到了R.id.fragment_C中。

fragment_B.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1"
        >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
        </ListView>

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_height="wrap_content">

            <Button
                android:id="@+id/button"
                android:text="@string/btn_fragment"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
            />

    </LinearLayout>

</LinearLayout>

FragmentB.java

package com.example.android.fragments_proto.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.example.android.fragments_proto.R;

public class FragmentB extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragmentB, container, false);

        ListView listView = (ListView) view.findViewById(R.id.listView1);
        Button button = (Button) view.findViewById(R.id.button);

        String[] machines = new String[] { "MachineId-001", "MachineId-002", "MachineId-003", "MachineId-004", "MachineId-005", "MachineId-006", "MachineId-007", "MachineId-008"};

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_multichoice, machines));
        final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.fragment_C);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
               Activity activity = getActivity();

               if (activity != null) {
                   getFragmentManager().beginTransaction().replace(R.id.fragment_C, new FragmentC()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).addToBackStack(null).commit();
                   frameLayout.setVisibility(View.VISIBLE);
                }
            }

        });

        return view;
    }

}

XML和.java文件是用于添加片段的。

fragment_C.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_height="wrap_content">

    <Button
        android:id="@+id/maximize_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Maximize Me!" />

    </LinearLayout>

    <TextView 
        android:id="@+id/text_view"
        android:textIsSelectable="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FF33FF"
        />

</LinearLayout>

FragmentC.java

package com.example.android.fragments_proto.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
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.Button;
import android.widget.TextView;

import com.example.android.fragments_proto.R;

public class FragmentC extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_C, container, false);

        TextView textView = (TextView) view.findViewById(R.id.text_view);

            final Fragment fragmentA = getFragmentManager().findFragmentById(R.id.fragment_A);
            final Fragment fragmentB = getFragmentManager().findFragmentById(R.id.fragment_B);

            button.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    FragmentTransaction ft = getFragmentManager().beginTransaction();

                    if (fragmentA.isVisible() && fragmentB.isVisible()) {
                        ft.hide(fragmentA);
                        ft.hide(fragmentB);
                        fragmentA.getView().setVisibility(View.GONE);
                        fragmentB.getView().setVisibility(View.GONE);
                        button.setText("Minimize Me!");
                        ft.addToBackStack(null);
                    } else {
                        ft.show(fragmentA);
                        ft.show(fragmentB);
                        fragmentA.getView().setVisibility(View.VISIBLE);
                        fragmentB.getView().setVisibility(View.VISIBLE);
                        button.setText("Maximize Me!");
                        ft.addToBackStack(null);
                    }
                    ft.commit();
                }
            });     

        return view;

    }
}



Moesio发现问题并提供解决方案

问题:

我的错误在于我试图通过以下代码在 FragmentB.java 中寻找视图:

final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.fragment_C);

这一行返回了 null,因此当代码到达应该进行 .setVisibility() 的点时,应用程序将返回 nullPointerException。

FragmentC.java 发生了同样的情况(所以我的两个问题是相关的)。视图没有被删除,因为我的 findViewById 是 null!


解决方案:

只需使用 getActivity.findViewById(R.id.your_view) 寻找您的 View 即可。


我将你的代码粘贴到本地项目中,我认为我找到了“null”的原因。我已经编辑了我的答案,请往下阅读。 - Moesio
2个回答

5
在FragmentB中,您正在尝试获取一个不在您的contentView上的视图。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_b, container, false);

    // this is in fragment_b layout
    ListView listView = (ListView) view.findViewById(R.id.listView1);
    Button button = (Button) view.findViewById(R.id.button);

    /* ... */
    // ****************************************
    // this is NOT in fragment_b layout, which causes null
    // ****************************************
    final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.fragment_C);

    /* ... */
}

尝试:

    final FrameLayout frameLayout = (FrameLayout) getActivity().getWindow().findViewById(R.id.fragment_C);

在MainActivity中,R.id.fragment_C被充气并设置。

此外,在使用额外标志之前,我遇到了同样的问题。

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    final Fragment fragmentC = new FragmentC();
    fragmentTransaction.add(R.id.fragment_C, fragmentC);
    fragmentTransaction.commit();
    menuIsOn = false;

    final View fragmentCView = findViewById(R.id.fragment_C);

    final Button btn = (Button) findViewById(R.id.btn);
    btnPowers.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!menuIsOn) {
                fragmentCView.setVisibility(View.VISIBLE);
            } else {
                fragmentCView.setVisibility(View.INVISIBLE);
            }
            menuIsOn = !menuIsOn;
        }
    });

谢谢你的回答。很抱歉这么问,但是有点不清楚。我必须在按钮点击时添加第三个片段(在我的示例中,只要我不在XML中添加可见性属性就可以工作)。问题是,当我添加它并点击按钮(请参见FragmentB.java)以添加我的第三个片段时,我的可见性属性会生成java.lang.NullPointerException并且应用程序崩溃。你的答案如何解决这个问题? - sebster
我不理解的是为什么我的代码会生成NullPointerException异常。如果您查看我的FragmentB.java示例,我使用了与您相似的代码。我无法弄清楚它们之间的差异以及如何帮助我的FragmentB。 - sebster
什么是null?它发生在哪里? - Moesio
我认为这与我在嵌套视图上调用.setVisibility有关(请参见main_activity.xml中的FrameLayout)。不过不确定。 - sebster
调试时显示 FragmentB.java 中的 frameLayout 为空。有任何想法为什么? - sebster
显示剩余3条评论

0
创建片段实例并添加,而不是替换。
        FragA  fa= new FragA();
        FragB  fb= new FragB();
        FragC  fc= new FragB();
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragmnt_container, fa);
        fragmentTransaction.add(R.id.fragmnt_container, fb);
        fragmentTransaction.add(R.id.fragmnt_container, fc);
        fragmentTransaction.show(fa);
        fragmentTransaction.hide(fb);
        fragmentTransaction.hide(fc);
        fragmentTransaction.commit();

假设我们想根据我们的操作隐藏和显示不同的片段。
    fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.hide(fa);
    fragmentTransaction.show(fb);
    fragmentTransaction.hide(fc);
    fragmentTransaction.commit();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接