Android UI:ListView -- SimpleAdapter

弱龄寄事外,委怀在琴书。这篇文章主要讲述Android UI:ListView -- SimpleAdapter相关的知识,希望能为你提供帮助。

SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局,而且使用很方便。

layout :
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5android:orientation="horizontal"> 6< ListView 7android:layout_width="match_parent" 8android:layout_height="wrap_content" 9android:divider="#7f00"//分割线 10android:dividerHeight="2dp" 11android:id="@+id/listview_sample"/> 12 < /LinearLayout>

header layout:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 < ImageView 6android:layout_width="match_parent" 7android:layout_height="wrap_content" 8android:src="https://www.songbingjia.com/android/@mipmap/ic_launcher"/> 9 < /LinearLayout>

 
 
自定义布局   item:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5android:orientation="horizontal"> 6< ImageView 7android:layout_width="wrap_content" 8android:layout_height="wrap_content" 9android:layout_margin="3px" 10android:id="@+id/img"/> 11< LinearLayout 12android:layout_width="match_parent" 13android:layout_height="wrap_content" 14android:orientation="vertical"> 15< TextView 16android:layout_width="match_parent" 17android:layout_height="wrap_content" 18android:textSize="16sp" 19android:id="@+id/title"/> 20< TextView 21android:layout_width="match_parent" 22android:layout_height="wrap_content" 23android:id="@+id/info" 24android:textSize="16sp"/> 25< /LinearLayout> 26 27 < /LinearLayout>

 

java 代码:
1 public class SampleAdapterActivity extends Activity { 2 3private ListView mListview; 4@Override 5protected void onCreate(Bundle savedInstanceState) { 6super.onCreate(savedInstanceState); 7setContentView(R.layout.sampleadapter_layout); 8mListview = (ListView) findViewById(R.id.listview_sample); 9SimpleAdapter adapter = new SimpleAdapter(this, 10getData(),//数据来源 11R.layout.item_listview, //对应item view 12new String[]{"img","title","info"}, //data 中对应值 13new int[]{R.id.img,R.id.title,R.id.info}); //填充layout位置 14mListview.setHeaderDividersEnabled(true); //是否显示头view 的分割线 15View header = View.inflate(this,R.layout.listview_header,null); 16View footer = View.inflate(this,R.layout.listview_header,null); 17mListview.addHeaderView(header); //添加头部view 18mListview.addFooterView(footer); //添加底部view 19mListview.setAdapter(adapter); 20} 21 22@Override 23protected void onResume() { 24super.onResume(); 25} 26private List< ? extends Map< String,?> > getData() { 27List< Map< String,Object> > items = new ArrayList< Map< String, Object> > (); 28for (int i = 0; i < 5; i++) { 29Map< String,Object> item = new HashMap< String,Object> (); 30item.put("img",R.mipmap.ic_launcher); 31item.put("title","title -- " + i ); 32item.put("info","info -- " + i ); 33items.add(item); 34} 35return items; 36} 37 }

  显示效果
 
Android UI:ListView -- SimpleAdapter

文章图片

【Android UI:ListView -- SimpleAdapter】 


    推荐阅读