android viewpager中怎么监听按钮事件,求源码~~!!
1个回答
展开全部
public class ViewPagerActor extends LinearLayout{
private ViewPager viewPager;
private MyAdapter myAdapter;
private List<View> lists;
public ViewPagerActor(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater =LayoutInflater.from(context);
inflater.inflate(R.drawable.actor_viewpager_layout, this, true);
viewPager = (ViewPager)findViewById(R.id.viewpager);//这个滚动页面
}
public void setDates(List<View> lists){
this.lists = lists;
}
public void initDisplay(){
myAdapter = new MyAdapter(lists);
viewPager.setAdapter(myAdapter);
}
public void notifyDataSetChanged(){
myAdapter.notifyDataSetChanged();
}
/**
* 设置显示第几页
* @param count
*/
public void setCurrentItem(int count){
viewPager.setCurrentItem(count);
}
/**
* 为 viewPager 添加换页监听器
* @param onPageChangeListener
*/
public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener){
viewPager.setOnPageChangeListener(onPageChangeListener);
}
/**
* viewPager 固定的适配器 继承PagerAdapter
* @author Administrator
*
*/
class MyAdapter extends PagerAdapter {
private List<View> views;
public MyAdapter(List<View> lists){
this.views = lists;
}
@Override
public int getCount() {
return views.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
if(views.get(arg1)!=null){
((ViewPager)arg0).removeView(views.get(arg1));
}
}
@Override
public Object instantiateItem(View arg0, int arg1) {
((ViewPager)arg0).addView(views.get(arg1),0);
return views.get(arg1);
}
}
}
上面的类你可以保存下来了 那是个通用的滑动切屏
将上面的类 注入到你要运行的activity类的xml中
在activity中
View compassView = makeCompassView();第一个页面
compassView.setOnClickListener(this.addUueeyeListener);//这个就是加监听器
如果你想给这个view中的别的控件加监听一样 直接掉例如
compassView.findViewBy().set监听
View dataView = makeDataView();第二个页面
this.viewDatas.add(compassView);
this.viewDatas.add(dataView);
this.viewPagerActor.setDates(this.viewDatas);赋值给上面滑动中
this.viewPagerActor.initDisplay();//上面有这个方法你自己看
希望你能看懂
private ViewPager viewPager;
private MyAdapter myAdapter;
private List<View> lists;
public ViewPagerActor(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater =LayoutInflater.from(context);
inflater.inflate(R.drawable.actor_viewpager_layout, this, true);
viewPager = (ViewPager)findViewById(R.id.viewpager);//这个滚动页面
}
public void setDates(List<View> lists){
this.lists = lists;
}
public void initDisplay(){
myAdapter = new MyAdapter(lists);
viewPager.setAdapter(myAdapter);
}
public void notifyDataSetChanged(){
myAdapter.notifyDataSetChanged();
}
/**
* 设置显示第几页
* @param count
*/
public void setCurrentItem(int count){
viewPager.setCurrentItem(count);
}
/**
* 为 viewPager 添加换页监听器
* @param onPageChangeListener
*/
public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener){
viewPager.setOnPageChangeListener(onPageChangeListener);
}
/**
* viewPager 固定的适配器 继承PagerAdapter
* @author Administrator
*
*/
class MyAdapter extends PagerAdapter {
private List<View> views;
public MyAdapter(List<View> lists){
this.views = lists;
}
@Override
public int getCount() {
return views.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
if(views.get(arg1)!=null){
((ViewPager)arg0).removeView(views.get(arg1));
}
}
@Override
public Object instantiateItem(View arg0, int arg1) {
((ViewPager)arg0).addView(views.get(arg1),0);
return views.get(arg1);
}
}
}
上面的类你可以保存下来了 那是个通用的滑动切屏
将上面的类 注入到你要运行的activity类的xml中
在activity中
View compassView = makeCompassView();第一个页面
compassView.setOnClickListener(this.addUueeyeListener);//这个就是加监听器
如果你想给这个view中的别的控件加监听一样 直接掉例如
compassView.findViewBy().set监听
View dataView = makeDataView();第二个页面
this.viewDatas.add(compassView);
this.viewDatas.add(dataView);
this.viewPagerActor.setDates(this.viewDatas);赋值给上面滑动中
this.viewPagerActor.initDisplay();//上面有这个方法你自己看
希望你能看懂
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询