Android 正确的片段事务,片段由XML定义,而不是以编程方式定义的片段

Android 正确的片段事务,片段由XML定义,而不是以编程方式定义的片段,android,android-fragments,fragmenttransaction,android-nested-fragment,pagerslidingtabstrip,Android,Android Fragments,Fragmenttransaction,Android Nested Fragment,Pagerslidingtabstrip,我有两个片段,每个片段都包含一个视图寻呼机,我现在使用它来为我的视图寻呼机使用不同颜色的标签。现在我有了一个动作条,当点击时基本上会导致碎片2。但是当我这样做的时候,fragment1并没有被删除,而是在用户刚刚切换到的fragment2之上,比如。现在,我尝试按照答案所说的做,并以编程方式添加片段,但这是不可能的(我认为),因为我使用的是定制的PagerSlidingTastrip。现在,在仍然使用自定义库的情况下,是否仍然可以通过单击按钮来更改片段?另外,我想补充一点,我对Android有点

我有两个片段,每个片段都包含一个视图寻呼机,我现在使用它来为我的视图寻呼机使用不同颜色的标签。现在我有了一个动作条,当点击时基本上会导致碎片2。但是当我这样做的时候,fragment1并没有被删除,而是在用户刚刚切换到的fragment2之上,比如。现在,我尝试按照答案所说的做,并以编程方式添加片段,但这是不可能的(我认为),因为我使用的是定制的PagerSlidingTastrip。现在,在仍然使用自定义库的情况下,是否仍然可以通过单击按钮来更改片段?另外,我想补充一点,我对Android有点陌生

片段1

import java.lang.reflect.Field;

import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.astuetz.PagerSlidingTabStrip;
import com.bernard.beaconportal.R.layout;



public class FragmentsView extends SherlockFragment {


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            setHasOptionsMenu(true);


            View view = inflater.inflate(R.layout.viewpager_schedule, container, false);




            ViewPager pager = (ViewPager) view.findViewById(R.id.viewPager1);




            pager.setAdapter(new ViewPagerAdapterScheduleView(getChildFragmentManager()));
            PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view.findViewById(R.id.pagerTabStrip1);


            tabs.setViewPager(pager);

            return view;
        }

        @Override
        public void onDetach() {
            super.onDetach();
            try {
                Field childFragmentManager = Fragment.class
                        .getDeclaredField("mChildFragmentManager");
                childFragmentManager.setAccessible(true);
                childFragmentManager.set(this, null);
            } catch (NoSuchFieldException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void onCreateOptionsMenu(
              Menu menu, MenuInflater inflater) {
           inflater.inflate(R.menu.android_edit, menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
           // handle item selection
           switch (item.getItemId()) {
              case R.id.edit:
                  alert_dialog();
                 return true;
              default:
                 return super.onOptionsItemSelected(item);
           }

        }



        private void alert_dialog() {
             {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Edit Bands")
                       .setItems(R.array.edit_mode, new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int which) {

                               Fragment newFragment = new FragmentsLinked();

                               android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();




                               transaction.replace(R.id.view_container, newFragment);
                               transaction.addToBackStack(null);



                               transaction.commit();
                       }
                });
                AlertDialog alertDialog = builder.create();

                         alertDialog.show();


        }
        }



        }
片段2

import java.lang.reflect.Field;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.astuetz.PagerSlidingTabStrip;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class FragmentsLinked extends SherlockFragment {



        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            setHasOptionsMenu(true);



            View view = inflater.inflate(R.layout.viewpager_schedule_linked, container, false);
            // Locate the ViewPager in viewpager_main.xml
            ViewPager pager = (ViewPager) view.findViewById(R.id.viewPager2);
            // Set the ViewPagerAdapter into ViewPager
            pager.setAdapter(new ViewPagerAdapterScheduleLinked(getChildFragmentManager()));

            return view;
        }

        @Override
        public void onDetach() {
            super.onDetach();
            try {
                Field childFragmentManager = Fragment.class
                        .getDeclaredField("mChildFragmentManager");
                childFragmentManager.setAccessible(true);
                childFragmentManager.set(this, null);
            } catch (NoSuchFieldException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void onCreateOptionsMenu(
              Menu menu, MenuInflater inflater) {
           inflater.inflate(R.menu.android_apply, menu);
           inflater.inflate(R.menu.android_help, menu);

        }




}
用于片段1的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/view_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <com.astuetz.PagerSlidingTabStrip
    android:id="@+id/pagerTabStrip1"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    app:pstsIndicatorColor="#3498DB" 
    app:pstsUnderlineColor="#3498DB"
    />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/pagerTabStrip1" />

</RelativeLayout>

用于片段2的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:id="@+id/linked_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="50dp"/>

    <View
        android:id="@+id/View2"
        android:layout_width="fill_parent"
        android:layout_height="8dp"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="#3498DB" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="Edits Will be Applied to All of the Same Band"
        android:textAlignment="gravity"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>


如果您有任何问题,或者我没有很好地解释某些内容,或者类似的内容,请说一些内容,我将添加它或更好地解释它

您不能删除xml布局文件中声明的片段。从xml中删除您的片段并从代码中添加它(即在activity的onCreate()中),之后您就可以删除它了

这正是我的问题所在,正如我在问题中所说,我使用的库不允许我删除片段,也不允许在xml中声明它。但是XML文件中声明的元素无法删除-这就是Android框架的工作方式,因此如果您的库在这方面出现问题,您需要使用不同的库。是否有其他方法可以切换片段,而不需要仅以编程方式定义片段?或者,最好的方法是制作片段活动?