0

我有两个片段,每个片段都包含一个视图寻呼机,我目前正在使用这个来为我的视图寻呼机使用不同颜色的选项卡。现在我有一个动作栏动作,当点击它时基本上会导致 fragment2。但是当我这样做时,fragment1 并没有被删除,而是在用户刚刚切换到的 fragment2 之上,就像这样。现在,我尝试按照答案所说的去做,并以编程方式添加片段,但这是不可能的(我认为),因为我使用的是自定义 PagerSlidingTabStrip。现在无论如何都可以通过单击按钮来更改片段,同时仍然使用自定义库?此外,我想补充一点,我对 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>

如果您有任何问题,或者我解释得不够好或类似的事情,请说一些,我会添加它或更好地解释它

4

1 回答 1

0

您不能删除在 xml 布局文件中声明的片段。从 xml 中删除您的片段并从代码中添加它(即在活动的 onCreate() 中),然后您就可以稍后将其删除。

于 2014-04-17T06:04:22.400 回答