list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bookshelfcell"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal" >
<ImageView
android:id="@+id/iv1"
android:layout_width="80dip"
android:layout_height="110dip"
android:layout_marginTop="15dip"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal" >
<ImageView
android:id="@+id/iv2"
android:layout_width="80dip"
android:layout_height="110dip"
android:layout_marginTop="15dip"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal" >
<ImageView
android:id="@+id/iv3"
android:layout_width="80dip"
android:layout_height="110dip"
android:layout_marginTop="15dip"
/>
</LinearLayout>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:background="@drawable/navbar2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="在线书城"
android:gravity="center"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/sharebg"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:divider="#00000000"
android:cacheColorHint="#00000000"
android:id="@+id/lv"
></ListView>
</LinearLayout>
</LinearLayout>
view 实现类
package my.hh.lin;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class TestbookActivity extends Activity {
/** Called when the activity is first created. */
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new ListAdapter(this));
}
class ListAdapter extends BaseAdapter{
private LayoutInflater inflater;
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>> ();
Resources res=getResources();
public ListAdapter(Context c) {
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(c);
for(int i = 0;i < 5;i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("iv1"+i, BitmapFactory.decodeResource(res, R.drawable.book1));
map.put("iv2"+i, BitmapFactory.decodeResource(res, R.drawable.book2));
map.put("iv3"+i,BitmapFactory.decodeResource(res, R.drawable.book3));
list.add(map);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final viewHolder myHolder ;
if(convertView == null){
myHolder = new viewHolder();
convertView = inflater.inflate(R.layout.list_item, null);
myHolder.iv1 = (ImageView) convertView.findViewById(R.id.iv1);
myHolder.iv2 = (ImageView) convertView.findViewById(R.id.iv2);
myHolder.iv3 = (ImageView) convertView.findViewById(R.id.iv3);
myHolder.iv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(v.getContext(), ""+position, Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(myHolder);
}
else
{
myHolder=(viewHolder)convertView.getTag();
}
myHolder.iv1.setImageBitmap((Bitmap) list.get(position).get("iv1"+position));
myHolder.iv2.setImageBitmap((Bitmap) list.get(position).get("iv2"+position));
myHolder.iv3.setImageBitmap((Bitmap) list.get(position).get("iv3"+position));
return convertView;
}
}
}