每秒传递不同的图像碎片
试图每秒动态传递不同的图像Fragment
,我无法在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));
布局
在此处输入图像描述
这是我询问的viewPager和fragmentList
public class PagerAdapter extends FragmentStatePagerAdapter { //Line 3 List fragmentList; //Line 4 public PagerAdapter(FragmentManager fm, List 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
传递数据Fragment
,请尝试使用静态静态工厂方法作为以下代码。
片段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; } @Override public 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));
- 权限被拒绝导致“安装因APK文件无效而失败”
- 如何在滚动时修复折叠工具栏中的视图?
- java.lang.NullPointerException:尝试在空对象引用上调用虚方法’boolean java.lang.String.equals(java.lang.Object)’
- FileDescriptor什么时候关闭?
- Android上两个应用程序之间的套接字通信
- 在4.0上使用onTouchListener和onClickListener的Android TextView NullPointerException
- 仅在Android 6.0中没有此类文件或目录
- Android从MySql表中获取数据
- 我可以在独立项目中使用android.os。*库吗?