Android ListView简单示例
程序员文章站
2022-07-14 12:49:38
...
笔者能力有限,不足之处望各位码友不吝赐教!
笔者使用的IDE是Android Studio,关于新建项目部分不再赘述。
先看需求:
-将姓名、学号和性别信息等写入ListView;
-如果是男生,添加一个ImageView。点击之后进入详细页面;
-如果是女生,则不添加ImageView,点击之后进入详细页面;
1.首先在LAUNCHER activity的布局文件中添加ListView
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
2.在MainActivity中添加ListView对象并初始化
ListView listView = (ListView) findViewById(id.list_view);
3.编写ListView的item
按照需要,我们需要的item有两种:有无ImageView
(1)boy_item_layout
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_margin="10dp" >
<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
app:srcCompat="@mipmap/ic_launcher"
android:layout_weight="0.03" />
<LinearLayout
android:orientation="vertical"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:text="@string/name_string"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="1"
android:gravity="bottom"
android:text="@string/student_number_string"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:textSize="25sp"
android:id="@+id/student_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/student_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom"
android:textSize="25sp" />
</LinearLayout>
<TextView
android:id="@+id/sex"
android:gravity="center_horizontal|center_vertical"
android:text="@string/sex_string"
android:textSize="30sp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
(2)girl_item_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_margin="10dp" >
<LinearLayout
android:orientation="vertical"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:text="@string/name_string"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="1"
android:gravity="bottom"
android:text="@string/student_number_string"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:textSize="25sp"
android:id="@+id/student_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/student_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom"
android:textSize="25sp" />
</LinearLayout>
<TextView
android:id="@+id/sex"
android:gravity="center_horizontal|center_vertical"
android:text="@string/sex_string"
android:textSize="30sp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
4.为ListView编写Adapter类,主要用于ListView渲染数据 自定义的Adapter类主要继承BaseAdapter,重写其中的几个方法即可
在这之前:
(1)由于信息不止一个,所以需要用到List对象来储存Bean对象
(2)自定义layout时需要用到LayoutInflater对象,该对象初始化时需要用到该Activity的Context对象
(3)关于Bean类,该类提供几个字段即可,有无访问器都可。(这里为了简单明了,就没有提供)
(4)ViewHolder类,该自定义类主要储存我们需要渲染的item组件,此处不再详细介绍,网上有很多资料
-自定Bean类InfoBean
-自定Bean类InfoBean
public class InfoBean {
public String name;
public String student_number;
public String sex;
public String phone_number;
}
-ViewHolder类
public static class ViewHolder{
public TextView name;
public TextView sex;
public TextView student_number;
public ImageView img;
}
(1)构造器 初始化context、list和layoutinflater。
public LvAdapter(Context context,List<InfoBean> list){
mContext = context;
mList = list;//储存这所有需要渲染的数据
mLayoutInflater = LayoutInflater.from(context);
}
(2)getCount 返回该ListView的数量public int getCount() {
return mList.size();
}
(3)getItem 返回item的数据对象(比如你的list存的某一个Bean对象)public Object getItem(int position) {
return mList.get(position);
}
(4)getItemId 返回item的编号,也相当于list的下标public long getItemId(int position) {
return position;
}
(5)getItemViewType 该方法根据list中的数据来确定需要渲染的layout类型 public int getItemViewType(int position) {
if (mList.get(position).sex.equals("男"))
return ISBOY;//自定义的字段,方便操作,主要是避免使用“魔法数字” 下同
else
return ISGIRL;
}
(6)getViewTypeCount 该方法返回item的类型数量public int getViewTypeCount() {
return 2;
}
(7)getView 该方法是最重要的,为每一个子item填充数据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;
import java.util.List;
/**
* Created by PCC on 2017/8/15.
*/
public class LvAdapter extends BaseAdapter {
private List<InfoBean> mList;
private Context mContext;
private LayoutInflater mLayoutInflater;
private static final int ISGIRL = 1;
private static final int ISBOY = 0;
private int type = 0;
public LvAdapter(Context context,List<InfoBean> list){
mContext = context;
mList = list;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
public LvAdapter() {
super();
}
@Override
public int getItemViewType(int position) {
if (mList.get(position).sex.equals("男"))
return ISBOY;
else
return ISGIRL;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
type = getItemViewType(position);
if (type == ISBOY) {
if (convertView == null){
convertView = mLayoutInflater.inflate(R.layout.boy_item_layout,null);//该数据是boy,故加载boy_item_layout
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.student_name);t
viewHolder.sex = (TextView) convertView.findViewById(R.id.sex);
viewHolder.img = (ImageView) convertView.findViewById(R.id.imageView);
viewHolder.student_number = (TextView)convertView.findViewById(R.id.student_number) ;
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
}else {
if (convertView == null){
convertView = mLayoutInflater.inflate(R.layout.girl_item_layout,null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.student_name);
viewHolder.student_number = (TextView) convertView.findViewById(R.id.student_number);//该数据是boy,故加载boy_item_layout
viewHolder.sex = (TextView) convertView.findViewById(R.id.sex);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
}
viewHolder.name.setText(mList.get(position).name);
viewHolder.student_number.setText(mList.get(position).student_number);
viewHolder.sex.setText(mList.get(position).sex);
if (type == ISBOY)
viewHolder.img .setImageResource(R.mipmap.ic_launcher);
return convertView;
}
public static class ViewHolder{
public TextView name;
public TextView sex;
public TextView student_number;
public ImageView img;
}
}
5.接下来写点击某个item后的详细页面 写在前面:
(1)两种类型的差别就是有无ImageView,该处做法是使用同一个details Activity来展示;girl就不填充ImageView即可
(2)由于涉及到Activity间的跳转,但是传递的的信息较多(单个对象),此处使用单例模式模式比较方便(单例模式是将构造方法私有--保证只有一种方式能够获得对象实例,使在整个APP存活期间都存在,有且只有一个实例)。
-Singleton
public class Singleton {
private static Singleton singleton ;
private static boolean hasInited = false;//是否实例化标记
private InfoBean mInfoBean;
private Singleton(){//将构造方法设置为私有
}
private static void init(){
singleton = new Singleton();
}
public static Singleton getInstance(){
if (!hasInited){
init();//实例化Singleton
hasInited = true;
}
return singleton;
}
//对象的访问器方法
public InfoBean getmInfoBean() {
return mInfoBean;
}
public void setmInfoBean(InfoBean mInfoBean) {
this.mInfoBean = mInfoBean;
}
}
-activity_details
<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"
android:layout_margin="20dp"
android:gravity="center"
android:orientation="vertical"
tools:context="home.pcc.myapplication.Details">
<ImageView
android:id="@+id/image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/name_string"
android:textSize="24sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/sex_string"
android:textSize="24sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/student_number_string"
android:textSize="24sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/phone_string"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/details_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:textSize="24sp" />
<TextView
android:id="@+id/details_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:textSize="24sp" />
<TextView
android:id="@+id/details_student_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:textSize="24sp" />
<TextView
android:id="@+id/details_phone_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
-Detailsimport android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class Details extends AppCompatActivity {
private TextView studentName;
private TextView studentNumber;
private TextView phoneNumber;
private TextView sex;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
// 初始化显示信息的组件
studentName = (TextView) findViewById(R.id.details_name);
studentNumber = (TextView) findViewById(R.id.details_student_number);
phoneNumber = (TextView) findViewById(R.id.details_phone_number);
sex = (TextView) findViewById(R.id.details_sex);
imageView = (ImageView) findViewById(R.id.image);
init();//渲染数据
}
public void init(){
InfoBean infoBean = Singleton.getInstance().getmInfoBean();//从单例模式中获取对象
if (infoBean.sex.equals("男"))
imageView.setImageResource(R.mipmap.ic_launcher);//加载库中提供的图片
studentName.setText(infoBean.name);
phoneNumber.setText(infoBean.phone_number);
studentNumber.setText(infoBean.student_number);
sex.setText(infoBean.sex);
}
}
6.接下来就是MainActivity,要实现点击item后跳转Activity就要用到onItemClick方法,此处实现了OnItemClickListener接口实现该方法public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
private ListView listView;
private List<InfoBean> mList;
private LvAdapter mAdapter;//自定义的Adapter类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
listView = (ListView) findViewById(id.list_view);
mList = new ArrayList<>();
initView();//给list加载数据
mAdapter = new LvAdapter(this,mList);
listView.setAdapter(mAdapter);//将数据渲染到预先设置好的LV中
listView.setOnItemClickListener(this);//添加点击事件
}
/**
* 初始化List
*/
public void initView(){
String name = "测试",sex,number = "20150000000";
for (int i = 0; i < 10; i++){
if (i%2 == 0)
sex = "女";
else
sex = "男";
InfoBean mInfoBean = new InfoBean();
mInfoBean.sex = sex;
mInfoBean.name = name;
mInfoBean.student_number = number+i;
mInfoBean.phone_number = "1520000000"+i;
mList.add(mInfoBean);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Singleton.getInstance().setmInfoBean(mList.get(position));//将点击的子item的数据(InfoBean)对象存入单利中
Intent intent = new Intent(MainActivity.this,Details.class);//设置intent,表明需要跳转的Activity
startActivity(intent);
}
}
效果图:
最后:
页面的布局肯定需要修改,到这里就仁者见仁了。关于数据的来源就有很多方法了,可以通过访问接服务器获得数据解析json、xml;也可以将数据保存在本地的sqlite数据库中,app反复打开也不会丢失数据。后面会介绍如何使用自定义AlertDialog添加数据到本地Sqlite数据库中。
ListView还可以实现一个OnItemLongClickListener接口,使得ListView更加灵活,比如长按item实现 删除信息、打开到编辑页面、跳出选择菜单实现即时联系等等。