【问题标题】:How to implement show and hide fragment inside fragment in android如何在android中实现片段内的显示和隐藏片段
【发布时间】:2011-12-08 14:06:48
【问题描述】:

如何在 Android 中实现显示和隐藏 Fragment 内的 Fragment?我在活动中添加了两个片段。一个片段包含菜单,一个片段包含子菜单。我在菜单片段中有很多按钮,例如主页,想法等。如果我单击想法按钮。我必须显示子菜单。如果我再次点击想法按钮,我必须隐藏子菜单。谁能提供示例,或者如何访问另一个片段中的一个视图片段?

这是我的主要布局

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<fragment class="com.gcm.fragment.CommonFragment"
            android:id="@+id/the_frag"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />  
 <fragment class="com.gcm.fragment.SubFragment"
            android:id="@+id/the_frag1"
            android:layout_marginTop="130dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />             


</LinearLayout>

在我的片段中

package com.gcm.fragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class CommonFragment extends Fragment implements OnClickListener {
    TextView txtIhaveIdea=null;
  boolean menuVisible=false;
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { 
        ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.collapsed_menu2, container, false); 

        txtIhaveIdea=(TextView)layout.findViewById(R.id.txtIhaveAnIdea);
        txtIhaveIdea.setOnClickListener(this);

        return layout; 
        }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(!menuVisible)
        {
        FragmentManager fm = getFragmentManager(); 
        FragmentTransaction ft = fm.beginTransaction(); 
        fm.beginTransaction(); 
        Fragment fragOne = new SubFragment(); 
        ft.show(fragOne);
        }
        else
        {
            FragmentManager fm = getFragmentManager(); 
            FragmentTransaction ft = fm.beginTransaction(); 

            fm.beginTransaction(); 
            Fragment fragOne = new SubFragment(); 
            ft.hide(fragOne);   
        }

    } 



}

谢谢

【问题讨论】:

标签: android android-fragments android-fragmentactivity


【解决方案1】:

您可以尝试通过 id 获取框架布局或片段并更改其可见性

View frag = findViewById(R.id.my_fragment);
frag.setVisibility(View.VISIBLE);

【讨论】:

    【解决方案2】:

    考虑到这个问题有超过 2K .. 一个答案可能仍然对新读者有所帮助,所以就这样吧:

    • 您真的不希望 FragmentManager 和 FragmentTransactions 在 Fragment 内部发生,而不是有 Cast 或对您的 Activity 的潜在有害引用

    所以我所做的和工作正常的就是为 Fragment 设置一个接口并给出一个方法,比如 needHide():

    public class MyFrag extends Fragment {
    
    public interface MyFragInterface {
    
        public void needsHide();
    }
    

    然后在你的Activity上实现它:

    public class MainActivity extends FragmentActivity implements MyFrag.MyFragInterface {
    public void needsHide() {
    
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //find the fragment by View or Tag
        MyFrag myFrag = (MyFrag)fragmentManager.findFragmentByTag(SOME_TAG);
        fragmentTransaction.hide(myFrag);
        fragmentTransaction.commit();
            //do more if you must
    }}
    

    唯一需要考虑的部分是何时调用 needsHide(),这可以在 Fragment 的 onViewCreated 中执行,因为您确信 MainActivity 提交事务还为时过早。如果您将它放在 onCreate() 上,它可能无法正常工作,具体取决于您对其他片段所做的操作:

     @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    
        // Making sure Main activity implemented interface
        try {
            if (USE_A_CONDITION) {
                ((MyFragInterface)this.getActivity()).needsHide();
            }
        } catch (ClassCastException e) {
            throw new ClassCastException("Calling activity must implement MyFragInterface");
        }
        super.onViewCreated(view, savedInstanceState);
    }
    

    【讨论】:

      【解决方案3】:

      简单地说,在您的“父”活动中创建一个公共方法。它隐藏了片段。

      然后从点击事件的片段中获取“父|”活动,将其强制转换,然后调用您创建的方法。

          ((ParentActitity)getActivity()).hideFragment();
      

      【讨论】:

        【解决方案4】:

        您需要使用接口与您的父 Activity 进行通信。

        查看 Vogella 的教程“3.4. 使用 Fragments 进行应用程序通信”。这是link

        【讨论】:

          【解决方案5】:

          方法hide():隐藏现有片段。这仅与视图已添加到容器中的片段相关,因为这会导致视图被隐藏。

          你的代码:

          @Override
          public void onClick(View v) {
              if(!menuVisible)
              {
              FragmentManager fm = getFragmentManager(); 
              FragmentTransaction ft = fm.beginTransaction(); 
              fm.beginTransaction(); 
              Fragment fragOne = new SubFragment(); 
              ft.show(fragOne);
              }
              else
              {
                  FragmentManager fm = getFragmentManager(); 
                  FragmentTransaction ft = fm.beginTransaction(); 
          
                  fm.beginTransaction(); 
                  // it's wrong , you just hide the fragment that not added to  FragmentTransaction
                  Fragment fragOne = new SubFragment(); 
                  ft.hide(fragOne);   
              }
          
          } 
          

          【讨论】:

            【解决方案6】:

            下面的代码对我有用..

            View frag = findViewById(R.id.fragment);
            frag.setVisibility(View.GONE);//Or View.INVISBLE
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-12-30
              • 1970-01-01
              • 1970-01-01
              • 2014-09-06
              • 2011-10-27
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多