替换Viewpager中的片段

时间:2016-03-02 22:21:33

标签: android android-fragments android-viewpager

我有一个带有三个片段的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_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        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_width="match_parent"
    android:layout_height="match_parent"
    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_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="@string/formulario"
        android:textSize="30dp" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:id="@+id/switch_layout"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:text="@string/txt_categorias"
            android:id="@+id/loginUsernameText"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"/>
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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_width="fill_parent"
        android:layout_height="wrap_content"
        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_width="match_parent"
    android:layout_height="match_parent"
    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_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="@string/formularioT"
        android:textSize="30dp" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:text="@string/txt_canton"
            android:id="@+id/labelCantonT"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"/>
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:id="@+id/spinner"
            android:padding="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:text="@string/txt_tipo"
            android:id="@+id/labelTipo"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"/>
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:id="@+id/spinnerT"
            android:padding="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:text="@string/txt_nombreT"
            android:id="@+id/labelNombreT"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/nombreT"
            android:layout_gravity="center_horizontal"
            android:padding="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:text="@string/txt_direccionT"
            android:id="@+id/labelDireccionT"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            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_width="fill_parent"
            android:layout_height="wrap_content"
            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>

2 个答案:

答案 0 :(得分:1)

如果要替换viewpager中的片段,则必须通知适配器更改。需要更新您的片段列表以删除第3个片段并在其位置添加新片段。完成后,在调用适配器中调用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();
}

答案 1 :(得分:-1)

在阅读有关Viewpager的android文档后,我将activity_webservice.xml内容修改为framelayout,并使用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布局,并在fragment类中添加:

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

它已经完成了。

相关问题