Android Viewpager item access

荒凉一梦 提交于 2019-12-12 09:59:12

问题


my aim is to be able to swipe through 3 different layouts, and be able to click items on each layout. At the moment the swiping is working well and all 3 layouts can be viewed.

Activity:

    public class FetchMenu extends Fetch {

        protected ImageView block;  

        /** Called when the activity is first created. */
         @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                //pagerviwer

                MyPagerAdapter adapter = new MyPagerAdapter();
                ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
                myPager.setAdapter(adapter);
                myPager.setCurrentItem(1); 

                //icon on layout 1

            fbicon = (ImageView)findViewById(R.id.facebookicon);            
            fbicon.setOnTouchListener(new OnTouchListener()
            {

                @Override
                public boolean onTouch(View v, MotionEvent event)
                {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/pages/Fetch-Training/174358202669554"));
                    startActivity(browserIntent);                   
                    // TODO Auto-generated method stub
                    return false;
                }
           });

PageAdapter:

class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 3;
    }

    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.training_topics;
            break;
        case 1:
            resId = R.layout.behaviour_topics;

            break;
        case 2:
            resId = R.layout.tricks_topics;
            break;
        }

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

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

        return v;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);

    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);

    }

    @Override
    public Parcelable saveState() {
        return null;
    }
}

The Problem: Each of the layouts that get swiped through contains a bunch of ImageViews, if I set up code in my Activity that listens to touches of them imageviews, I get a force close error. I'm guessing that the code in the activity does not know in which layout the ImageView is stored? I have read about fragments is this what I need?

Thanks guys always love the help


回答1:


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;



回答2:


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


来源:https://stackoverflow.com/questions/9043569/android-viewpager-item-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!