1

I am having two pages in ViewPager.In the two pages I am having buttons for which i have to make action on them.On click listener for button is working perfectly fine for second page and it is not working for first page. Here is the activity and adapter class which i have tried

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


}

And the adapter class is as follows

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

    }

}
4

3 回答 3

2

如果你想捕捉按钮点击并做某事,最好以最简单的方式来做......
更少的代码更好!
开始了;
在按钮组件(或任何组件)的 layout.xml 中应该有这个

android:onClick="onClick_function"  

然后你可以在你的代码中自动捕获这个事件,你只需要一个同名的函数

public void onClick_function(View v) {
// here we go!
}
于 2013-07-25T09:54:34.230 回答
1

我面临同样的问题,在我的情况下,它通过在 instanceiteItem 中设置单击侦听器来解决。你可以做这样的事情可能会有所帮助

click.setOnClickListener(new OnClickListener() {

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

        }
    });

或者

你也可以试试这个

 click.setOnClickListener(act);
于 2013-07-25T09:33:19.360 回答
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();
于 2013-07-25T09:23:32.130 回答