显示/隐藏片段和编程方式更改visibility属性(Show/Hide Fragments and

2019-07-22 02:30发布

这是一个两部分的问题。 什么我有一个是3片段布局其中3'rd片段(FragmentC)加入动态当用户轻敲在另一个片段中发现的按钮。 然后,它增加了3'rd片段后有一个按钮最大化/最小化。

更新:双击自动滚屏在年底解决方案



问题1:

我试图改变充当3'rd片段(R.id.fragment_C)容器中的FrameLayout的可见性属性。

什么代码是应该做的是产生,原本具有包含机器人的XML另一个片段:知名度=“水涨船高”。 然后,按一个按钮就当片段,并将能见度想改变为可见。

我知道这之前已经覆盖,但4个小时的努力,使其工作后,我决定问我在做什么错。

问题2:

产生3'rd片段后,我有一个最小化/最大化按钮是应该隐藏第2个片段,并允许3'rd片段以填充屏幕。

问题是所述第一2个片段的视图中使用.setVisibility(View.GONE)时不会被删除。 这种情况以前也被覆盖为好,但我想不通为什么它不在我的代码工作。

到目前为止的代码(抱歉,如果它以冗长,但我认为这是更好地包括你们这些人的所有详细信息):

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>

土地/ 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();
        }
    }
}

现在,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;
    }
}

现在,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)与

最终的FrameLayout的FrameLayout =(FrameLayout里)view.findViewById(R.id.fragment_C);

这条线返回null,所以当代码到达了它应该做.setVisibility(),然后应用点。 将返回一个NullPointerException。

同样的事情发生了FragmentC.java(所以我的2个问题是有关)。 该意见未被删除,因为我findViewById为空!


解:

只要搜索你的浏览与getActivity .findViewById(R.id.your_view);

Answer 1:

在FragmentB您想获得一个观点是不是在你的内容查看

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);

而R.id.fragment_C膨大和设置好的上MainActivity。

此外,我有同样的问题,直到使用额外的标志

    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;
        }
    });


文章来源: Show/Hide Fragments and change the visibility attribute programmatically