我遇到了支持库ViewPager的问题。 ViewPager位于一个片段内,它由3个选项卡组成:一个将显示一些信息,第二个和第三个显示一个元素列表(因此它们都是从同一个片段生成的)。当我从第一个滚动到第二个时,一切正常,但是如果我尝试从第二个滚动到第三个,则碎片发生了一些事情,即使发生这种情况,它们也不会再次出现,甚至PagerTabStrip也会消失。此外,我尝试使用相同类型的片段为3个选项卡(与列表一起消失的那个),一切似乎都运行良好,所以我对此非常感兴趣。此外,与问题相关的控制台上唯一的日志是这一个:
W / FragmentManager:moveToState:StoreListFragment的片段状态{3fb980e7#2 id = 0x7f0d0080 android:switcher:2131558528:2}未内联更新;预期状态3发现2
这是我的父片段的代码:
public class StoreFragment extends Fragment {
@Bind(R.id.pager) ViewPager mPager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_store, container, false);
ButterKnife.bind(this, v);
setupViewPager();
return v;
}
private void setupViewPager() {
String[] titles = {getString(R.string.store_information),
getString(R.string.store_offers),
getString(R.string.store_products)
};
mPager.setAdapter(new StoresPagerAdapter(getChildFragmentManager(), titles));
}
}
这是PagerAdapter的代码:
public class StoresPagerAdapter extends FragmentPagerAdapter {
private String[] mPageTitles;
public StoresPagerAdapter(FragmentManager fm, String[] titles) {
super(fm);
mPageTitles = titles;
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return StoreInfoFragment.newInstance();
//return StoreListFragment.newInstance(position);
case 1:
return StoreListFragment.newInstance(position);
case 2:
return StoreListFragment.newInstance(position);
default:
return null;
}
}
@Override
public int getCount() {
return mPageTitles != null ? mPageTitles.length : 0;
}
@Override
public CharSequence getPageTitle(int position) {
return mPageTitles[position];
}
}
片段之一:
public class StoreListFragment extends Fragment implements MyListListener {
@Bind(R.id.store_list) RecyclerView mStoreContentView;
private ArrayList<StoreModel> mStoreContents = new ArrayList<>();
public static StoreListFragment newInstance(int page) {
return new StoreListFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mStoreContents.add(new Schedule());
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_store_list, container, false);
ButterKnife.bind(this, v);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupList();
}
protected void setupList() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
mStoreContentView.setHasFixedSize(true);
mStoreContentView.setLayoutManager(layoutManager);
mStoreContentView.setAdapter(new ArrayAdapter(this, mStoreContents, R.layout.list_elem_locations));
}
@Override
public void onClickElement(int elementId, String elementName) {
Intent i = new Intent(this.getActivity(), DetailActivity.class);
startActivity(i);
}
}
答案 0 :(得分:0)
我认为你的问题在这里:
public static StoreListFragment newInstance(int page) {
return new StoreListFragment();
}
您没有在任何地方使用此页面,而是获得两个完全相同的StoreListFragments。你应该添加:
Bundle args = new Bundle();
args.putInt("key", page);
fragment.setArguments(args);
然后在某处使用这种区别。