如何替换已经有一些视图的片段?

时间:2014-04-15 07:02:21

标签: java android xml android-fragments

我有这个fragment_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout_settings_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview_my_account"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="My Account" />

    <TextView
        android:id="@+id/textview_my_account_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="List" />

</LinearLayout>

使用以下代码:

public class SettingsFragment extends Fragment implements Constants, OnClickListener{

    public static SettingsFragment newInstance() {
        SettingsFragment f = new SettingsFragment();
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
            Bundle savedInstanceState) {
        container.setPadding(0, 0, 0, 0);
        final View view = inflater.inflate(R.layout.fragment_settings, container,
                false);
        initViews(view);
        return view;
    }

    private void initViews(View view) {
        // TODO Auto-generated method stub
        TextView textView = (TextView)view.findViewById(R.id.textview_my_account);
        textView.setOnClickListener(this);
        TextView textView1 = (TextView)view.findViewById(R.id.textview_my_account_1);
        textView1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        switch (v.getId()) {
        case R.id.textview_my_account:
            SettingsContactDetailFragment settingsContactDetailFragment = SettingsContactDetailFragment.newInstance();
            settingsContactDetailFragment.setRetainInstance(true);
            fragmentTransaction.replace(R.id.linearlayout_settings_fragment, settingsContactDetailFragment);
            fragmentTransaction.addToBackStack(FragmentEnum.SETTINGS_CONTACT_DETAIL_FRAGMENT);
            fragmentTransaction.commit();
            break;

        case R.id.textview_my_account_1:
            SettingsContactListFragment settingsContactListFragment = SettingsContactListFragment.newInstance();
            settingsContactListFragment.setRetainInstance(true);
            fragmentTransaction.replace(R.id.linearlayout_settings_fragment, settingsContactListFragment);
            fragmentTransaction.addToBackStack(FragmentEnum.SETTINGS_CONTACT_LIST_FRAGMENT);
            fragmentTransaction.commit();
            break;
        }
    }
}

我试过这个工作。但是,当我使用 fragmentTransaction.replace 时,会添加片段,其中包含已存在于fragment_settings.xml中的视图

LinearLayout linearLayout = (LinearLayout)view.findViewById(R.id.linearlayout_settings_fragment);
linearLayout.removeAllViews();

但为什么以下不起作用?

fragmentTransaction.replace(R.id.linearlayout_settings_fragment, settingsContactListFragment);

1 个答案:

答案 0 :(得分:2)

FragmentTransaction.replace方法仅从容器中删除以前添加的Fragment视图。它不会删除属于容器的所有视图。这就是为什么它被附加到另外2 TextViews

如果您要替换TextView,那么它们应该是另一个Fragment的一部分,最初可以添加到R.id.linearlayout_settings_fragment,后者可以用SettingsContactDetailFragment替换它SettingsContactListFragment个片段。