zoukankan      html  css  js  c++  java
  • Android:自定义适配器

    无论是ArrayAdapter还是SimpleAdapter都继承了BaseAdapter,自定义适配器同样继承BaseAdapter

    实例:Gallery实现图片浏览器

    <?xml version="1.0" encoding="utf-8"?>
     <Gallery
      xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        
    
    </Gallery>
    public class MainActivity extends Activity {
    private Gallery gallery;
    private int[] res={R.drawable.ic_launcher,R.drawable.ic_launcher};
    private ImageAdapter adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.demo);
    
            gallery = (Gallery) findViewById(R.id.gallery);
            
           adapter = new ImageAdapter(res, this);
            gallery.setAdapter(adapter);
            
            
            
        }
    
    
    }

    自定义的适配器

    public class ImageAdapter  extends BaseAdapter{
        public int res[];
        private Context context;
        public ImageAdapter(int res[],Context context){
            this.res=res;
            this.context=context;
        }
        
        @Override
        //返回已定义数据源总数量
        public int getCount() {
            // TODO Auto-generated method stub
            return res.length;
        }
    
        @Override
        //告诉适配器取得目前容器中的数据对象
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        //告诉适配器取得目前容器中的数据ID
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        //取得当前欲显示的图像View
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ImageView image=new ImageView(context);
            image.setBackgroundResource(res[position]);
            image.setLayoutParams(new Gallery.LayoutParams(400,300));
            image.setScaleType(ScaleType.FIT_XY);
            return image;
        }
    
    }
  • 相关阅读:
    SQL Server 复制订阅
    杂谈经验与未来
    泛泰A820L (高通MSM8660 cpu) 3.4内核的CM10.1(Android 4.2.2) 測试版第二版
    hdu1280 前m大的数(数组下标排序)
    Design Pattern Adaptor 适配器设计模式
    ssh命令、ping命令、traceroute 命令所使用的协议
    Android禁止ViewPager的左右滑动
    推荐一款优雅的jquery手风琴特效
    vijos
    iOS 7 UI 过渡指南
  • 原文地址:https://www.cnblogs.com/tinyphp/p/3919800.html
Copyright © 2011-2022 走看看