【问题标题】:fragment won't show after hide?隐藏后片段不会显示?
【发布时间】:2013-03-12 19:21:56
【问题描述】:

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

} 

【问题讨论】:

    标签: java android android-fragments


    【解决方案1】:

    控件似乎总是在这个里面

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

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

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

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

    【讨论】:

    • 谢谢,会试试的。但是,我也尝试过 if (!fragment.isInLayout()) 而不是 else,但这也不起作用。你也知道为什么如果我使用 remove 而不是 hide 会有一个 backstack 异常吗?
    • 感谢更改为 isVisible 使事情正常工作,除了 .remove 之外,但我会再写一个关于它的线程。
    • 还有一个问题,你说的切换是什么意思,我只有一个 if 然后一个 else if。
    • 切换我的意思是boolean,它会在每个onClick() 上不断地从true 变为false,以及false 到true
    猜你喜欢
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2022-01-06
    • 2011-10-27
    • 2012-12-30
    • 1970-01-01
    相关资源
    最近更新 更多