1. MainActivity.java
public class MainActivity extends Activity {
//声明UI组件和资源变量等private ListView lv2;private MyListViewAdapter listViewAdapter;;protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//基于BaseAdapter//setContentView(R.layout.gallery);//setGalleryView();//基于ListView、GridView及Spinner和BaseAdapter实现setContentView(R.layout.main);setLGSView();}private void setLGSView() {
lv2 = (ListView)findViewById(R.id.listview2);listViewAdapter = new MyListViewAdapter(this);lv2.setAdapter(listViewAdapter);}2. MyListViewAdapter.java
package home.lee.listviewadapterdemo;
import android.content.Context;
import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class MyListViewAdapter extends BaseAdapter {
private Context context;//为myInflater而采用context
private LayoutInflater myInflater;public MyListViewAdapter(Context context) {
super();this.context = context;}@Override
public int getCount() {//用来说明需要绘制的条目数return 8;//数组的长度,即个数,可以根据实际调整}@Override
public Object getItem(int position) { return position;}@Override
public long getItemId(int position) { return position;}@Override
public View getView(int position, View convertView, ViewGroup parent) {//用来说明需要绘制的条目数个条目就调用一次这个方法ViewHolder holder = new ViewHolder();if (convertView == null){ /*** 这个方法getView(),是用来逐一绘制每一条item** setTag()会把View与ViewHolder绑定,形成一一对应的关系,拖动listview的时候会重新绘制每一条item* 但是那些已经取得绑定的View,会调用getTag()方法,就不会重新绘制,而是拿到内存中已经取得的资源,提高了效率** @param position position就是位置从0开始* @param convertView convertView是Spinner,ListView中每一项要显示的view* @param parent parent就是父窗体了,也就是Spinner,ListView,GridView了* @return 通常return 的view也就是convertView** 在程序中动态加载以上布局:* LayoutInflater flater = LayoutInflater.from(this);* View view = flater.inflate(R.layout.example, null);*/myInflater = LayoutInflater.from(context);convertView = myInflater.inflate(R.layout.item_list, null);holder.icon = (ImageView)convertView.findViewById(R.id.itemimageview2);
holder.text = (TextView)convertView.findViewById(R.id.itemtextview2);convertView.setTag(holder);
}else{ holder = (ViewHolder) convertView.getTag();}if (position <= 1)
{ holder.icon.setImageResource(android.R.drawable.btn_dropdown);holder.text.setText("你好");}else if (position <= 3 && position >= 2){ holder.icon.setImageResource(android.R.drawable.edit_text);holder.text.setText("Hello");}else{ holder.icon.setImageResource(android.R.drawable.spinner_dropdown_background);holder.text.setText("안녕하세요");}return convertView;}private class ViewHolder
{ TextView text;ImageView icon;}}
3. main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"android:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/linearlayout2"android:orientation="vertical" ><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="ListView" /><ListViewandroid:id="@+id/listview2"android:layout_width="fill_parent"android:layout_height="wrap_content"></ListView></LinearLayout>4. item_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal" ><TextViewandroid:id="@+id/itemtextview2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""android:layout_weight="1"/><ImageViewandroid:id="@+id/itemimageview2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:contentDescription="@string/app_name"android:src="@drawable/ic_launcher"android:layout_weight="10"/></LinearLayout>运行结果:
其改进应用可参考: