我需要在viewpager中保留先前片段的状态。所以我为viewPager设置了ScreenLimit。当我设置OffScreenLimit时,页面加载滞后。
动态创建多个片段。这些是我初始化片段的方式。
<%= javascript_include_tag 'students', 'data-turbolinks-track' => true %>
以上用于初始化片段的代码。 在这里设置offScreenLimit
public class DynamicFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static Map map;
private static DynamicFragment f;
private JSONArray radiodata;
Window window;
private String[] radioName;
public static RadioGroup radioGroup;
DynamicViews dynamicviews;
Page2Info[] page2Infos = new Page2Info[10];
private int dynamicViewId;
View rootView;
private HashMap<Integer, Page2Info> infomap;
private static HashMap hashMap;
static String selitem;
private static Spinner spinner;
static EditText myEditTxt;
private JSONArray spinnerdata;
private View view;
static Context mContext;
private int maxLength = 10;
boolean _areLecturesLoaded = false;
private int data;
private FragmentManager fragmentManager;
MyPageAdapter myPageAdapter ;
public static final DynamicFragment newInstance(String message) {
f = new DynamicFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
/*
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
PageViewActivity pageViewActivity = new PageViewActivity();
List<Fragment> fragments = pageViewActivity.getFragments();
myPageAdapter = new PageViewActivity.MyPageAdapter(getChildFragmentManager(),fragments);
}
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
rootView = inflater.inflate(R.layout.myfragment_layout1, container, false);
ImageView dynamicImage = (ImageView) rootView.findViewById(R.id.dynamic_image);
map = HomeFrag.dynamicPagesHashMap;
dynamicviews = new DynamicViews();
mContext = getActivity();
infomap = new HashMap<Integer, Page2Info>();
hashMap = new HashMap();
LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.dynamic_layout);
LinearLayout topLayout = (LinearLayout) rootView.findViewById(R.id.toplayout);
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
int id = (int) pair.getKey();
DynamicPages dynamicPages[] = (DynamicPages[]) pair.getValue();
String headings = dynamicPages[id].getHeadings();
String properties = dynamicPages[id].getProperties();
String clientData = dynamicPages[id].getClientData();
String detractor = dynamicPages[id].getDetractor();
String passive = dynamicPages[id].getPassive();
String promoter = dynamicPages[id].getPromoter();
String categories = dynamicPages[id].getCategories();
String scale = dynamicPages[id].getScale();
String scaleType = dynamicPages[id].getScaletype();
String questionnaire = dynamicPages[id].getQuestionnaire();
String commentBox = dynamicPages[id].getComment();
String pageName = dynamicPages[id].getPageName();
String listParam = null;
String category = dynamicPages[id].getCategory();
String dataclient = dynamicPages[id].getDataclient();
String paramViews = dynamicPages[id].getParamViews();
if(pageName.equalsIgnoreCase("parameterizedpage")){
listParam = dynamicPages[id].getList();
}
if (message.equalsIgnoreCase("Fragment" + id + "")) {
switch (pageName){
case "welcomepage" :
LayoutInflater layoutWelcome = LayoutInflater.from(getActivity());
DisplayWelcomePage displayWelcomePage = new DisplayWelcomePage(layoutWelcome);
view = displayWelcomePage.createWelcomePage(getActivity(),container,headings,properties);
topLayout.addView(view);
break;
case "personalinfo":
Constants.REQUIRED_FIELD = "On";
LayoutInflater layoutPersonalInfo = LayoutInflater.from(getActivity());
DisplayPersonalInfo displayPersonalInfo = new DisplayPersonalInfo(layoutPersonalInfo);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
displayPersonalInfo.setResources(getResources());
view = displayPersonalInfo.createInfoPage(getActivity(),container,clientData,headings,properties);
topLayout.addView(view);
break;
case "scale":
Constants.SCALE_REQUIRED = "On";
LayoutInflater layoutScale = LayoutInflater.from(getActivity());
if (scaleType.equalsIgnoreCase("NPS")) {
scale = "0";
DisplayNpsScale displayNpsScale = new DisplayNpsScale(layoutScale);
view = displayNpsScale.CreateScale(scale, container, getContext(),headings,properties);
displayNpsScale.setResource(getResources());
topLayout.addView(view);
break;
} else {
DisplayCsatScale displayCsatScale = new DisplayCsatScale(layoutScale);
view = displayCsatScale.CreateScale(scale, container, getContext(),headings,properties);
displayCsatScale.setResources(getResources());
topLayout.addView(view);
break;
}
case "questionnaire":
Constants.CAMPAIGN_DETRACTOR = detractor;
Constants.CAMPAIGN_PROMOTER = promoter;
Constants.CAMPAIGN_PASSIVE = passive;
// Constants.CAMPAIGN_DATA_CAT = category;
LayoutInflater layoutList = LayoutInflater.from(getActivity());
DisplayExpanList displayExpanList = new DisplayExpanList(layoutList);
view = displayExpanList.createList(container, getContext(),headings,properties,category,dataclient,paramViews);
topLayout.addView(view);
break;
case "questionnaire2":
Constants.CAMPAIGN_DETRACTOR = detractor;
Constants.CAMPAIGN_PROMOTER = promoter;
Constants.CAMPAIGN_PASSIVE = passive;
// Constants.CAMPAIGN_DATA_CAT = category;
LayoutInflater layoutList2 = LayoutInflater.from(getActivity());
DisplayExpanList2 displayExpanList2 = new DisplayExpanList2(layoutList2);
view = displayExpanList2.createList(container, getContext(),headings,properties,category,dataclient,paramViews);
topLayout.addView(view);
break;
case "questionnaire3":
Constants.CAMPAIGN_DETRACTOR = detractor;
Constants.CAMPAIGN_PROMOTER = promoter;
Constants.CAMPAIGN_PASSIVE = passive;
// Constants.CAMPAIGN_DATA_CAT = category;
LayoutInflater layoutList3 = LayoutInflater.from(getActivity());
DisplayExpanList3 displayExpanList3 = new DisplayExpanList3(layoutList3);
view = displayExpanList3.createList(container, getContext(),headings,properties,category,dataclient,paramViews);
topLayout.addView(view);
break;
case "commentbox":
LayoutInflater layoutComment = LayoutInflater.from(getActivity());
DisplayCommentBox displayCommentBox = new DisplayCommentBox(layoutComment);
view = displayCommentBox.createCommentBox(container, getContext(), headings, properties);
topLayout.addView(view);
break;
case "thankyoupage":
Constants.THANKYOUPAGE_HEADING = headings;
Constants.THANKYOUPAGE_PROPERTIES = properties;
break;
case "CRM" :
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
DisplayCRM displayCRM = new DisplayCRM(layoutInflater);
view = displayCRM.createCRM(container,getContext());
topLayout.addView(view);
break;
case "parameterizedpage":
LayoutInflater layoutParamPage = LayoutInflater.from(getActivity());
DisplayParameterizedViews displayParameterizedViews = new DisplayParameterizedViews(layoutParamPage);
displayParameterizedViews.setResources(getResources());
window = getActivity().getWindow();
displayParameterizedViews.setWindow(getActivity().getWindow());
view = displayParameterizedViews.createParameterizedViews(container, getContext(), headings,listParam,properties,window );
topLayout.addView(view);
break;
}
}
}
//messageTextView.setText(message);
return rootView;
}
@Override
public void onResume() {
super.onResume();
}
}
滞后是从第2页移到第3页。