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

时间:2017-02-16 09:50:18

标签: android android-fragments tabs android-pageradapter

我一直坚持这个问题一段时间了。我正在制作一个有三个几乎相同的片段的应用程序。我使用了this教程。我还尝试将数据从MainActivity传递到所有片段(片段为restaurantFragment,favouriteRestaurants,blockedRestaurants),只有 第三个​​片段存在问题。此外,当我更改哪些数据发送到哪个选项卡时 - 第一个和第二个选项卡获得正确的选项卡。第三个选项卡通常根本不被调用,如果它被调用它主要得到错误的数据,不像其他2个选项卡,并且当我选择第三个选项卡时也没有显示错误,并且很多次我尝试在调试时评估选项卡的数量通常只有2个选项卡 。我尝试了thisthis和其他一些解决方案,但这些解决方案都不适用于我。

有时如果我在第二个标签之前选择第三个标签会显示正确的列表,但如果我先选择第二个标签,则只是复制它,而不是显示正确的值。

MainActivity:

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) {

        }
    });

PagerAdapter:

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>

2 个答案:

答案 0 :(得分:1)

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

 MyRestaurantsFragment tab3 = MyRestaurantsFragment.newInstance(restaurants);

而不是

MyRestaurantsFragment tab3 = new MyRestaurantsFragment();

答案 1 :(得分:1)

您是否在呼叫setUpWithViewPager(...)

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

<强>更新

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

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