2013-03-12 4 views
0

저는 오늘 조각을 배우고 있습니다. 단추를 누르면 조각이 숨겨집니다. 그러나 조각을 보여 주려고해도 아무 일도 일어나지 않는데 왜? 나는이 자습서를 수행 한 중간에 나는 시도하고 조각이 나는 버튼을 http://www.vogella.com/articles/AndroidFragments/article.html조각이 숨기기 후에 표시되지 않습니까?

Button2를 조각 누르면 표시/사라지게하기로 결정

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.button2_fragment, 
      container, false); 
     Button button = (Button) view.findViewById(R.id.button2); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       ButtonFragment fragment = (ButtonFragment) getFragmentManager() 
         .findFragmentById(R.id.ButtonFragment); 
       if (fragment != null && fragment.isInLayout()) { 
       FragmentManager fragmentManager = getFragmentManager(); 
       FragmentTransaction transaction = fragmentManager.beginTransaction(); 
       transaction.hide(fragmentManager.findFragmentById(R.id.ButtonFragment)); 
       transaction.commit(); 
       } 
       else 
       { 
        FragmentManager fragmentManager = getFragmentManager(); 
        FragmentTransaction transaction = fragmentManager.beginTransaction(); 
        transaction.show(fragmentManager.findFragmentById(R.id.ButtonFragment)); 
        transaction.commit(); 
       }  

      } 
     }); 
     return view; 
     } 

내가 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="horizontal" 
    android:background="#123456" > 

    <fragment 
     android:id="@+id/ButtonFragment" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_marginTop="?android:attr/actionBarSize" 
     class="com.example.myfragment.ButtonFragment" ></fragment> 

    <fragment 
     android:id="@+id/TimeFragment" 
     android:layout_width="0dp" 
     android:layout_weight="2" 
     android:layout_height="match_parent" 
     class="com.example.myfragment.TimeFragment" > 
     <!-- Preview: [email protected]/details --> 
    </fragment> 



    <fragment 
     android:id="@+id/Button2Fragment" 
     android:layout_width="0dp" 
     android:layout_weight="3" 
     android:layout_height="match_parent" 
     class="com.example.myfragment.Button2Fragment" > 
     <!-- Preview: [email protected]/details --> 
    </fragment> 

</LinearLayout> 

코드는 내 button2 조각에 있었는데, 주 ​​활동에 뭔가 넣어야합니까?

package com.example.myfragment; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

public class MainActivity extends Activity implements ButtonFragment.OnItemSelectedListener{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    // if the wizard generated an onCreateOptionsMenu you can delete 
    // it, not needed for this tutorial 

    @Override 
    public void onRssItemSelected(String link) { 
    TimeFragment fragment = (TimeFragment) getFragmentManager() 
      .findFragmentById(R.id.TimeFragment); 
     if (fragment != null && fragment.isInLayout()) { 
      fragment.setText(link); 
     } 
    } 

} 

답변

0

제어는 항상 따라서이

if (fragment != null && fragment.isInLayout())

및 내부가는 것 같다, 조각은 항상 숨겨져 한 번 숨겨진, 표시되지 않습니다.

토글을 시도하면 모든 onClick() 이벤트가 토글됩니다. 그렇게하면 컨트롤은 ifelse 블록으로 번갈아 가며 따라서 조각을 표시하는 &을 숨 깁니다. isVisible() 대신 isInLayout()

+0

감사를 사용

시도는 것을 시도 할 것이다. 그러나 나는 if (! fragment.isInLayout()) 대신 else를 시도해 보았고 그 중 하나는 작동하지 않습니다. 또한 왜 숨기기 대신 제거를 사용하면 배킹 예외가 발생하는지 알아? – Paul

+0

내 업데이트를 확인하십시오. – SudoRahul

+0

isVisible로 변경해 주셔서 감사 드리며, .remove를 제외하고는 올바르게 작동하지만, 그것에 대해 또 다른 스레드를 만들 수 있습니다. – Paul

관련 문제