第三个选项卡接收给第二个选项卡的值

Third Tab Receives Values Given to Second Tab

我被这个问题困扰了一段时间。我正在制作一个包含三个几乎相同片段的应用程序。我使用了 this tutorial. I also tried passing data from MainActivity to all fragments (fragments being restaurantFragment, favouriteRestaurants, blockedRestaurants), and only the third one is problematic. Also when I change which data is sent to which tab - first and second tabs get the correct ones. The third tab is frequently not called at all, and if it is called it mostly gets the wrong data, unlike the other 2 tabs, and also no error is displayed when I select the third tab, and a lot of times when I try to evaluate number of tabs while debugging it often gets only 2 tabs. I have tried this, 和许多其他解决方案,但其中 none 对我有用。

有时如果我 select 第二个选项卡之前的第三个选项卡显示正确的列表,但如果我 select 第二个选项卡第一个它只是复制它,而不是显示正确的值。

主要活动:

TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_one)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_two)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_three)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);

final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

寻呼机适配器:

public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override    
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            return MapsFragment.newInstance(restaurants);
        case 1:
            return RestaurantsFragment.newInstance(restaurants);
        case 2:
            return MyRestaurantsFragment.newInstance(restaurants);
        default:
            return null;
    }
}

@Override
public int getCount() {
    return mNumOfTabs;
    }
}

每个片段看起来都一样,像这样:

    public static MyRestaurantsFragment newInstance(ArrayList<Restaurant> restaurants) {

    Bundle args = new Bundle();
    args.putSerializable("restaurants",restaurants);

    MyRestaurantsFragment fragment = new MyRestaurantsFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    restaurantList = (ArrayList<Restaurant>)getArguments().getSerializable("restaurants");
    super.onCreate(savedInstanceState);
}

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

    listView = (ListView) view.findViewById(R.id.restaurants_list);
    adapter = new RestaurantsListAdapter(getActivity(), restaurantList);
    listView.setAdapter(adapter);

    return view;
}

content_main:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="@android:color/white" />
</android.support.design.widget.AppBarLayout>

您应该将数据传递给适配器并调用

 MyRestaurantsFragment tab3 = MyRestaurantsFragment.newInstance(restaurants);

而不是

MyRestaurantsFragment tab3 = new MyRestaurantsFragment();

你是在打电话给 setUpWithViewPager(...) 吗?

// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) root.findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);

更新

要查看标题,请尝试覆盖 PagerAdapter 中的 getPageTitle(),如下所示:

@Override
public CharSequence getPageTitle(int position) {
    if ( position == 0 ) {
        return "Title 1";
    } else if ( position == 1 ) {
        return "Title 2";
    } else {
        return "Title 3";
    }
}

此外,如果您使用 tabLayout.setupWithViewPager,您可以删除代码的这些部分:

tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_one)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_two)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_three)));

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

我假设您的布局是这样的:

<android.support.v4.view.ViewPager
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <android.support.design.widget.TabLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="top" />

 </android.support.v4.view.ViewPager>

更新 2

尝试以这种方式更改您的 getItem 方法:

@Override
public Fragment getItem(int position) {

    if ( position == 0 ) {
        return MapsFragment.newInstance(restaurants);
    } else if ( position == 1 ) {
        return RestaurantsFragment.newInstance(restaurants);
    }

    return MyRestaurantsFragment.newInstance(restaurants);
}