ssummeraw 2013-01-14 06:52 采纳率: 10%
浏览 6397
已采纳

在 viewpager 中如何实现 Button 的 onClick 方法

我想在viewpaper中,点击按钮弹出Toast信息,并且想知道如何在viewpaper上访问views?
我用的以下的代码,不好用。

public class MyPagerAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Object instantiateItem(final View collection, final int position) {
         v = new View(collection.getContext());
        LayoutInflater inflater =
                (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.cate1;
            v = inflater.inflate(R.layout.cate1, null, false);
            add1 = (Button) v.findViewById(R.id.btnAdd);
            add1.setOnClickListener( new OnClickListener() {
                public void onClick(View m) {
                   Toast.makeText(collection.getContext(),"click",Toast.LENGTH_LONG).show();
                }
            });


            break;
        case 1:
            resId = R.layout.cate2;
            break;
        case 2:
            resId = R.layout.cate3;
            break;
        }

        View view = inflater.inflate(resId, null);
        ((ViewPager) collection).addView(view, 0);

        return view;
    }

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

    }

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

    }

    @Override
    public void finishUpdate(View arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void startUpdate(View arg0) {
        // TODO Auto-generated method stub

    }
        }

下面是 button onclick代码,也没有实现

v = inflater.inflate(R.layout.cate1, null, false);
            add1 = (Button) v.findViewById(R.id.btnAdd);
            add1.setOnClickListener( new OnClickListener() {
                public void onClick(View m) {
                   Toast.makeText(collection.getContext(),"click",Toast.LENGTH_LONG).show();
                }
            });

请求大家的帮忙,看看如何处理这个问题。

  • 写回答

2条回答

  • hxn_217 2013-01-22 03:34
    关注

    把你代码中的

    ((ViewPager) collection).addView(view, 0);
    
        return view;
    

    改为

    ((ViewPager) collection).addView(v, 0);
    
        return v;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog