簡體   English   中英

隱藏后不會顯示片段?

[英]fragment won't show after hide?

我今天只是在學習片段。 我按下一個按鈕,它隱藏了一個片段。 但是,如果我嘗試顯示該片段沒有任何反應,那為什么呢? 我遵循了本教程,在中間,我決定嘗試使片段消失/出現,當我按下按鈕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: layout=@layout/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: layout=@layout/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);
        } 
  }

} 

控件似乎總是在里面

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

因此,這些片段一旦被隱藏,就始終被隱藏而不顯示。

嘗試進行切換,該切換將始終在每個onClick()事件上進行切換。 這樣,控件將交替流至ifelse塊,從而隱藏和顯示片段。

嘗試使用isVisible()代替isInLayout()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM