每秒传递不同的图像碎片 [英] Passing different images every second Fragment

查看:114
本文介绍了每秒传递不同的图像碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to pass dynamically different image every second Fragment and I have trouble to set it properly in onCreateView method and if statement. All images are stored in drawable file.

第二个布局的名称是 maps ImageView id android:id =@ + id / map_images

Name for this second layout is mapsand ImageView id is 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;
    }

MainActivity

MainActivity

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 你问的是什么

Here is my viewPager and fragmentList you ask about

    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的数据?

So how to change dynamically this layout with data using a viewPager?

推荐答案

如果你想传递数据 Fragment 活动,尝试使用静态静态工厂方法作为以下代码。

If you want to pass data Fragment from Activity, try to use static static factory method as the following codes.

Fragment1

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

这篇关于每秒传递不同的图像碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆