本文介绍了Android的Viewpager项目访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是能够通过3种不同的布局来刷卡,并能够点击每个版面项目。目前刷卡运作良好,所有3个布局可以查看。

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.

活动:

    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:

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

的问题:每一个获得刷卡通过,包含了一些ImageViews的,如果我在我的活动监听到他们imageviews触摸设置code中的布局,我得到一个强制关闭错误。我猜测,在code的活动不知道在哪个布局中的ImageView存储?我看了一下片段是这样的我需要什么?

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?

谢谢你们一直爱帮助

推荐答案

Shereef是正确的。比方说你有两个文本视图。在您的R.layout.training_topics布局。并在您的R.layout.tricks_topics一个图像视图。并且使用PageAdapter,你的code应该是这样的....

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;

但不要被confuesd我

But don't be confuesd by my

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

您仍然可以使用

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

这篇关于Android的Viewpager项目访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:18