调用按钮单击View Pager android中的Listener

时间:2014-03-25 08:33:39

标签: android android-viewpager onclicklistener

我正在使用View寻呼机在Android中滑动页面,我的所有页面都有按钮,我想设置按钮上的点击监听器。我怎样才能做到这一点?我查了这个链接。 On Click listener is not working in View Pager但它对我不起作用。

这是我的代码:

Main.class

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        Button btnDone = (Button) findViewById(R.id.buttonalpha);
        btnDone.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_SHORT).show();



            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

ViewPager

public 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.hello;
                break;
            case 1:
                resId = R.layout.hello2;
                break;
            case 2:
                resId = R.layout.hello3;
                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 boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == ((View) arg1);

        }

        @Override
        public Parcelable saveState() {
            return null;
        }
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为最好使用FragmentPagerAdapter。

片段的例子。

public class YourFragment extends Fragment {

private OnClickListener mListener;


public static YourFragment newInstance(int layoutId){
    Bundle args = new Bundle();
    args.putInt("id", layoutId);
    YourFragment fr = new YourFragment();
    fr.setArguments(args);
    return fr;
}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    mListener = (OnClickListener)activity;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(getArguments().getInt("id"), container, false);
    view.findViewById(R.id.action_bar).setOnClickListener(mListener);


    return view;
}
}

mainActivity应该是imlement OnClickListener。

观看http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity