我的ViewPager
无效。我不知道我的代码有什么问题。我在使用Fragments
:
基本上,我有一个名为Fragment
的{{1}}有一个列表,在列表项的点击事件中,我移动到名为MyListFragment
的第二个Fragment
FlowersFragment
包含三个项目(另外三个ViewPager
)。我的Fragments
中有一个Button
,点击FlowersFragment
后,我移动到名为Button
的第三个屏幕。现在我有一个问题:
如果我从WelcomeFragment
返回FlowersFragment
,那么在经过一些研究后我得到了一个像WelcomeFragment
这样的解决方案时,它会从Fragments
丢失一些ViewPager
适配器。这对我的寻呼机很有用childFragmentManager
现在正在我的Fragments
寻呼机中正确显示。但是,如果我回到FlowersFragment
并点击MyListFragment
的第二项或第三项,它会显示我的异常,但没有活动。
这是我的名为ListFragment
MyListFragment
这是我的public class MyListFragment extends Fragment{
private View view;
private FragmentManager fm;
private FragmentTransaction ft;
private ListView listviewPlaces;
private FloristAdapter adapter;
private ArrayList<FloristItems> items = new ArrayList<FloristItems>();
private FlowersFragment frag;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflater = LayoutInflater.from(getActivity());
view = inflater.inflate(R.layout.fragment_list, container,false);
listviewPlaces = (ListView)view.findViewById(R.id.listviewPlaces);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
for (int i = 0; i < 10; i++) {
FloristItems florist = new FloristItems();
florist.away = "3 miles away";
florist.title = "Florist Flowers";
florist.price = "$45";
items.add(florist);
}
adapter = new FloristAdapter(getActivity().getApplicationContext(), items);
listviewPlaces.setAdapter(adapter);
listviewPlaces.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(frag == null){
frag = new FlowersFragment();
}
handleFragments(frag);
}
});
}
private void handleFragments(Fragment fragment){
fm = getActivity().getSupportFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.addToBackStack(null);
ft.commit();
}
}
类,其中包含三个片段的视图寻呼机:
FlowersFragment
这是我的寻呼机适配器类:
public class FlowersFragment extends Fragment{
private View view;
private FragmentManager fm;
private FragmentTransaction ft;
private ViewPager pager;
private Button btnSendThatFlower;
private FlowerPagerAdapter pageAdapter;
private WelcomeFragment welcomeFrag;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflater = LayoutInflater.from(getActivity());
view = inflater.inflate(R.layout.fragment_choose_flower_buyer, container,false);
pager = (ViewPager)view.findViewById(R.id.viewpager);
btnSendThatFlower = (Button)view.findViewById(R.id.btnSendThatFlower);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
pageAdapter = new FlowerPagerAdapter(getActivity().getSupportFragmentManager());
pager.setAdapter(pageAdapter);
btnSendThatFlower.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(welcomeFrag == null){
welcomeFrag = new WelcomeFragment();
}
handleFragments(welcomeFrag);
}
});
}
private void handleFragments(Fragment fragment){
fm = getActivity().getSupportFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.addToBackStack(null);
ft.commit();
} }
public class FlowerPagerAdapter extends FragmentPagerAdapter{
private ArrayList<Fragment> pagerItems;
public FlowerPagerAdapter(FragmentManager fm) {
super(fm);
pagerItems = new ArrayList<Fragment>();
pagerItems.add(FlowerImage1.getInstance());
pagerItems.add(FlowerImage2.getInstance());
pagerItems.add(CustomFlowerFragment.getInstance());
}
@Override
public Fragment getItem(int num) {
if (num == 0) {
return new FlowerImage1();
}
else if(num == 1) {
return new FlowerImage2();
}
else{
return new CustomFlowerFragment.getInstance();
}
}
@Override
public int getCount() {
return pagerItems.size();
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
//super.restoreState(state, loader);
}}
FlowerImage1
FlowerImage1
是仅包含CustomFlowerFragment
的片段。
这是我的最后一个片段imageview
WelcomeFragment
当我按下后按,然后它在viewpager中没有显示片段,但在浏览器上滑动后,它只显示第一个或有时是最后一个寻呼机片段。
答案 0 :(得分:0)
我也面临同样的问题,最后在片段适配器中添加了以下代码。
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
//super.restoreState(state, loader);
}
答案 1 :(得分:0)
我也遇到了同样的问题,但我通过扩展&#34; FragmentStatePagerAdapter&#34;来修复它。而不是FragmentPagerAdapter。对于你的情况,它将是:
public class FlowerPagerAdapter extends FragmentStatePagerAdapter{
private ArrayList<Fragment> pagerItems;
public FlowerPagerAdapter(FragmentManager fm) {
super(fm);
pagerItems = new ArrayList<Fragment>();
pagerItems.add(FlowerImage1.getInstance());
pagerItems.add(FlowerImage2.getInstance());
pagerItems.add(CustomFlowerFragment.getInstance());
}
@Override
public Fragment getItem(int num) {
if (num == 0) {
return new FlowerImage1();
}
else if(num == 1) {
return new FlowerImage2();
}
else{
return new CustomFlowerFragment.getInstance();
}
}
@Override
public int getCount() {
return pagerItems.size();
}
}
&#13;