ListView
作用:1.将資料填充到布局。2.處理使用者的選擇點選等操作。
根據清單的擴充卡類型,清單分為三種,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
執行個體:
listview.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="afterDescendants">
<!-- android:cacheColorHint="#00000000" 背景透明 -->
<!-- android:dividerHeight="1dp" item界限寬度 -->
<!-- android:cacheColorHint="#00000000" item界線顔色 -->
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:dividerHeight="1dp"
android:divider="#FF0000"
/>
</LinearLayout>
item.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<!-- 清單item布局 -->
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="15dp"
android:src="@drawable/a2"/>
<TextView
android:id="@+id/txtname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"/>
</LinearLayout>
ActivityListView.Java
public class ActivityListView extends Activity {
private ListView mListView;
protected void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加載listview控件所在的布局檔案
setContentView(R.layout.listview_layout);
//擷取listview控件的對象
mListView = (ListView)findViewById(R.id.listview);
/**
*定義SimpleAdapter(并加載對應item布局檔案,獲得對應的keY,獲得對應item布局檔案中的元件的id
*第二個參數data:資料源第三個參數resource:listView每個item對應的布局檔案第四個參數 from:
*第五個參數to: new String[]{對應的key}v
*/
SimpleAdapter simpleAdapter =new SimpleAdapter(this, get_data(),
R.layout.item_listview,new String[]{"name","img" },
newint[] { R.id.txtname, R.id.img });
//通過setAdapter将擴充卡綁定到listView控件
mListView.setAdapter(simpleAdapter);
};
/**
*資料源
*/
String[] name = { "張三","李四","小星星","靜靜","明明","小翠" };
private ArrayList<Map<String, Object>> get_data() {
//定義一個ArrayList的集合(集合中又封裝一個map類型的集合)
ArrayList<Map<String, Object>> data_list =new ArrayList<Map<String,Object>>();
for (int i = 0; i<name.length; i++) {
// key,valueintstring map不能new map
Map<String, Object> data_map =new HashMap<String, Object>();
data_map.put("name",name[i]);
data_map.put("img",R.drawable.a1);
//将map集合添加(封裝)到ArrayList集合中
data_list.add(data_map);
}
//傳回ArrayList集合
return data_list;
}
}
Gallery
作用:實作圖檔計時滾動顯示
執行個體:
gallery.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spacing="0dip"/>
</LinearLayout>
public class ActivityGrelly extends Activity {
/** Called when the activity isfirst created. */
private int index;
private Gallery g;
private Handler handler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_layout);
initView();
}
private void initView() {
// TODOAuto-generated method stub
// 獲得Gallery對象
g = (Gallery)findViewById(R.id.gallery);
// 添加ImageAdapter給Gallery對象
g.setAdapter(newImageAdapter(this));
// 設定Gallery的背景
g.setBackgroundResource(R.drawable.bg);
TimerTask task = new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 2;
index = g.getSelectedItemPosition();
index++;
handler.sendMessage(message);
}
};
Timer timer = new Timer();
timer.schedule(task, 3000, 3000);
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 2:
g.setSelection(index);
break;
default:
break;
}
}
};
// 設定Gallery的事件監聽
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position,long id) {
Toast.makeText(ActivityGrelly.this,
"你選擇了" + (position + 1) +"号圖檔", Toast.LENGTH_SHORT)
.show();
}
});
}
}
效果:
源碼下載下傳:
Eclipse下載下傳:http://download.csdn.net/detail/dickyqie/9620303
AndroidStudio下載下傳: https://github.com/DickyQie/android-list-control/tree/list-use