替换 Viewpager 中的片段 [重复]

Posted

技术标签:

【中文标题】替换 Viewpager 中的片段 [重复]【英文标题】:replace fragment inside Viewpager [duplicate] 【发布时间】:2016-03-02 22:21:33 【问题描述】:

我有一个带有三个片段的 ViewPager,我想用另一个片段替换第三个片段,我检查了关于它的帖子,但没有任何效果,我以前的布局仍然显示,新的布局只是出现在它上面。请给我一个提示,非常感谢。 这是我的 HomeFragment :

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();
    // Inflate the layout for this fragment
    return rootView;

private void setupTabIcons() 
    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    tabLayout.getTabAt(2).setIcon(tabIcons[2]);

private void setupViewPager(ViewPager viewPager) 
    ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
    adapter.addFragment(new ChatActivity(), "Chat");
    adapter.addFragment(new MapaActivity(), "Mapa");

    adapter.addFragment(new WebServiceActivity(), "Form");

    viewPager.setAdapter(adapter);


class ViewPagerAdapter extends FragmentStatePagerAdapter 
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) 
        super(manager);
    

    @Override
    public Fragment getItem(int position) 

        return mFragmentList.get(position);
    

    @Override
    public int getCount() 

        return mFragmentList.size();
    

    public void addFragment(Fragment fragment, String title) 
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    

    @Override
    public CharSequence getPageTitle(int position) 
        return mFragmentTitleList.get(position);
    

    @Override
    public int getItemPosition(Object object)
        return PagerAdapter.POSITION_NONE;
    

布局fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_
    android:layout_>
    <android.support.design.widget.AppBarLayout
        android:layout_
        android:layout_
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_
            android:layout_
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_
        android:layout_
        app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
</android.support.design.widget.CoordinatorLayout>

webServiceActivity onCreateView 方法:

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        View rootView = inflater.inflate(R.layout.activity_web_service, container, false);
        manager= getFragmentManager();
        //creacion de spinner
        Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(),
                R.array.categorias_array, android.R.layout.simple_spinner_item);   adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
        spinner.setAdapter(adapter);
        b = (Button) rootView.findViewById(R.id.button1);
        // btnPosicion = (Button) rootView.findViewById(R.id.boton_posicion);
        b.setOnClickListener(new View.OnClickListener() 
            public void onClick(View v) 
                // fragmento a insertar
                OneFragment formTurismo = new OneFragment();
                FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
// and add the transaction to the back stackx`
                transaction.replace(R.id.selector_forms, formTurismo,"Turismo");                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                transaction.addToBackStack(null);
                transaction.commit();
            
        );
        return rootView;
    

activity_web_service.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/selector_forms"
    >
    <TextView
        android:id="@+id/textView1"
        android:layout_
        android:layout_
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="@string/formulario"
        android:textSize="30dp" />
    <LinearLayout
        android:layout_
        android:layout_
        android:layout_centerVertical="true"
        android:id="@+id/switch_layout"
        android:orientation="vertical">
        <TextView
            android:layout_
            android:layout_
            android:layout_marginTop="40dp"
            android:text="@string/txt_categorias"
            android:id="@+id/loginUsernameText"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"/>
    <Spinner
        android:layout_
        android:layout_
        android:layout_marginTop="8dp"
        android:id="@+id/spinner"
        android:layout_below="@id/textView1"
        android:padding="10dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp" />
    <Button
        android:id="@+id/button1"
        android:layout_
        android:layout_
        android:layout_gravity="center_horizontal"
        android:padding="10dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="8dp"
        android:text="Ver formulario" />
    </LinearLayout>
</RelativeLayout>

fragment_one.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/turismo_form"
    >
    <TextView
        android:id="@+id/textView1"
        android:layout_
        android:layout_
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="@string/formularioT"
        android:textSize="30dp" />
    <LinearLayout
        android:layout_
        android:layout_
        android:layout_centerVertical="true"
        android:orientation="vertical">
        <TextView
            android:layout_
            android:layout_
            android:layout_marginTop="40dp"
            android:text="@string/txt_canton"
            android:id="@+id/labelCantonT"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"/>
        <Spinner
            android:layout_
            android:layout_
            android:layout_marginTop="8dp"
            android:id="@+id/spinner"
            android:padding="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp" />
        <TextView
            android:layout_
            android:layout_
            android:layout_marginTop="30dp"
            android:text="@string/txt_tipo"
            android:id="@+id/labelTipo"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"/>
        <Spinner
            android:layout_
            android:layout_
            android:layout_marginTop="8dp"
            android:id="@+id/spinnerT"
            android:padding="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp" />
        <TextView
            android:layout_
            android:layout_
            android:layout_marginTop="40dp"
            android:text="@string/txt_nombreT"
            android:id="@+id/labelNombreT"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"/>
        <EditText
            android:layout_
            android:layout_
            android:id="@+id/nombreT"
            android:layout_gravity="center_horizontal"
            android:padding="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"/>
        <TextView
            android:layout_
            android:layout_
            android:layout_marginTop="40dp"
            android:text="@string/txt_direccionT"
            android:id="@+id/labelDireccionT"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"/>
        <EditText
            android:layout_
            android:layout_
            android:id="@+id/direccionT"
            android:layout_gravity="center_horizontal"
            android:padding="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"/>
        <Button
            android:id="@+id/button1"
            android:layout_
            android:layout_
            android:layout_gravity="center_horizontal"
            android:padding="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:layout_marginTop="8dp"
            android:text="Registrar" />
    </LinearLayout>
</RelativeLayout>

【问题讨论】:

【参考方案1】:

如果您想替换视图页面中的片段,您必须通知您的适配器更改。您的片段列表需要更新以删除第三个片段并在其位置添加一个新片段。完成后,在适配器中调用 notifyDataSetChanged。

您的适配器中可以有这样的 API:

public void replaceFragment(Fragment fragment, String title, int index) 
    mFragmentList.remove(index);
    mFragmentList.add(index, fragment);
    // do the same for the title
    notifyDataSetChanged();

【讨论】:

非常感谢您的快速回复,我按照您的建议将该方法添加到我的内部类中,并更改为静态类 ViewPageAdapter,也在我的 onClickListener 中进行如下调用:OneFragment formTurismo = new OneFragment( ); ViewPagerAdapter 适配器 = new ViewPagerAdapter(getFragmentManager()); adapter.replaceFragment(formTurismo,"Turismo",0);但是启动这个错误:java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 我不确定你想说什么,但你在这里粘贴了一个例外。您必须在添加任何片段之前调用 replaceFragment,因为异常告诉您列表为空。 我试图在我想要更改的布局按钮的 OnclickListener 方法中启动是这样还是我做错了? 当您想要替换片段时,您不必创建新的适配器。您使用现有适配器并调用 API 以将其中一个片段替换为新片段。 好的,现在很清楚了,但是很抱歉这个愚蠢的问题,但是我如何在其他布局中获得当前的适配器【参考方案2】:

阅读有关 Viewpager 的 android 文档后,我将 activity_webservice.xml 内容修改为框架布局,并在 onCreateMethod 中使用此代码动态加载内容:

 manager= getFragmentManager();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        SelectorFragment fragment = new SelectorFragment();
        fragmentTransaction.add(R.id.selector_forms, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

它调用另一个名为 fragment_selector.xml 的 xml 布局,并在片段类中添加:

 FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.selector_forms, formulario);
                transaction.addToBackStack(null);

它已经完成了。

【讨论】:

以上是关于替换 Viewpager 中的片段 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

从ViewPager android替换片段

如何确定 Fragment 何时在 ViewPager 中可见

ViewPager2 中的片段在返回父片段时被重新创建

如何从(和替换)TabLayout 和 ViewPager 移动到新片段?

ViewPager2 无法动态添加删除片段

在tablayout中替换片段