On Click侦听器在View Pager中不起作用

时间:2013-07-25 09:11:51

标签: android android-viewpager

我在ViewPager中有两个页面。在这两个页面中,我有按钮,我必须对它们进行操作。点击监听器按钮对于第二页工作得非常好,并且它不适用于第一页。 这是我尝试过的活动和适配器类

    public class MainActivity extends Activity {
    int noofsize = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this,
                noofsize);
        ViewPager myPager = (ViewPager) findViewById(R.id.reviewpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
    }


}

适配器类如下

    public class ViewPagerAdapter extends PagerAdapter implements OnClickListener {
    int size;
    Activity act;
    View layout;
    TextView pagenumber;
    Button click;

    public ViewPagerAdapter(MainActivity mainActivity, int noofsize) {
        // TODO Auto-generated constructor stub
        size = noofsize;
        act = mainActivity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return size;
    }

    @Override
    public Object instantiateItem(View container, int position) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = inflater.inflate(R.layout.pages, null);
        pagenumber = (TextView) layout.findViewById(R.id.pagenumber);
        pagenumber.setText("Now your in Page"+position);
        click = (Button) layout.findViewById(R.id.click);
        click.setOnClickListener(this);
        ((ViewPager) container).addView(layout, 0);
        return layout;
    }

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

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == click) {
            Toast.makeText(act, "click event called", Toast.LENGTH_SHORT)
                    .show();
        }

    }

}

3 个答案:

答案 0 :(得分:2)

如果您想抓住按钮点击并执行某些操作,最好以最简单的方式执行此操作...
更少的代码更好!
我们走了;
在按钮组件的layout.xml中(或任何组件)应该有这个

android:onClick="onClick_function"  

然后您可以在代码中自动捕获此事件,只需拥有一个具有相同名称的函数

即可
public void onClick_function(View v) {
// here we go!
}

答案 1 :(得分:1)

我面对同样的问题,在我的例子中,它通过在instantiateItem中设置点击监听器来解决。你可以做这样的事情可能会有所帮助

click.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

你也可以尝试这个

 click.setOnClickListener(act);

答案 2 :(得分:0)

我认为这部分是错误的:

if (v == click) { // What is click ?! you can't compare that
    Toast.makeText(act, "click event called", Toast.LENGTH_SHORT)
            .show();
}

将其替换为:

if (v.getId() == R.id.theIdOfTheView) {
    Toast.makeText(act, "click event called", Toast.LENGTH_SHORT)
            .show();
}

instantiateItem

中使用此功能
container.onClickListener();