TabLayout始终显示最后一个片段

时间:2018-07-16 11:56:26

标签: android android-layout android-fragments android-tablayout

我正在尝试根据参数Tablayout的值(0或1)从2个不同的片段填充Radios.newInstance(int type)

第一个片段包含所有广播电台,第二个片段包含收藏的电台

但是它总是向我显示包含收藏夹电台

的片段
public class HomeFragment extends Fragment {

public static final String TAG = "HomeFragment";
private SectionsPagerAdapter mSectionsPagerAdapter;
private ProgressBar mProgressView;
private ViewGroup mContainer;
private BaseActivity activity;

public HomeFragment() {
}

public static HomeFragment newInstance() {
    return new HomeFragment();
}

public void showProgress(final boolean show) {
    mContainer.setVisibility(show ? View.GONE : View.VISIBLE);
    mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    View view = inflater.inflate(R.layout.fragment_home, container, false);
    activity = (BaseActivity) getActivity();
    activity.findViewById(R.id.tabs).setVisibility(View.VISIBLE);
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    activity.getSupportActionBar().show();
    mProgressView = (ProgressBar) activity.findViewById(R.id.progress);
    mContainer = container;
    showProgress(true);

    ViewPager mViewPager = (ViewPager) view.findViewById(R.id.container);

    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(RadiosFragment.newInstance(0));
    fragments.add(RadiosFragment.newInstance(1));

    mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager(), fragments);

    mViewPager.setAdapter(mSectionsPagerAdapter);
    TabLayout tabLayout = (TabLayout) activity.findViewById(R.id.tabs);
    tabLayout.setVisibility(View.VISIBLE);
    tabLayout.setTabMode(TabLayout.GRAVITY_CENTER);
    tabLayout.setupWithViewPager(mViewPager);
    showProgress(false);

    return view;
}

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> mFragments;

    public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        mFragments = fragments;

    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position) {
            case 0:
                return "RADIOS";
            case 1:
                return "FAVORITES";
            default:
                return null;
        }


    }
}
}

这是我要显示的片段,如果type == 0显示所有广播电台,如果type == 1显示最喜欢的电台

public class RadiosFragment extends Fragment {

private static String TYPE = "NONE";

private FirebaseRecyclerAdapter<FireBaseManager.Radio, ViewHolder> mAdapter = null;
private FirebaseRecyclerAdapter<FireBaseManager.Favorites, ViewHolder> mAdapter_fav = null;

private RecyclerView recyclerView;

public ArrayList<FireBaseManager.Favorites> listOrder;

public RadiosFragment() { }

public static RadiosFragment newInstance(int type) {
    if (type == 1 )
        TYPE = "favorite";
    else
        TYPE = "radios";

    RadiosFragment fragment = new RadiosFragment();
    Bundle args = new Bundle();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

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

    final Context context = view.getContext();
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    recyclerView = (RecyclerView) view.findViewById(R.id.list);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));

    Query query = FireBaseManager.Radio.Ref.orderByChild(FireBaseManager.Radio.Table.Name.text);
    ProgressDialog progressDialog = new ProgressDialog(getContext());

    if (TYPE == "favorite"){
                    ....
                    @Override
                    protected void populateViewHolder(ViewHolder viewHolder, FireBaseManager.Favorites model, int position) {
                        ...
                        viewHolder.Initialize(Data);
                        ...
                    }
                };
                recyclerView.setAdapter(mAdapter_fav);
            }
        });
    }
    else if(TYPE == "radios") {
                ....
                @Override
                protected void populateViewHolder(ViewHolder viewHolder, FireBaseManager.Radio model, int position) {
                    ...
                    viewHolder.Initialize(Data);
                    ...
                    });
                }
            };
            recyclerView.setAdapter(mAdapter);
        });
    }
    return view;
}

}

我认为问题与 onCreateView / onCreate 有关,但是我不确定,请您帮我一下。

2 个答案:

答案 0 :(得分:3)

这可能会对您有所帮助。

mViewPager.setCurrentItem(0); 

使用tabLayout设置viewpager后添加此行

答案 1 :(得分:0)

非常感谢您的回答,我已经找到解决问题的方法。

解决方案是在Tablayout上添加Listener并拦截事件。

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            if (tab.getPosition() == 0)

                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.flContent, RadiosFragment.newInstance(0), FavoriteActivity.TAG).addToBackStack(null).commit();

            if (tab.getPosition() == 1)

                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.flContent, RadiosFragment.newInstance(1), FavoriteActivity.TAG).addToBackStack(null).commit();


        }

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

        }

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

        }
    });