前面我介绍了listview控件的不同用法,但是这些用法在实际的开发项目中是不足以满足需求的,因为前面的几种用法只能简单的显示文本信息,而且布局都比较单一,很难做出复杂的结果,在实际的开发项目中,90%以上都是需要自己自定义listview的,这一篇,我们将介绍如何使用自定义布局的listview
先看效果图
好了,其实这种自定义布局的实现,是通过自定义adapter来实现的,首先我们简单介绍下adapter
每个listview要想实现数据的显示,都必须绑定一个adapter,adapter主要实现将数据和listview中的每一个item进行绑定,这样就可以灵活的设计自己的布局了,我们看一下这次的项目目录
现在开始看代码
主界面的布局文件
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="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
item_listview.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="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/img" android:layout_width="80dp" android:layout_height="80dp" android:padding="10dp" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="3dp" android:layout_marginTop="3dp" android:gravity="center" android:text="标题" android:textSize="20sp" /> <TextView android:id="@+id/tv2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="3dp" android:layout_marginTop="3dp" android:text="简介" android:textSize="16sp" /> </LinearLayout> </LinearLayout>
实体类thing.java
package com.example.diylistview; public class Thing { private String title; private String Introduce; private int picture; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getIntroduce() { return Introduce; } public void setIntroduce(String introduce) { Introduce = introduce; } public int getPicture() { return picture; } public void setPicture(int picture) { this.picture = picture; } /** * */ public Thing() { super(); } /** * @param title * @param introduce * @param picture */ public Thing(String title, String introduce, int picture) { super(); this.title = title; Introduce = introduce; this.picture = picture; } }
MyAdapter.java
package com.example.diylistview; import java.util.List; 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.TextView; public class MyAdapter extends BaseAdapter { private Context context; private List<Thing> lists; private LayoutInflater layoutInflater; ImageView img; TextView tv1; TextView tv2; /** * 构造函数,进行初始化 * * @param context * @param lists */ MyAdapter(Context context, List<Thing> lists) { this.context = context; this.lists = lists; layoutInflater = LayoutInflater.from(this.context); } // 获得长度,一般返回数据的长度即可 @Override public int getCount() { return lists.size(); } @Override public Object getItem(int position) { return lists.get(position); } @Override public long getItemId(int position) { return position; } /** * 最重要的方法,每一个item生成的时候,都会执行这个方法,在这个方法中实现数据与item中每个控件的绑定 */ @Override public View getView(int position, View convertView, ViewGroup parent) { // convertView对象就是item的界面对象,只有为空的时候我们才需要重新赋值一次,这样可以提高效率,如果有这个对象的话,系统会自动复用 //item_listview就是自定义的item的布局文件 if (convertView == null) { convertView = layoutInflater.inflate(R.layout.item_listview, null); } //注意findViewById的时候,要使用convertView的这个方法,因为是在它里面进行控件的寻找 img = (ImageView) convertView.findViewById(R.id.img); tv1 = (TextView) convertView.findViewById(R.id.tv1); tv2 = (TextView) convertView.findViewById(R.id.tv2); //将数据与控件进行绑定 img.setBackgroundResource(lists.get(position).getPicture()); tv1.setText(lists.get(position).getTitle()); tv2.setText(lists.get(position).getIntroduce()); return convertView; } }
MainActivity.java
package com.example.diylistview; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener { private ListView listview; private List<Thing> lists; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.listview); //初始化数据 lists = getLists(); //设置适配器 listview.setAdapter(new MyAdapter(this, lists)); //设置监听 listview.setOnItemClickListener(this); } //返回数据 private List<Thing> getLists() { List<Thing> lists = new ArrayList<Thing>(); for (int i = 0; i < 20; i++) { Thing thing = new Thing(); thing.setPicture(R.drawable.ic_launcher); thing.setTitle("我是标题" + i); thing.setIntroduce("我是简介" + i); lists.add(thing); } return lists; } //item的点击监听时间 @Override public void onItemClick(AdapterView<?> view, View arg1, int position, long arg3) { Toast.makeText(this, ((Thing)view.getItemAtPosition(position)).getTitle(), 0).show(); } }
好了,自定义的listview的使用就到这里了,如果有疑问,请留言