是否有可能删除的片段,而不由其他片段代替它? [英] Is it possible to remove a fragment without replacing it by another fragment?

查看:265
本文介绍了是否有可能删除的片段,而不由其他片段代替它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用抽屉的内容框架与NavigationDrawer的ActionBarActivity和一个ListView。

 <?XML版本=1.0编码=UTF-8&GT?;

 <的FrameLayout
    机器人:ID =@ + ID / content_frame
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =match_parent
    机器人:方向=垂直>    <的LinearLayout
        机器人:ID =@ + ID / lyt_list
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_weight =1
        机器人:方向=横向>        < ListView控件
            机器人:ID =@ + ID / list_notes
            机器人:layout_width =0dip
            机器人:layout_height =FILL_PARENT
            机器人:layout_weight =1>
        < /&的ListView GT;
    < / LinearLayout中>
< /&的FrameLayout GT;<的FrameLayout
    机器人:ID =@ + ID / drawer_frame
    机器人:layout_width =240dp
    机器人:layout_height =FILL_PARENT
    机器人:layout_gravity =开始
    机器人:layout_weight =1>    < ListView控件
        机器人:ID =@ + ID / left_drawer
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:背景=@彩色/深灰
        机器人:choiceMode =singleChoice/>
     < /&的FrameLayout GT;

我开这样从抽屉的项目之一preferenceFragment:

  FragmentTransaction交易= getFragmentManager()调用BeginTransaction()。
                prefsFragment preFS =新的prefsFragment();
                transaction.add(R.id.content_frame,preFS);
                transaction.addToBackStack(NULL);
                器transaction.commit();

这是我的prefsFragment:

 公共类prefsFragment延伸片段{@覆盖
公共无效onActivityCreated(捆绑savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    getActivity()。getFragmentManager()调用BeginTransaction()
            .replace(R.id.content_frame,新的prefsFr())
            。承诺();
}
公共类prefsFr扩展$ P $ {pferenceFragment
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);
        加preferencesFromResource(R.xml preferences);
      ...}
}}

但我不能删除此片段。
当我切换到另一个抽屉项; preferenceFragment仍然ListView控件的顶部content_frame与tranceparent背景。

是否有可能在ActionBarActivity删除片段而不用另一个片段代替它?
如果不是这样如何使用preferenceFragment为API> 11?这是真的,唯一的办法就是互相替代的片段?


解决方案

  

是否有可能在ActionBarActivity删除片段而不用另一个片段代替它?


是的,你可以使用<一个href=\"http://developer.android.com/reference/android/app/FragmentTransaction.html#remove(android.app.Fragment)\"相对=nofollow> FragmentTransaction#删除(片段)

潜在的问题不过是你显示preferenceFragment与 transaction.add(R.id.content_frame,preFS); 。如果你只想preferenceFragment显示wihthout它背后的ListView控件,你应该使用替换()代替。

I use an ActionBarActivity with NavigationDrawer and a ListView in the content frame of Drawer.

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/lyt_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/list_notes"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"       >
        </ListView>
    </LinearLayout>
</FrameLayout>

<FrameLayout
    android:id="@+id/drawer_frame"
    android:layout_width="240dp"
    android:layout_height="fill_parent"
    android:layout_gravity="start"
    android:layout_weight="1" >

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/darkGray"
        android:choiceMode="singleChoice"/>
     </FrameLayout>

I open PreferenceFragment from one of Drawer items like this:

  FragmentTransaction transaction = getFragmentManager().beginTransaction();
                PrefsFragment prefs = new PrefsFragment();
                transaction.add(R.id.content_frame, prefs);
                transaction.addToBackStack(null);
                transaction.commit();

and this is my PrefsFragment:

public class PrefsFragment extends Fragment {

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getActivity().getFragmentManager().beginTransaction()
            .replace(R.id.content_frame, new PrefsFr())
            .commit();
}


public class PrefsFr extends PreferenceFragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);          
        addPreferencesFromResource(R.xml.preferences);
      ...  }
}

}

but I can not remove this fragment. When I toggle to another Drawer item; PreferenceFragment remains on top of ListView in content_frame with tranceparent background.

Is it possible in ActionBarActivity to remove a fragment without replacing it by another fragment ? If not so how to use PreferenceFragment for api > 11 ? Is it true that the only way is to replace fragments with each other?

解决方案

Is it possible in ActionBarActivity to remove a fragment without replacing it by another fragment ?

Yes, you can use FragmentTransaction#remove(Fragment)

The underlying problem however is that you are showing the PreferenceFragment with transaction.add(R.id.content_frame, prefs);. If you want only the PreferenceFragment to display wihthout the ListView behind it, you should use replace() instead.

这篇关于是否有可能删除的片段,而不由其他片段代替它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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