自定义Adapter及其实例

  Android界面中有时候需要显示稍微复杂的界面时,就需要我们自定义一个adapter,而此adapter就要继承BaseAdapter,重新其中的方法.

        Android中Adapter类其实就是把数据源绑定到指定的View上,然后再返回该View,而返回来的这个View就是ListView中的某一行item。这里返回来的View正是由我们的Adapter中的getView方法返回的。这样就会容易理解数据是怎样一条一条显示在ListView中的。
       在完成这篇文章中的例子之后,我思考了很长时间,关于重写一个adapter,这其中真的有很多讲究,遇到一处不懂的都会查阅很长时间,但也不能保证我已经把其中的重中之重已经找完了,只要你想延伸都可以发现其中的无限.....
       问题1:为什么我们要重写一个adapter?
       问题2:android中这么多adapter,什么情况下该重写哪一个adapter(ArrayAdapter/SimpleAdapter/SimpleCursorAdapter...)?
       问题3:我们重写的adapter为什么是一个内部类,是否建议把adapter做成一个内部类?
       问题4:理解应用中的数据源一般都会用一个Map类型的List,有何意途?
       问题5:通过adapter是怎样做到把一条一条的item放到ListView中的?
       问题6:理解重写adapter时,重写的几个方法(getCount()/getItem()/getItemId()/getView())?
       问题7:理解ListView使用adapter的机制
      
案例概览步骤:

1)创建2个layout,一个是界面顶部的显示,一个是ListView中的内容

   1.1)ListView_header.xml(注:自我感觉界面中的控件的位置摆放与layout_width/layout_height/orientation/gravity/layout_gravity的属性设置关系非常大,一定要注意每个属性的用法)

 




    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    
    
    
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal">
        
     
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textColor="#ff9966ff"
         android:textSize="25dp"
         android:text="@string/places"/>
     
     
     
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/quna"/>
    
    
    
         android:layout_height="wrap_content"
         android:text="@string/placesList"
         android:textColor="#ff9966ff"
         android:textSize="20dp"/>
    
    
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    


 

1.2)listview_item.xml

 



    android:orientation="horizontal" //此属性的设置非常重要,决定里面的控件横向摆放
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"/>
    //也很重要决定里面的两个textview竖着放
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ff3399ff"
            android:textSize="20dp"/>
        
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ff3399ff"
            android:textSize="13dp"/>
    
    
    
        android:layout_height="wrap_content"
        >
    

 

  
运行结果:
 
 

转载于:https://www.cnblogs.com/tutuha/p/4710335.html

你可能感兴趣的:(自定义Adapter及其实例)