如何使用tablayout(设计支持库)解决ViewPager的问题?

时间:2015-10-01 05:04:11

标签: android android-fragments android-viewpager android-tablayout

我正在使用最新的android设计tablayout和viewpager,我有三个标签,其中一个公共片段使用网格视图显示图像网格。所有选项卡都有不同的图像集。但数据在滑动时无法正确显示1和2片段。这是我的代码:

public class ChannelPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
private ChannelsModel data;

public ChannelPagerAdapter(FragmentManager fm,  ChannelsModel data) {
    super(fm);
    this.data = data;
}

public void addFragment(Fragment fragment, String title) {
    mFragments.add(fragment);
    mFragmentTitles.add(title);
}

@Override
public Fragment getItem(int position) {
    ChannelsFragment fragment = (ChannelsFragment) mFragments.get(position);
    Bundle bundle = new Bundle();

    if(position == 0) {
        fragment.setData(data);
    }
    else if(position == 1) {
        fragment.setData(data);
    }
    else if(position == 2) {
        fragment.setData(data);

    }
    bundle.putInt("position", position);
    fragment.setArguments(bundle);
    return fragment;
}

@Override
public int getCount() {
    return mFragments.size();
}

@Override
public CharSequence getPageTitle(int position) {
    return mFragmentTitles.get(position);
}}

以下是片段的代码:

public class ChannelsFragment extends Fragment实现ChannelGridAdapter.OnItemClickListener {

private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private ChannelGridAdapter mAdapter;
private List<? extends VehicalModel> vehicalData = new ArrayList<>();
private ChannelsModel data;
public ChannelsFragment() {
    // Required empty public constructor
}

public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;

public void setData(ChannelsModel data) {
    this.data = data;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int position = getArguments().getInt("position");
    if(position == 0)
        vehicalData = new ArrayList<>(data.getCar());
    else if(position == 1)
        vehicalData = new ArrayList<>(data.getBike());
    else if(position == 2)
        vehicalData = new ArrayList<>(data.getHmv());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View rootView =  inflater.inflate(R.layout.fragment_channels, container, false);
    setRetainInstance(true);

    mRecyclerView = (RecyclerView)rootView.findViewById(R.id.channelGrid);
    mRecyclerView.setHasFixedSize(true);

    // The number of Columns
    mLayoutManager = new GridLayoutManager(getContext(), 2);

    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new ChannelGridAdapter(getContext(), vehicalData);
    mAdapter.setOnItemClickListener(this);

    mRecyclerView.setAdapter(mAdapter);
    return rootView;
}

@Override
public void onItemClick(Object object, int position) {
    startActivity(new Intent(getActivity(), ChannelListActivity.class));
}
}

Plz帮助我弄清楚问题。

0 个答案:

没有答案