kum*_*mar 8 android android-fragments android-fragmentactivity
如何在Android中实现片段内显示和隐藏片段?我在活动中添加了两个片段.包含菜单和一个片段的一个片段包含子菜单.菜单片段中有很多按钮,如家,想法等.如果我点击想法按钮.我必须显示子菜单.如果我再次点击idea按钮,我必须隐藏子菜单.任何人都可以提供示例,或者如何访问另一个片段中的一个视图片段?
这是我的布局主要
?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>
Run Code Online (Sandbox Code Playgroud)
在我的片段中
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
您可以尝试通过 id 获取框架布局或片段并更改其可见性
View frag = findViewById(R.id.my_fragment);
frag.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)
考虑到这个问题已超过2K ..答案可能仍然有助于新读者所以在这里:
所以我做的和工作得很好就是设置Fragment的接口并给出一个方法,比如needsHide():
public class MyFrag extends Fragment {
public interface MyFragInterface {
public void needsHide();
}
Run Code Online (Sandbox Code Playgroud)
然后在您的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
}}
Run Code Online (Sandbox Code Playgroud)
需要思考的唯一部分是何时调用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);
}
Run Code Online (Sandbox Code Playgroud)
简而言之,在您的“父”活动中创建一个公共方法。它隐藏了片段。
然后从点击事件中的片段中获取“parent|” 活动,投射它,然后调用您创建的方法。
((ParentActitity)getActivity()).hideFragment();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35063 次 |
| 最近记录: |