1

好的,所以我有一个带有导航抽屉的应用程序。抽屉中的每个菜单项都会打开一个不同的片段,我向用户显示它并且效果很好。

其中一个片段 (DashboardClientFragment) 在其 onCreateView() 方法中调用了 Web 服务,然后填充 ViewPager。在第一次运行时,它会加载数据并显示得很好。

当我打开抽屉并单击再次打开此片段的同一项目时,问题就开始了。片段加载,我对 web 服务的调用被再次调用(它显示了来自服务的 json 响应),但是 viewpager 是空的。

下面是一些问题的截图:

屏幕一 - 一切都很好

屏幕二 - 打开导航抽屉并单击仪表板菜单项

屏幕三 - 错误

这是该活动的精简版。

public class DashboardActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

    if (savedInstanceState == null) {
        displayView(1, false);
    }
}

public void displayView(int id, boolean addBackStack) {
    Fragment fragment = null;

    switch (id) {
    case 1:
        fragment = new DashboardClientFragment();
        break;
    case 2:
        fragment = new ClientsFragment();
        break;
    case 5:
        fragment = new StorefrontPreviewFragment();
        break;
    case 6:
        new SessionManager(this).logoutUser();
        startActivity(new Intent(this, MainActivity.class));
        finish();
        break;

    default:
        break;
    }

    if (fragment != null) {
        drawer.closeDrawers();

        FragmentManager fragmentManager = getSupportFragmentManager();
        if (addBackStack)
            fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).addToBackStack(null).commit();
        else
            fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
    }
}


}

这是我的片段(完整代码)

public class DashboardClientFragment extends Fragment implements OnClickListener {

private ViewPager pager;
private TextView add_booking;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_dashboard_client, container, false);

    ((DashboardActivity) getActivity()).setSelectedMenu(1);
    ((DashboardActivity) getActivity()).setBarTitle("Dashboard");

    pager = (ViewPager) v.findViewById(R.id.pager);
    add_booking = (TextView) v.findViewById(R.id.add_booking);

    add_booking.setOnClickListener(this);

    final Map<String, String> params = new HashMap<String, String>();
    params.put("auth", DashboardActivity.user.get(SessionManager.AUTH));

    Log.d("params", params.toString());

    StringRequest dash = new StringRequest(
            Method.POST,
            G.DOMAIN + "index/rest/dashboard",
            new Listener<String>() {

                @Override
                public void onResponse(String json) {

                    Log.d("json", json);

                    DashboardPagerAdapter pagerAdapter = new DashboardPagerAdapter(getActivity().getSupportFragmentManager(), json);
                    pager.setAdapter(pagerAdapter);
                }
            },
            new ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("error", "Error: " + error.getMessage());
                }
            }) {
        @Override
        protected Map<String, String> getParams() {
            return params;
        }
    };

    AppController.getInstance().addToRequestQueue(dash, null);

    return v;
}

@Override
public void onClick(View v) {
    if (v == add_booking) {
        startActivity(new Intent(getActivity(), BookingActivity.class));
    }
}

}
4

1 回答 1

3

您应该使用 getChildFragmentManager()

代替:

DashboardPagerAdapter pagerAdapter = new DashboardPagerAdapter(getActivity().getSupportFragmentManager(),
json);
pager.setAdapter(pagerAdapter);

和:

DashboardPagerAdapter pagerAdapter = new DashboardPagerAdapter(getChildFragmentManager,
json);
pager.setAdapter(pagerAdapter);

如果它有效,现在让我;)

于 2014-06-18T09:33:31.507 回答