我有一個ViewPager
其處理三種Fragment
個Fragment
。其中第一個包含RecyclerView
。下面是一些代碼:ViewPager和RecyclerView交流中心的行爲
FragmentViewPager
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_player_view_pager, container, false);
ViewPager vpPager = (ViewPager) view.findViewById(R.id.playerViewPager);
adapterViewPager = new PlayerPagerAdapter(getFragmentManager());
vpPager.setAdapter(adapterViewPager);
return view;
}
FragmentPagerAdapter
// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FirstFragment.newInstance("0", "Page # 1");
case 1:
return SecondFragment.newInstance("1", "Page # 2");
case 2:
return ThirdFragment.newInstance("2", "Page # 3");
default:
return null;
}
}
FirstFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
adapter = new FragmentAdapter(getActivity(), myList);
recyclerView.setItemAnimator(new SlideInUpAnimator(new OvershootInterpolator(1f)));
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
recyclerView.setAdapter(adapter);
loadData();
return view;
}
和loadData
方法是通過AsyncTask
填充適配器的API調用。這是onPostExecute方法:
myList = (ArrayList<>) result;
adapter.notifyDataSetChanged();
問題是,當我第一次實例化ViewPager
沒有被顯示在第一Fragment
甚至API調用和notifyDataSetChanged()
方法被調用。但是如果我在其他片段之間滾動,並且由ViewPager
重新創建FirstFragment
,則會顯示數據。
你有什麼想法,這裏發生了什麼?
嘗試爲每個標籤不只是一個片斷實例設置不同的片段類。我希望它會有所幫助 –
對不起,masoud,我的問題有一些錯誤:)。我編輯它。 – jos