ListView
作用: 1.将数据填充到布局。 2.处理用户的选择点击等操作。
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
实例:listview.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="afterDescendants">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:dividerHeight="1dp"
android:divider="#FF0000"
/>item.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="15dp"
android:class="lazyload" src="https://img-blog.csdnimg.cn/2022010700564781686.png" data-original="@drawable/a2"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"/>
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> get_data() { //定义一个ArrayList的集合(集合中又封装一个map类型的集合)
ArrayList> data_list =new ArrayList>(); for (int i = 0; i
Map data_map =new HashMap();
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
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spacing="0dip"/>
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();
}
});
}
}