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

} 
4

1 回答 1

0

控制似乎总是在这个里面

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

因此,碎片总是被隐藏起来,一旦隐藏起来,就永远不会显示出来。

尝试有一个切换,它将不断切换每个onClick()事件。这样,控件将交替流向ifelse块,从而隐藏和显示片段。

尝试使用isVisible()而不是isInLayout()

于 2013-03-12T11:15:38.197 回答