• viewPage


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:background="@android:color/white">
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:layout_gravity="center" >
    </android.support.v4.view.ViewPager>
    
    </LinearLayout>

    添加视图

      List<View> viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
            for(int i=0;i<5;i++){
                ImageView image=new ImageView(this);
                image.setImageResource(R.drawable.abd);
                viewList.add(image);
            }
           
             MyAdapter pageAdapter = new MyAdapter(viewList);
      ViewPager viewpage=(ViewPager)findViewById(R.id.viewpager); viewpage.setAdapter(pageAdapter);

    添加适配器

    package sdf.ess;
    
    import java.util.List;
    
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class MyAdapter extends PagerAdapter{
        List<View>  viewList;
    
        public MyAdapter(List<View> viewList2) {
            this.viewList=viewList2;
        }
    
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
              ((ViewPager)container).removeView(viewList.get(position));
        }
    
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager)container).addView(viewList.get(position));
            return viewList.get(position);
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return viewList.size();
        }
    
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0==arg1;
        }
        //添加viewpager标题用  不需要标题就不用重写
    //    @Override
    /*    public CharSequence getPageTitle(int position) {
    
            // TODO Auto-generated method stub
    
            return titles.get(position);
    
        }*/
    }
  • 相关阅读:
    【如何使用翻译工具翻译网页】英语没过关的可以参考下。
    HTTP 请求报文和响应报文
    VIM+ctags+cscope用法
    Ubuntu下命令行访问网站
    mini_httpd的安装和配置
    堆排序(代码2)
    插入排序
    堆排序(代码1)
    快速排序
    Python中的控制流
  • 原文地址:https://www.cnblogs.com/clarence/p/3693073.html
Copyright © 2020-2023  润新知