1

我正在成功使用寻呼机适配器,但想知道如何为每个单独的“页面”添加点击监听器?然后它将使用意图启动一个活动。有 5 个页面,每个页面都有自己的 xml 文件。这是代码:

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

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



private class MyPagerAdapter extends PagerAdapter {



        public int getCount() {
                return 5;
        }

        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.farleft;
                        break;
                case 1:
                        resId = R.layout.left;
                        break;
                case 2:
                        resId = R.layout.middle;
                        break;
                case 3:
                        resId = R.layout.right;
                        break;
                case 4:
                        resId = R.layout.farright;
                        break;
                }

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

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

                return view;
        }



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

        }

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

        }

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

        }

        @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

        }

}

}

4

1 回答 1

4

只需使用这个

view.setOnClickListener()
于 2012-04-25T19:23:13.680 回答