android 书架效果实现

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;
}



    }




}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值