所以我使用 FragmentStatePagerAdapter 实现了 ViewPager ,我有2个片段。我已经使用参数1和2调用了setOffscreenPageLimit(),希望不会重新创建片段,但是第一个片段总是被重新创建,而第二个片段被创建了。
第一个片段总是经过这些步骤,只要它不在视图范围内(向右滑动到另一个片段,或点击主页按钮等)。
由于某种原因,第二个片段只创建一次,如果我滑动到第一个片段,它会按预期运行 - 除非我按下主页按钮或电源按钮,否则不会重新创建它。 我想要的行为是在应用程序的持续时间内创建两个片段。
我的片段结构如下:
我有一个FrameLayout活动。我将SlidingTabFragment添加到FrameLayout。 SlidingTabFragment本身包含两个片段(2个标签)
FragmentStatePagerAdapter
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
/** Constructor of the class */
public TabsPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.mContext = context;
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
switch(arg0){
/** tab1 is selected */
case 0:
Fragment1 frag1 = new Fragment1();
return frag1;
/** tab2 is selected */
case 1:
Fragment2 frag2 = new Fragment2();
return frag2;
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
return Settings.TAB_TITLES[position];
}
/** Returns the number of pages */
@Override
public int getCount() {
return Settings.TAB_TITLES.length;
}
}
父片段:
public class SlidingTabFragment extends Fragment {
ViewPager pager = null;
TabsPagerAdapter adapter = null;
SlidingTabLayout tabs = null;
public static SlidingTabFragment newInstance() {
SlidingTabFragment fragment = new SlidingTabFragment();
return fragment;
}
public SlidingTabFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
adapter = new TabsPagerAdapter(getChildFragmentManager(), getActivity());
pager = (ViewPager) view.findViewById(R.id.pager);
pager.setAdapter(adapter);
pager.setOffscreenPageLimit(2);
// Assigning the Sliding Tab Layout View
tabs = (SlidingTabLayout) view.findViewById(R.id.sliding_layout);
tabs.setCustomTabView(R.layout.custom_tab_view, 0);
tabs.setDistributeEvenly(true);
// Setting indicator color to white
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.White);
}
});
tabs.setViewPager(pager);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_sliding_tab, container, false);
}
}
R.layout.fragment_sliding_tab
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_series_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.myapp.views.SlidingTabLayout
android:id="@+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="@color/tab_bar_background_color"
/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
答案 0 :(得分:1)
将此添加到您的片段中:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// load data here
}else{
// fragment is no longer visible
}
}