本文介绍了ANDROID:InstantiateItem方法中具有不同片段的ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ViewPager创建向左滑动的片段,现在我想在每个片段页面中插入片段.

I use ViewPager to create fragments that swipe left and now I want insert fragments into each fragment page.

activityMain.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    ...
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:id="@+id/myviewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <devlight.io.library.ntb.NavigationTabBar
        android:id="@+id/ntb_horizontal"
        app:ntb_swiped="true"/>


</android.support.design.widget.CoordinatorLayout>

和这个: tab0_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:textSize="30sp"
        android:gravity="center"
        android:id="@+id/textView"
        android:layout_centerHorizontal="true"
        android:textColor="@android:color/holo_green_dark"
        android:text="Social\nFragment"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:textSize="15sp"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"
        android:text="androidbelieve.com"
        android:textColor="#000"
        android:layout_below="@+id/textView"
        android:textStyle="italic"/>

</RelativeLayout>

我在 mainActivity.java

final ViewPager viewPager = (ViewPager) findViewById(R.id.myviewpager);
viewPager.setAdapter(new PagerAdapter() {
...

...
}

我尝试通过片段布局在viewpager中打开一个屏幕.

I try open one screen in viewpager via fragment layout.

我尝试:

final ViewPager viewPager = (ViewPager) findViewById(R.id.myviewpager);
viewPager.setAdapter(new PagerAdapter() {
    ...
    public Object instantiateItem(final ViewGroup container, final int position) {
           if (position == 0)
           {
                final View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.fragment_tab0, null, false);
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.myviewpager,new tab0Fragment()).commit();
                container.addView(view);
                return view;
           }
           else if (position == 1)
           ...

   }

...
}

AND tab0Fragment.java

public class tab0Fragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tab0,null);
    }
}

我是android的菜鸟.如何更正此代码?

I am noob in android. how can correct this code?

解决方案:答案不是我的答案,但对我有很大帮助.

Solution:The answers was not my answer but that help me a lot.

@Mr.先生Rabbit ans @Amit Upadhyay我将顶级代码更改为此:

According @Mr. Rabbit ans @Amit Upadhyay I change top code to this:

@Override
            public Object instantiateItem(final ViewGroup container, final int position) {

                if (position == 0)
                {
                    final View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.fragment_tab0, null, false);
                    MyAdapter adapter = new MyAdapter(getSupportFragmentManager());
                    adapter.addFragment(new tab0Fragment(), getResources().getString(R.string.tab0));
                    container.addView(view);
                    return view;
                }
                else if (position == 1)
                {

,并根据@Mr添加MyAdapter类.兔子的答案.

and also adding MyAdapter class according @Mr. Rabbit's answer.

希望可以帮助别人.

推荐答案

尝试分离出适配器和下面的代码.

Try to separate out your adapter and below code.

MyAdapter.java

MyAdapter.java

public class MyAdapter extends FragmentPagerAdapter{

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @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);
    }
}

创建片段

FragmentOne.java

FragmentOne.java

public class FragmentOne extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)    {
      View  view = inflater.inflate(R.layout.fragment_one, container, false);
        return view;
    }
}

FragmentTwo.java

FragmentTwo.java

public class FragmentTwo extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)    {
     View  view = inflater.inflate(R.layout.fragment_two, container, false);
        return view;
    }
}

在活动中像这样设置您的viewpager.

Setup your viewpager like this in your activity.

final ViewPager viewPager = (ViewPager) findViewById(R.id.myviewpager);
    MyAdapter adapter = new MyAdapter(getSupportFragmentManager());

    // Add your fragments in adapter.
    FragmentOne fragmentOne = new FragmentOne();
    adapter.addFragment(fragmentOne, getResources().getString(R.string.fragment_one_title));

    FragmentTwo fragmentTwo = new FragmentTwo();
    adapter.addFragment(fragmentTwo, getResources().getString(R.string.fragment_two_title));

    viewPager.setAdapter(adapter);

然后最后用ViewPager设置NavigationTabBar.

Then finally set your NavigationTabBar with your ViewPager.

final NavigationTabBar navigationTabBar = (NavigationTabBar) findViewById(R.id.ntb_horizontal);
navigationTabBar.setViewPager(viewPager, 2);

这篇关于ANDROID:InstantiateItem方法中具有不同片段的ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 19:04
查看更多