Android Viewpager item access

冷暖自知 提交于 2019-12-06 07:29:05

Try This:

View v = inflater.inflate(resId, null);
ImageView img = v.findviewbyid(R.id.IMAGEID);
// do what you want with the image

put the code in the switch case and return from there for the best result

i.e.: move the following code to each case

    View v = inflater.inflate(resId, null);

    ((ViewPager) collection).addView(v, 0);

    return v;

Shereef is right. Lets say you have two text views. In your R.layout.training_topics layout. And one image view on your R.layout.tricks_topics. And you use PageAdapter, your code should look like this....

    int resId = 0;
        View v = null;
        switch (position) {
        case 0:
            resId = R.layout.training_topics;
            v = inflater.inflate(resId, null);
            View tv = v.findViewById(R.id.text1);
            View tv2 = v.findViewById(R.id.text2);
            ((TextView)tv).setText("testtesttes");
            ((TextView)tv2).setText("sttesttes2");
            break;
        case 1:
            resId = R.layout.behaviour_topics;
            v = inflater.inflate(resId, null);
            break;
        case 2:
            resId = R.layout.tricks_topics;
            v = inflater.inflate(resId, null);
            View im = v.findViewById(R.id.image1);
            ((ImageView)im).setBackgroundResource(R.drawable.icon);

            break;
        }



        ((ViewPager) collection).addView(v, 0);

        return v;

But don't be confuesd by my

    View im = v.findViewById(R.id.image1);
    ((ImageView)im).setBackgroundResource(R.drawable.icon);

you can still use

    ImageView im = v.findViewById(R.id.image1);
    im.setBackgroundResource(R.drawable.icon);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!