尝试每秒动态传递不同的图像Fragment
,但我无法在onCreateView
方法和if
语句中正确设置它。所有图像都存储在可绘制文件中。
第二个布局的名称是 maps
,ImageView id
是 android:id="@+id/map_images"
fragment
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;
}
主要 Activity
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<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
传递数据Fragment
,请尝试使用静态静态工厂方法,如下代码。
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");
}
主要 Activity
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 - 每秒传递不同的图像 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47062603/