定制自己的ListView

自定义布局需要哪些东西? 
1、数据模型(数据) 
2、显示数据的组件 
 
3、重写BaseAdapter中的相关方法 

 

数据模型代码:

package cc.androidos.layout;public class RSS {
 private String title; public RSS(String title) {
  this.title = title;
 } public String getTitle() {
  return title;
 } public void setTitle(String title) {
  this.title = title;
 }
 @Override
 public String toString() {
  return title;
 }}

 

显示组件的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/tablelist"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView android:id="@+id/TextView1" android:layout_width="200px"
   android:layout_height="wrap_content" android:text="TextView1"></TextView>
  <ImageButton android:id="@+id/ImageButton01"
   android:layout_width="wrap_content" android:layout_height="wrap_content"
   android:layout_marginLeft="80px"></ImageButton>
 </TableRow>
 
 
</TableLayout>

 

这个Activity和重写BaseAdapter的代码:

package cc.androidos.layout;import java.util.ArrayList;
import java.util.List;import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.Scroller;
import android.widget.TextView;public class MyLayout extends ListActivity {
 
 //要被显示的数据模型
 public static  List<RSS> dataModel = new ArrayList<RSS>();
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  getListView().setSelector(getResources().getDrawable(R.drawable.icon));
  //将组定义的List组件加入到当前的Activity中
  setListAdapter(new MyListAdapter(getApplicationContext()));
  
  dataModel.add(new RSS("title 1"));
  dataModel.add(new RSS("title 2"));
  dataModel.add(new RSS("title 3"));
  dataModel.add(new RSS("title 4"));
 } 
 /*自定义布局组件中用于显示数据的组件类**/
 public class ViewHolder {
  TextView mTextView;
  ImageButton mIcon;
 } // 自定义的布局组件
 public class MyListAdapter extends BaseAdapter {
  private LayoutInflater mInflater = null;
  private Bitmap mIcon1 = null;
  private Bitmap mIcon2 = null;
  private Context mcContext = null;  public MyListAdapter(Context context) {
   this.mcContext = context;
   
   mInflater = LayoutInflater.from(this.mcContext);
   mIcon1 = BitmapFactory.decodeResource(mcContext.getResources(),
     R.drawable.delete);
   mIcon2 = BitmapFactory.decodeResource(mcContext.getResources(),
     R.drawable.delete);
   
  }  @Override
  public int getCount() {
   return dataModel.size();
  }  @Override
  public Object getItem(int position) {
   return dataModel.get(position);
  }  @Override
  public long getItemId(int position) {
   return position;
  }  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   String tag = "getView";
   final ViewHolder holder;
   if (convertView == null) {
    //使用XML文件填充整个布局
    convertView = mInflater.inflate(R.layout.list, null);
    holder = new ViewHolder();//生成存放数据的组件模型
    holder.mTextView = (TextView) convertView
      .findViewById(R.id.TextView1);
    holder.mIcon = (ImageButton) convertView
      .findViewById(R.id.ImageButton01);
    convertView.setTag(holder);//将存放数据的组件模型加入到整个View视图中
   } else {
    holder = (ViewHolder) convertView.getTag();
   }
   Log.d(tag, "start get View.....");
   
   holder.mTextView.setText(dataModel.get(position).toString());
   holder.mIcon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
   holder.mIcon.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {//设置按钮点击事件
     holder.mTextView.setBackgroundColor(Color.WHITE);
     holder.mIcon.setVisibility(View.VISIBLE);
    }
   });
   return convertView;
  }
 }}

 

这样自定义完成后,显示数据将会有一个图片和一个字符串放在ListActivity中每项中

你可能感兴趣的:(xml,android,OS)