每秒传递不同的图像碎片

时间:2017-11-01 19:49:14

标签: java android android-fragments

尝试每秒动态传递不同的图像onCreateView,我无法在if方法和maps语句中正确设置它。所有图像都存储在可绘制文件中。

第二个布局的名称为ImageView idandroid: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));

MainActivity

fragmentList

布局

enter image description here

以下是我提出的viewPager和 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 } }

{{1}}

那么如何使用viewPager动态更改此布局?

1 个答案:

答案 0 :(得分:1)

如果要从Fragment传递数据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;
}

@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");
}

<强> MainActivity

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));