如何实现显示和隐藏在里面的android片段片段 [英] How to implement show and hide fragment inside fragment in android

查看:193
本文介绍了如何实现显示和隐藏在里面的android片段片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现显示和隐藏内部片段片段的Andr​​oid?我已经加入活动中的两个片段。一个片段包含菜单和一个片段包含子菜单。我在家里一样,观念等菜单片段很多按钮,如果我点击想法按钮。我必须表现出子菜单。如果我再次点击按钮的想法,我要隐藏子菜单。有谁能够提供例如,或者如何在另一个片段访问一个视图片段?

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?

这是我的布局主要

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

    } 



}

感谢

推荐答案

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

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


  • 您真的不希望有FragmentManager和FragmentTransactions内发生的片段不具有强制类型转换,也可能产生的有害引用您的活动(S)

所以,我做什么和工作得很好,设置接口的片段,并给予的方法,说needsHide():

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

这是需要思考的唯一的部分是,当调用needsHide(),这个你可以做你的碎片的onViewCreated,因为你确信这不是太早您MainActivity提交事务。如果你把它的onCreate(),它可能不取决于你做什么oter片段工作:

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

这篇关于如何实现显示和隐藏在里面的android片段片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆