2013-03-20 79 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

相關問題