2013-03-20 80 views
6

这是一个2部分问题。我所拥有的是3片段布局,其中当用户点击另一个片段中找到的按钮时,动态添加3'rd片段(FragmentC)。然后,它添加后,第三个片段有一个按钮来最大化/最小化它。以编程方式显示/隐藏碎片并更改visibility属性

UPDATE:双击自动滚屏在年底SOLUTION



问题1:

我试图改变充当容器的FrameLayout的可见性属性对于第3个片段(R.id.fragment_C)。

代码应该做的是生成另一个片段,最初有一个包含android:visibility =“gone”的XML。然后,当点击一个按钮时添加片段,并且可见性被设想变为可见。

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

问题2:

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

问题是使用.setVisibility(View.GONE)时不会删除前2个碎片的视图。之前也已经介绍过,但我无法弄清楚为什么它在我的代码中不起作用。

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

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

现在是第一个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; 
    } 
} 

现在的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

问题:

我的错误是,我试图找到

最终的FrameLayout FrameLayout里=(FrameLayout里)view.findViewById(R.id一个视图(FragmentB.java)。 fragment_C);

这行代码返回null,所以当代码达到它应该执行.setVisibility()然后应用程序。会返回一个nullPointerException。

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


SOLUTION:

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

+0

我粘贴代码在本地的项目,我想我找到了“空”的原因。我编辑过可能会回答。参见下文。 – Moesio 2013-03-20 17:54:55

回答

4

在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; 
     } 
    }); 
+0

感谢您的回答。 我很抱歉问这个,但有点不清楚。 我必须在点击按钮上添加3'rd片段(在我的示例中,只要我不在XML中添加可见性属性,就会工作)。问题是,当我添加它,然后点击按钮(请参阅FragmentB.java)应该添加我的第3个片段时,我的可见性属性会生成java.lang.NullPointerException,并且应用程序崩溃。 你的答案应该如何解决这个问题? – sebster 2013-03-20 11:59:03

+0

我不明白的是为什么我的代码生成一个NullPointerException。如果你看看我的FragmentB.java例子,我使用一个与你的代码非常相似的代码。无法弄清楚差异在哪里以及它可能如何帮助我FragmentB – sebster 2013-03-20 12:03:38

+0

什么是空?它发生在哪里? – Moesio 2013-03-20 15:28:45

相关问题