自定义复杂布局的ListView

自定义ListView中,已经做了比较复杂的布局,但和芒果TV还有一定的差距。继续模仿芒果TV,实现了更为复杂的ListView。效果如下,点击Item左部弹出播放影片的对话框:


image_thumb20.png

  image_thumb21.png


点击Item的右部,弹出收藏影片的对话框:


image_thumb22.png

image_thumb23.png


代码结构如下:


image_thumb24.png

布局文件mylist.xml,不是很复杂,ImageView的宽和高都用的是绝对变量,需要以后改进:


  1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:orientation="horizontal"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"  >
  5.     <imageview android:id="@+id/img" android:layout_gravity="center"
  6.         android:layout_width="wrap_content"
  7.         android:layout_height="wrap_content"
  8.         android:layout_margin="3px"/>
  9.     <linearlayout android:orientation="vertical" android:layout_gravity="center"
  10.         android:layout_width="203px"
  11.         android:layout_height="wrap_content">
  12.         <textview android:id="@+id/title"
  13.             android:layout_width="wrap_content"
  14.             android:layout_height="wrap_content"
  15.             android:textColor="#000000"
  16.             android:textSize="22px" />
  17.         <textview android:id="@+id/info"
  18.             android:layout_width="wrap_content"
  19.             android:layout_height="wrap_content"
  20.             android:textColor="#000000"
  21.             android:textSize="13px" />
  22.    
  23.     <imageview android:id="@+id/view_btn"
  24.         android:layout_width="40px"
  25.         android:layout_height="fill_parent"
  26.         android:background="@drawable/aa"
  27.         android:layout_gravity="right"
  28.         android:layout_marginRight="1px"/>

复制代码
java代码MyListView实现自定义布局,它的onCreate方法中,注册自定义的Adapter:
  1. public class MyListView extends ListActivity {
  2.     private List<map> mData;
  3.     @Override
  4.     public void onCreate(Bundle savedInstanceState) {
  5.         super.onCreate(savedInstanceState);
  6.         mData = ListVIewDate.getData();
  7.         this.getListView().setCacheColorHint(0);
  8.         this.getListView().setBackgroundColor(android.graphics.Color.WHITE);
  9.         MyAdapter adapter = new MyAdapter(this);
  10.         setListAdapter(adapter);
  11.     }
复制代码
自定义的Adapter代码片段,先不做太多解释:
  1. public final class ViewHolder {
  2.     public ImageView img;
  3.     public TextView title;
  4.     public TextView info;
  5.     public ImageView viewBtn;
  6. }
  7. public class MyAdapter extends BaseAdapter {
  8.     private LayoutInflater mInflater;
  9.     public MyAdapter(Context context) {
  10.         this.mInflater = LayoutInflater.from(context);
  11.     }
  12.     @Override
  13.     public int getCount() {
  14.         // TODO Auto-generated method stub
  15.         return mData.size();
  16.     }
  17.     @Override
  18.     public Object getItem(int arg0) {
  19.         // TODO Auto-generated method stub
  20.         return null;
  21.     }
  22.     @Override
  23.     public long getItemId(int arg0) {
  24.         // TODO Auto-generated method stub
  25.         return 0;
  26.     }
  27.     @Override
  28.     public View getView(int position, View convertView, ViewGroup parent) {
  29.         ViewHolder holder = null;
  30.         if (convertView == null) {
  31.             holder = new ViewHolder();
  32.             convertView = mInflater.inflate(R.layout.mylist, null);
  33.             holder.img = (ImageView) convertView.findViewById(R.id.img);
  34.             holder.title = (TextView) convertView.findViewById(R.id.title);
  35.             holder.info = (TextView) convertView.findViewById(R.id.info);
  36.             holder.viewBtn = (ImageView) convertView
  37.                     .findViewById(R.id.view_btn);
  38.             convertView.setTag(holder);
  39.         } else {
  40.             holder = (ViewHolder) convertView.getTag();
  41.         }
  42.         holder.img.setBackgroundResource((Integer) mData.get(position).get(
  43.                 "img"));
  44.         holder.title.setText((String) mData.get(position).get("title"));
  45.         holder.info.setText((String) mData.get(position).get("info"));
  46.         holder.viewBtn.setOnClickListener(new View.OnClickListener() {
  47.             @Override
  48.             public void onClick(View v) {
  49.                 showMyDialog(2);
  50.             }
  51.         });
  52.         return convertView;
  53.     }
  54. }
复制代码
ListViewDate只是负责填充数据,这里不做说明。具体程序源码见:androidlistviewdemo0.4.rar
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值