如何在android中实现在fragment内显示和隐藏fragment

发布于 2024-12-20 08:28:19 字数 2457 浏览 1 评论 0原文

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

    } 



}

谢谢

How to implement show and hide fragment inside fragment in Android? I have added two fragment inside activity. One fragment containing menu and one fragment contain sub menu. I have lot of button in menu fragment like home, idea, etc. If i click idea button. I have to show sub menu. If I again click idea button, I have to hide the sub menu. Can anybody provide example, or how to access one view fragment in another fragment?

this is my layout main

?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>

In My fragment

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

    } 



}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

请远离我 2024-12-27 08:28:19

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

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

You could try get framelayout or fragment by id and change its visibility

View frag = findViewById(R.id.my_fragment);
frag.setVisibility(View.VISIBLE);
毁我热情 2024-12-27 08:28:19

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

  • 您真的不想让 FragmentManager 和 FragmentTransactions 发生在片段内部,而不具有强制转换或对您的 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
}}

唯一需要考虑的部分是何时调用 needHide(),您可能会这样做在 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);
}

Considering this question has over 2K .. an answer may still help new readers so here it goes:

  • You don't really want to have FragmentManager and FragmentTransactions happening inside fragments not to have Casts nor potential harmful references to your Activity(s)

So what I do and works just fine is set an interface to the Fragment and give a method, say needsHide():

public class MyFrag extends Fragment {

public interface MyFragInterface {

    public void needsHide();
}

Then implement it on your 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
}}

The only part that requires thought is when to call needsHide(), this you might do in your Fragment's onViewCreated, since you are sure that it's not too early for your MainActivity to commit transactions. If you place it onCreate() it may not work depending on what you do with oter fragments:

 @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);
}
春夜浅 2024-12-27 08:28:19

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

然后从点击事件中的片段中获取“parent|”活动,投射它,然后调用您创建的方法。

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

Simply, create a public method in your "parent" activity. which hides the fragment.

Then from within the fragment in your click event get the "parent|' activity, cast it and then call the method you created.

    ((ParentActitity)getActivity()).hideFragment();
属性 2024-12-27 08:28:19

您需要使用接口与父活动进行通信。

看看Vogella的教程,“3.4.应用程序与Fragments的通信”。这是链接

You need to use an Interface to communicate with your parent Activity.

Take a look on Vogella's tutorial, "3.4. Application communication with Fragments". Here is the link

守护在此方 2024-12-27 08:28:19

方法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);   
    }

} 

method hide():Hides an existing fragment. This is only relevant for fragments whose views have been added to a container, as this will cause the view to be hidden.

your code :

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

} 
离笑几人歌 2024-12-27 08:28:19

下面的代码对我有用..

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

Below code worked for me..

View frag = findViewById(R.id.fragment);
frag.setVisibility(View.GONE);//Or View.INVISBLE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文