2011-12-08 131 views
7

如何在Android中实现显示和隐藏片段片段?我在活动中添加了两个片段。一个包含菜单和一个片段的片段包含子菜单。我在菜单片段中有很多按钮,比如家,想法等。如果我点击创意按钮。我必须显示子菜单。如果我再次点击创意按钮,我必须隐藏子菜单。任何人都可以提供示例,或者如何访问另一个片段中的一个视图片段?如何在Android中实现显示和隐藏片段片段

这是我的布局主要

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

    } 



} 

感谢

+0

参考参考代码片段显示/隐藏在Android网站http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentHideShow.html给出 – potter

+0

@kumar你做到了吗? – AndroidOptimist

回答

2

简单,创造你的 “父母” 活动的公共方法。它隐藏了片段。

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

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

你需要使用一个接口与父活动进行沟通。

就以Vogella的教程,“3.4一看。与碎片”应用的通信。这里是link

2

你可以尝试得到的FrameLayout或ID片段,并改变其知名度

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

考虑这个问题已经在2K ..答案仍可能帮助新读者如此这里有云:

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

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

public class MyFrag extends Fragment { 

public interface MyFragInterface { 

    public void needsHide(); 
} 

然后实现它在你的活动:

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(),它可以不依赖于你oter片段做什么工作:

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

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

} 
-3

下面的代码为我工作..

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