![](/img/trans.png)
[英]Executing method every second in Fragment - NULL Value on second run
[英]Passing different images every second Fragment
試圖每秒動態傳遞不同的圖像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<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
,請嘗試使用靜態靜態工廠方法作為以下代碼。
片段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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.