1、ListView的一个经典实现动态增加、删除行的例子实现。
2、Listview优化策略?
1)convetView进行判空,为空时加载布局,不为空时直接重新使用 convertView,减少了布局的加载。(效率提高200%)
2)定义ViewHolder,传入ViewHolder对象给convetView设置tag,用于缓存控件。(效率再提高50%)
3)当ListView加载数据量较大时可采用分页加载和图片异步加载方法。
3、代码示例
0.data.java
package com.guan.http.model;
public class Data {
private int id;
private ImageView image;
private int num;
public Data() {
super();
}
public Student(int id, ImageView image, int num) {
super();
this.id = id;
this.image = image;
this.num = num;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImage() {
return image;
}
public void setName(String image) {
this.image = image;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
①activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加" />
<Button
android:id="@+id/btn_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减少" />
</LinearLayout>
<ListView
android:id="@+id/lv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
②adater_line.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="42dp"
android:layout_marginTop="20dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView" />
</RelativeLayout>
③ShowAdapter
package com.example.guan.adapter;
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 com.example.guan.listview.R;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
* @author Guan
* @file com.example.guan.adapter
* @date 2015/8/14
* @Version 1.0
*/
public class ShowAdapter extends BaseAdapter {
private Context mContext;
private List mList;
public ShowAdapter(Context context, List list) {
this.mContext = context;
this.mList = list;
}
@Override
public int getCount() {
if (mList != null && mList.size() > 0) {
return mList.size();
} else {
return 0;
}
}
@Override
public Object getItem(int position) {
if (mList != null && mList.size() > 0) {
return mList.get(position);
} else {
return null;
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 2、使用了ViewHolder作为子布局的缓存,使用View的setTag方法将缓存与每个item绑定,
// 则也可以省去了findViewById的事件
ViewHolder holder;
if (convertView != null) {
// 3、获取ViewHolder
holder = (ViewHolder) convertView.getTag();
} else {
convertView = LayoutInflater.from(mContext).inflate(R.layout.adater_line, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
// 获取数据对象
Data data = (Data) getItem(position);
// 4、绑定数据到holder.
holder.textView.setText(data.getNum() + "");
//holder.textView.setText(mList.get(position).toString());
return convertView;
}
/**
* 1、将ViewHolder设置为static的目的是指在初始化Adapter时初始化一次这个内部类,
* 否则将会在每次创建Adapter时都要初始化一次,而这是没有必要的。
*/
static class ViewHolder {
@InjectView(R.id.imageView)
ImageView imageView;
@InjectView(R.id.textView)
TextView textView;
ViewHolder(View view) {
ButterKnife.inject(this, view);
}
}
}
④MainActivity
package com.example.guan.listview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.example.guan.adapter.ShowAdapter;
import java.util.ArrayList;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
/**
* @author Guan
* @file com.example.guan.listviews
* @date 2015/8/14
* @Version 1.0
*/
public class MainActivity extends AppCompatActivity {
@InjectView(R.id.btn_add)
Button btnAdd;
@InjectView(R.id.btn_remove)
Button btnRemove;
@InjectView(R.id.lv_show)
ListView lvShow;
private List list;
private ShowAdapter showAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
// 初始化
Init();
}
/**
* 初始化
*/
private void Init() {
list = new ArrayList();
for (int i = 0; i < 3; i++) {
list.add("123");
}
showAdapter = new ShowAdapter(this, list);
lvShow.setAdapter(showAdapter);
}
/**
* 监听实现
* @param view
*/
@OnClick({R.id.btn_add, R.id.btn_remove})
public void pickDoor(View view) {
if (view == btnAdd) {
list.add("123");
showAdapter.notifyDataSetChanged();
} else {
list.remove(0);
showAdapter.notifyDataSetChanged();
}
}
}
⑤图片资源
4、效果图
5、知识补充
1)adapter.notifyDataSetChanged()
①实现对listView列表的刷新;
②新建一个集合时,adapter调用此方法时强制传到getView执行。
2)布局
android:divider 边框线条 = “#FFFFFF”/ “@drawable/list”
android:dividerHeight 边框线条
3)去除分割线
①设置android:divider="@null"
②android:divider="#00000000" #00000000后面两个零表示透明
③.setDividerHeight(0) 高度设为0
4)去除滚动棒
android:scrollbars=“none”