PagerSlidingStrip也在加载另一个片段

时间:2016-02-23 05:02:43

标签: android android-fragments pagerslidingtabstrip

我已经实施了Pager Sliding Strip,它工作得很好。

我有5个标签,每个标签包含不同的片段。当我点击第二个标签加载相应的片段时,它还会加载第三个片段的详细信息,它显示我在第三个标签片段中编码的吐司。

这完全破坏了应用程序的性能,为什么我要解决这个问题,我不知道它们究竟发生了什么。

我正在开展一个非常大的项目,我无法发布整个代码。

请帮助我,我会按要求发布相应的代码。

问候。

修改

正在加载另一个片段的通知片段

    ListView notification_listview;
ArrayList<FragmentNotificationDTO> arraylist;
NotificationAdapter adapter;
private static View notificationView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    if (notificationView != null) {
        ViewGroup parent = (ViewGroup) notificationView.getParent();
        if (parent != null)
            parent.removeView(notificationView);
    }
    notificationView = inflater.inflate(R.layout.fragment_notification,
            container, false);
    initLayout(notificationView);
    loadData();
    return notificationView;
}

private void initLayout(View view) {
    notification_listview = (ListView) view.findViewById(R.id.notification_listview);
    arraylist = new ArrayList<FragmentNotificationDTO>();
    adapter = new NotificationAdapter(mContext, arraylist);
    notification_listview.setAdapter(adapter);

}

这是Food Fragment,当我们点击Notification Tab

时也会加载
  private static View foodlistView;
RelativeLayout rl_foodshare, rl_foodrequested, rl_foodkitchen;
TextView tv_foodshare, tv_foodrequested, tv_foodkitchen;
ImageView img_foodshare, img_foodrequested, img_foodkitchen;
Fragment fr;
FragmentManager fm;
FragmentTransaction fragmentTransaction;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    if (foodlistView != null) {
        ViewGroup parent = (ViewGroup) foodlistView.getParent();
        if (parent != null)
            parent.removeView(foodlistView);
    }
    foodlistView = inflater.inflate(R.layout.fragment_location_list,
            container, false);
    initLayout(foodlistView);
    ChangeFragment(1);
    return foodlistView;
}

MainActivity.java包含PagerSlidingStrip&amp;查看寻呼机

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()));

    PagerSlidingTabStrip PGSTRIP = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    PGSTRIP.setViewPager(viewPager);
    viewPager.setOffscreenPageLimit(0);
}

1 个答案:

答案 0 :(得分:0)

我通过在每个片段中覆盖此方法解决了我的问题,然后在片段的可见性对用户为真时调用加载方法。

    @Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        // load data here
    }else{
        // fragment is no longer visible
    }
}