隐藏后片段不会显示?

时间:2013-03-12 11:06:17

标签: java android android-fragments

我今天刚学习片段。我按一个按钮,它隐藏了一个片段。但是,如果我试图显示片段没有任何反应,为什么?我正在按照这个教程,在中间我决定尝试按下一个按钮时使片段消失/出现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);
        } 
  }

} 

1 个答案:

答案 0 :(得分:0)

控件似乎总是在这个

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

因此,一旦隐藏,片段总是隐藏起来并且永远不会显示。

尝试进行切换,这将在每个onClick()事件中继续切换。这样,控件将交替地流向ifelse块,从而隐藏&amp;显示片段。

尝试使用isVisible()代替isInLayout()