android 手把手实现自定义列表

这篇博客详细介绍了如何在Android中实现自定义列表。首先创建了一个名为CustomList的工程,接着设计了custom_list_cell.xml布局文件来定义列表项展示格式。然后,通过创建CustomListCellData类来存储列表数据。为了适配数据,自定义了一个CustomListAdapter,该适配器继承自BaseAdapter。此外,将所需的图片资源放入res/drawable目录。最后,在MainActivity中,将适配器与ListView进行绑定,完成列表的显示。
摘要由CSDN通过智能技术生成

1.创建名为CustomList的Android工程

2.创建custom_list_cell.xml用于显示一个列表项的具体布局格式。

3.创建一个CustomListCellData的类用于对应一个列表项的数据。

4.创建一个CustomListAdapter适配器继承自BaseAdapter用于适配数据。

5.在res/drawable下放置自己要使用的图片资源

6.在MainActivity中把适配器和列表相关联。



目录结构如下所示:

代码如下:


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />

</LinearLayout>

custom_list_cell.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    
    <ImageView android:id="@+id/iconId"
        android:layout_width="120dp"
        android:layout_height="120dp"/>
    
    <LinearLayout
        android:id="@+id/ll" 
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        >
        <TextView android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
             android:layout_weight="1"/>
        <TextView android:id="@+id/sex"
            android:layout_width="fill_parent"
           android:layout_height="0dp"
             android:layout_weight="1"/>
    </LinearLayout>
    

</LinearLayout>



package edu.yg.customlist;

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.LinearLayout;
import android.widget.TextView;

public class CustomListAdapter extends BaseAdapter {
	private Context context;
	
	public CustomListAdapter(Context context) {
		this.context=context;
		
	}
	

	public Context getContext() {
		return context;
	}




	CustomListCellData[] data={new CustomListCellData("img1",R.drawable.img1,"男"),new CustomListCellData("img2", R.drawable.img2, "女")};
	

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.length;
	}

	
	@Override
	public CustomListCellData getItem(int position) {
		// TODO Auto-generated method stub
		return data[position];
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout ll=null;
		if(convertView!=null){
			ll=(LinearLayout) convertView;
		}
		else{
			ll=(LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.custom_list_cell, null);
		}
		
		CustomListCellData dt=getItem(position);
		
		ImageView icon=(ImageView) ll.findViewById(R.id.iconId);
		TextView name=(TextView) ll.findViewById(R.id.name);
		TextView sex=(TextView) ll.findViewById(R.id.sex);
		
		icon.setImageResource(dt.getIconid());
		name.setText(dt.getName());
		sex.setText(dt.getSex());
		
		return ll;
	}

}


package edu.yg.customlist;

public class CustomListCellData {

	public CustomListCellData(String name, int iconid, String sex) {
		super();
		this.name = name;
		this.iconid = iconid;
		this.sex = sex;
	}
	private String name;
	private int iconid;
	private String sex;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getIconid() {
		return iconid;
	}
	public void setIconid(int iconid) {
		this.iconid = iconid;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
}


package edu.yg.customlist;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;


public class MainActivity extends Activity {

	ListView lv=null;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        lv=(ListView) findViewById(R.id.listview);
        lv.setAdapter(new CustomListAdapter(this));
    }


   
}

运行结果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值