试图每秒动态传递不同的图像片段,我无法在onCreateVIEw方法和if语句中正确设置它.所有图像都存储在可绘制文件中.
第二个布局的名称是maps和ImageVIEw ID是androID:ID =“@ ID / map_images”
分段
public class Fragment1 extends Fragment { String stringValue; int imagesResID; TextVIEw text; String[] rbData; RadioGroup radiobuttons; boolean mAPImage; VIEw answer; public Fragment1(String str, int imageVIEw , String[] rb, boolean arg) { this.stringValue = str; this.imagesResID = imageVIEw; this.rbData = rb; this.mAPImage = arg; } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { Log.i("x","onCreateVIEwFragment"); VIEw vIEw = inflater.inflate(R.layout.fragment_1, container, false); text = vIEw.findVIEwByID(R.ID.textVIEw); ImageVIEw imageResID = vIEw.findVIEwByID(image); answer = vIEw.findVIEwByID((R.ID.radioGroup)); ImageVIEw maps = vIEw.findVIEwByID(map_images); text.setText(stringValue); imageResID.setimageResource(imagesResID); maps. //HERE if (mAPImage) { vIEw = inflater.inflate(R.layout.maps, container, false); maps = vIEw.findVIEwByID(map_images); maps. //HERE } else { vIEw = inflater.inflate(R.layout.fragment_1, container, false); text = vIEw.findVIEwByID(R.ID.textVIEw); radiobuttons = vIEw.findVIEwByID(R.ID.radioGroup); text.setText(stringValue); imageResID.setimageResource(imagesResID); //checkBoxes, textvIEws, imagevIEws, etc } if (answer != null) { for (int i = 0; i < radiobuttons.getChildCount(); i++) { ((Radiobutton) radiobuttons.getChildAt(i)).setText(rbData[i]); } } return vIEw; }
主要活动
fragmentList = new ArrayList<>(); fragmentList.add(new Fragment1(getResources().getString(R.string.text_page_1), R.drawable.swans, new String[]{getResources().getString(R.string.answer1), getResources().getString(R.string.answer2),getResources().getString(R.string.answer3)},false)); fragmentList.add(new Fragment1(null, R.drawable.image_file, null, true)); // TALKING ABOUT THIS liNE HERE AND LATER EVERY SECOND FRAGMENT. JUST IMAGE WILL CHANGE. fragmentList.add(new Fragment1(getResources().getString(R.string.text_page_2), R.drawable.nature, new String[]{getResources().getString(R.string.answer4), getResources().getString(R.string.answer5),getResources().getString(R.string.answer6)},false));
布局
enter image description here
这是我询问的vIEwPager和fragmentList
public class PagerAdapter extends FragmentStatePagerAdapter { //line 3 List<Fragment> fragmentList; //line 4 public PagerAdapter(FragmentManager fm, List<Fragment> fragmentList) { //line 5 super(fm); //line 6 this.fragmentList = fragmentList; //line 7 } @OverrIDe public Fragment getItem(int position) { //line 8 return fragmentList.get(position); //line 9 } @OverrIDe public int getCount() { //line 10 return fragmentList.size(); //line 11 }}
那么如何使用vIEwPager动态更改此布局?
解决方法:
如果要从Activity传递数据片段,请尝试使用静态静态工厂方法作为以下代码.
片段1
public static Fragment newInstance(String str, int imageVIEw , String[] rb, boolean arg) { Fragment fragment = new Fragment1(); Bundle args = new Bundle(); args.putString("str", str); args.putInt("image_resID", imageVIEw); args.putStringArray("rb", rb); args.putBoolean("arg", arg); fragment.setArguments(args); return fragment;}@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle args = getArguments(); stringValue = args.getString("str"); imagesResID = args.getInt("image_resID"); rbData = args.getStringArray("rb"); mAPImage = args.getBoolean("arg");}
主要活动
fragmentList = new ArrayList<>();fragmentList.add(Fragment1.newInstance(getResources().getString(R.string.text_page_1), R.drawable.swans, new String[]{getResources().getString(R.string.answer1), getResources().getString(R.string.answer2),getResources().getString(R.string.answer3)},false));fragmentList.add(Fragment1.newInstance(null, R.drawable.image_file, null, true)); // TALKING ABOUT THIS liNE HERE AND LATER EVERY SECOND FRAGMENT. JUST IMAGE WILL CHANGE.fragmentList.add(Fragment1.newInstance(getResources().getString(R.string.text_page_2), R.drawable.nature, new String[]{getResources().getString(R.string.answer4), getResources().getString(R.string.answer5),getResources().getString(R.string.answer6)},false));
总结 以上是内存溢出为你收集整理的java – 每秒传递不同的图像片段全部内容,希望文章能够帮你解决java – 每秒传递不同的图像片段所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)