
public Object instantiateItem(VIEwGroup collection,int position) { relativeLayout wholeVIEw = new relativeLayout(collection.getContext()); // images final ImageVIEw workoutWidget = new ImageVIEw(collection.getContext()); workoutWidget.setimageResource(R.drawable.workout_Widget_master); final Imagebutton resetbutton = new Imagebutton(collection.getContext()); resetbutton.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.reset_button_master)); relativeLayout.LayoutParams resetbuttonParams = new relativeLayout.LayoutParams(relativeLayout.LayoutParams.WRAP_CONTENT,relativeLayout.LayoutParams.WRAP_CONTENT); resetbuttonParams.addRule(relativeLayout.AliGN_PARENT_left); resetbuttonParams.addRule(relativeLayout.AliGN_PARENT_BottOM); resetbuttonParams.height = 150; resetbuttonParams.wIDth = 150; resetbutton.setLayoutParams(resetbuttonParams); resetbutton.setVisibility(VIEw.GONE); final Imagebutton startbutton = new Imagebutton(collection.getContext()); startbutton.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.start_button_master)); startbutton.setTag(context.getString(R.string.wtrStartbuttonTag)); relativeLayout.LayoutParams startbuttonParams = new relativeLayout.LayoutParams(relativeLayout.LayoutParams.WRAP_CONTENT,relativeLayout.LayoutParams.WRAP_CONTENT); startbuttonParams.addRule(relativeLayout.AliGN_PARENT_RIGHT); startbuttonParams.addRule(relativeLayout.AliGN_PARENT_BottOM); startbuttonParams.height = 150; startbuttonParams.wIDth = 150; startbutton.setLayoutParams(startbuttonParams); startbutton.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { startbutton.setAlpha(1.0f); if (startbutton.getTag() == context.getString(R.string.wtrStartbuttonTag)) { startbutton.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.pause_button_master)); startbutton.setTag(context.getString(R.string.wtrPausebuttonTag)); resetbutton.setVisibility(VIEw.VISIBLE); } else if (startbutton.getTag() == context.getString(R.string.wtrPausebuttonTag)) { startbutton.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.start_button_master)); startbutton.setTag(context.getString(R.string.wtrStartbuttonTag)); } } }); // text labels vIEw linearLayout textLabels = new linearLayout(collection.getContext()); textLabels.setorIEntation(linearLayout.VERTICAL); TextVIEw activityDescription = new TextVIEw(collection.getContext()); activityDescription.setText("warm-up"); activityDescription.setpadding(200,200,0); activityDescription.setTextSize(30); textLabels.addVIEw(activityDescription); TextVIEw timeleftForThisActivity = new TextVIEw(collection.getContext()); timeleftForThisActivity.setText("00:00"); timeleftForThisActivity.setpadding(200,0); timeleftForThisActivity.setTextSize(60); textLabels.addVIEw(timeleftForThisActivity); linearLayout elapsedLabels = new linearLayout(collection.getContext()); elapsedLabels.setorIEntation(linearLayout.HORIZONTAL); TextVIEw elapsedtimeStatic = new TextVIEw(collection.getContext()); elapsedtimeStatic.setText("Elapsed Time: "); elapsedtimeStatic.setpadding(200,0); elapsedtimeStatic.setTextSize(20); TextVIEw elapsedtimeDynamic = new TextVIEw(collection.getContext()); elapsedtimeDynamic.setText("00:00"); elapsedtimeDynamic.setpadding(0,0); elapsedtimeDynamic.setTextSize(20); elapsedLabels.addVIEw(elapsedtimeStatic); elapsedLabels.addVIEw(elapsedtimeDynamic); textLabels.addVIEw(elapsedLabels); // adding images and text to overall vIEw wholeVIEw.addVIEw(workoutWidget); wholeVIEw.addVIEw(startbutton); wholeVIEw.addVIEw(resetbutton); wholeVIEw.addVIEw(textLabels); collection.addVIEw(wholeVIEw,0); return wholeVIEw;}解决方法 它失去了状态,因为vIEwpager默认只保存上一页和下一页(屏幕外页面限制为1).如果滚动到最后一页,则第一页将被销毁. 你可以使用setoffscreenPagelimit(2)增加内存中保存的页数;
总结以上是内存溢出为你收集整理的android – 在ViewPager中滑动到新页面会导致旧页面丢失状态全部内容,希望文章能够帮你解决android – 在ViewPager中滑动到新页面会导致旧页面丢失状态所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)