ViewPager中的片段在从另一个片段返回时不显示任何内容

时间:2016-02-01 09:34:23

标签: android-fragments

我在名为FragmentHome的片段中有一个ViewPager,而viewpager包含片段。我正在使用FragmentStatePagerAdapter。当fragmentHome首先加载时,Viewpager中的片段显示完美。我有另一个名为FragmentCountryList的片段来选择一个国家/地区,根据该国家/地区,viewpager中的片段将被刷新/更新。现在问题是在选择国家之后,国家片段被弹出并返回到FragmentHome,但是,在视图寻呼机中没有显示,如果我们滚动第二页(片段)可以查看然后向后滚动第一页(片段)可以查看。

FragmentStatePagerAdapter是新的,所以我无法纠正这个问题的原因。请帮忙。我使用的代码在下面给出

public class FragmentHome extends WeGrabBaseFragment {
private static String TAG = "FragmentHome";
private HomeDealsDataClass homeDealsDataClass;//1, homeDealsDataClass2;
private ViewPager viewPagerHome;
private ImageView imgViewDealIndicator;
private OnCitySelectedListener onCitySelectedListener;
private FragPageAdapter fragAdapt;
private ArrayList<HomeDealsDataClass> arrayListHomeData;
private FragmentHomeDetails fragmentHomeDetails1,fragmentHomeDetails2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Activity = (WeGrabBaseActivity) this.getActivity();
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater
            .inflate(R.layout.fragment_home, container, false);
    ((WeGrabMainActivity) Activity).showBottomBar();
    ((WeGrabMainActivity) Activity).setShowActionBar();
    ((WeGrabMainActivity) Activity).setHeading(PrefUtil.getCityName(Activity));
    ((WeGrabMainActivity) Activity).defaultButtonState();
    viewPagerHome = (ViewPager) v.findViewById(R.id.viewPagerHome);
    viewPagerHome.setOffscreenPageLimit(2);
    imgViewDealIndicator = (ImageView) v.findViewById(R.id.imgViewDealIndicator);
    Picasso.with(Activity).load(R.drawable.deals1).into(imgViewDealIndicator);

    viewPagerHome.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            if (position == 0) {
                Picasso.with(Activity).load(R.drawable.deals1).into(imgViewDealIndicator);
            } else if (position == 1) {
                Picasso.with(Activity).load(R.drawable.deals2).into(imgViewDealIndicator);
            }
        }

        @Override
        public void onPageSelected(int position) {
            fragAdapt.notifyDataSetChanged();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    onCitySelectedListener = new OnCitySelectedListener() {
        @Override
        public void onCitySelected() {
            fetchFromWeb();
        }
    };

    fetchFromWeb();
    return v;
}

private void fetchFromWeb() {
    if (Activity.isNetworkAvailable()) {
        if (VolleyUtils.volleyEnabled) {
            Activity.startSpinwheel(false, true);
            JSONObject jsonObject = new JSONObject();
            try {
                if (!PrefUtil.isGPSLocation(Activity)) {
                    jsonObject.putOpt(AppConstants.COUNTRY_ID, PrefUtil.getCountryCode(Activity));
                    jsonObject.putOpt(AppConstants.CITY_ID, PrefUtil.getCityCode(Activity));

                    Log.e(TAG, "Not GPS Home : " + jsonObject.toString());
                    CommandFactory commandFactory = new CommandFactory();
                    commandFactory.sendPostCommand(
                            AppConstants.WEGRAB_HOME_URL, jsonObject);
                } else {
                    /*jsonObject.putOpt(AppConstants.COUNTRY_NAME, PrefUtil.getCountryName(Activity));
                    jsonObject.putOpt(AppConstants.CITY_NAME, PrefUtil.getCityName(Activity));*/
                    jsonObject.putOpt(AppConstants.COUNTRY_ID, "112");
                    jsonObject.putOpt(AppConstants.CITY_ID, "6");

                    CommandFactory commandFactory = new CommandFactory();
                    Log.e(TAG, "In  GPS Home : " + jsonObject.toString());
                    /*commandFactory.sendPostCommand(
                    AppConstants.WEGRAB_HOME_URL_GPS, jsonObject);*/
                    commandFactory.sendPostCommand(
                            AppConstants.WEGRAB_HOME_URL, jsonObject);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}

@Override
public void onHomeDetailsPageDataLoadedSuccessfully(JSONObject jsonObject) {
    super.onHomeDetailsPageDataLoadedSuccessfully(jsonObject);
    Activity.stopSpinWheel();
    try {
        if (jsonObject != null) {

            if (jsonObject != null) {
                fragAdapt = new FragPageAdapter(getFragmentManager());
                viewPagerHome.setAdapter(fragAdapt);
                JSONArray jsonArray = jsonObject.getJSONArray(AppConstants.DEALS);
                arrayListHomeData = new ArrayList<HomeDealsDataClass>();
                if(jsonArray.length() > 0) {
                    viewPagerHome.setVisibility(View.VISIBLE);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        arrayListHomeData.add(new HomeDealsDataClass(jsonArray.getJSONObject(i)));
                    }
                    fragAdapt.notifyDataSetChanged();
                }else{
                    viewPagerHome.setVisibility(View.GONE);
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onHomeDetailsPageDataLoadedFailed(JSONObject jsonObject) {
    super.onHomeDetailsPageDataLoadedFailed(jsonObject);
    Activity.stopSpinWheel();
}

private class FragPageAdapter extends FragmentStatePagerAdapter {
    FragPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int pos) {
        switch (pos) {

            case 0:
                Log.e(TAG,"Returns Fragment Home deals 1");
                fragmentHomeDetails1 = FragmentHomeDetails.newInstance(arrayListHomeData.get(0));
                return fragmentHomeDetails1;
            case 1:
                Log.e(TAG,"Returns Fragment Home deals 2");
                fragmentHomeDetails2 =  FragmentHomeDetails.newInstance(arrayListHomeData.get(1));
                return fragmentHomeDetails2;
            default:
                return null;
        }
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        if(arrayListHomeData != null)
            return arrayListHomeData.size();
        else
            return 0;
    }
}

}

1 个答案:

答案 0 :(得分:1)

我之前也遇到过这个问题,在搜索完SO答案后,this帮助了我。

基本上尝试将PagerAdapter更改为FragmentStatePagerAdapter FragmentPagerAdapter exports.a = class Hello extends React.Component { render() { return <h1 style={style1}>Hello world,{this.props.name}</h1>; }