发布网友 发布时间:2022-04-26 15:40
共1个回答
热心网友 时间:2022-05-03 07:15
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();//上面有这个方法你自己看 希望你能看懂