listview作为一个常用控件,对listview的操作都应该滚瓜烂熟了。对listview 的数据管理也很熟悉。包括listview的单击,双击,多选删除,完成刷新,添加数据等等。我再次对listview的管理操作写了一个demo。里面包含我说的那些部分操作,根据这个demo可以参考实现自已多更能操作。
源码地址:下载
1、实现的效果
2、主要的代码
package com.example.listmanage;
import java.util.ArrayList;
import java.util.LinkedList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener ,OnCheckedChangeListener {
class ItemStatus {
public boolean mIsCheckBoxVisible = false;
public boolean mIsCheckBoxSelected = false;
}
// 记录当前每个工程是否在UI上被选中
private LinkedList<ItemStatus> mItemsExtendData;
//listview的数据源
private ArrayList<String> mList;
private ListView mListView;
//适配器
private ListAdapt mListAdapt;
private TextView mtextviewShow;
//编辑或者完成
private Button mbuttonEdit;
//编辑,显示选择的checkbox
private boolean mbStatueShow = false;
// private arr
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initView() {
mListView = (ListView) findViewById(R.id.listView1);
mListAdapt = new ListAdapt(this);
mListView.setAdapter(mListAdapt);
ToolUtil.ReCalListViewHeightBasedOnChildren(mListView);
mListView.setOnItemClickListener(new ListItemonClick());
mbuttonEdit = (Button)findViewById(R.id.buttonEdit);
mbuttonEdit.setOnClickListener(this);
CheckBox cBox = (CheckBox)findViewById(R.id.checkBox1);
cBox.setOnCheckedChangeListener(this);
TextView textviewDetele = (TextView)findViewById(R.id.textviewDetele);
textviewDetele.setOnClickListener(this);
mtextviewShow = (TextView)findViewById(R.id.textviewShow);
mtextviewShow.setText("已选" + String.valueOf(0) + "项");
findViewById(R.id.layoutBottom).setVisibility(View.GONE);
findViewById(R.id.layoutBottomNew).setVisibility(View.VISIBLE);
}
private void initData() {
mList = new ArrayList<String>();
mList.clear();
for (int i = 0; i < 25; i++) {
mList.add("第 " + String.valueOf(i) + " 项");
}
InitListViewExtendData();
SetShowStatus(false);
}
//设置每一项是选择checkbox的显示状态
private void SetShowStatus(Boolean bStatueShow) {
mbStatueShow = bStatueShow;
if (mbStatueShow) {
// 遍历listView的数据源,设置部分控件的隐藏和显示
for (ItemStatus ItemData : GetItemsExtendData()) {
ItemData.mIsCheckBoxSelected = false;
ItemData.mIsCheckBoxVisible = true;
}
} else {
// 遍历listView的数据源,设置部分控件的隐藏和显示
for (ItemStatus ItemData : GetItemsExtendData()) {
ItemData.mIsCheckBoxSelected = false;
ItemData.mIsCheckBoxVisible = false;
}
}
if (mListAdapt != null) {
//使用listview手动计算高度
ToolUtil.ReCalListViewHeightBasedOnChildren(mListView);
mListAdapt.notifyDataSetChanged();
}
}
//listview的单击事件
private class ListItemonClick implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
CheckBox isSelectedCheckBox = (CheckBox)view.findViewById(R.id.checkBox1);
//处理编辑状态的单击选择
if (mbStatueShow) {
if (isSelectedCheckBox != null) isSelectedCheckBox.setChecked(!isSelectedCheckBox.isChecked());
return;
}
Toast.makeText(MainActivity.this, "单击" + String.valueOf(position), Toast.LENGTH_LONG).show();
}
}
//适配器使用的内部类
class ViewHolder {
TextView titleTextView;
CheckBox selectCheckBox;
}
class ListAdapt extends BaseAdapter implements OnCheckedChangeListener {
private Context mContext;
private LayoutInflater mLayoutInflater;
public ListAdapt(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vHolder = null;
if (convertView == null) {
vHolder = new ViewHolder();
convertView = mLayoutInflater.inflate(
R.layout.activity_main_list_item, null);
vHolder.titleTextView = (TextView) convertView
.findViewById(R.id.textView1);
vHolder.selectCheckBox = (CheckBox) convertView
.findViewById(R.id.checkBox1);
convertView.setTag(vHolder);
} else {
vHolder = (ViewHolder) convertView.getTag();
}
vHolder.titleTextView.setText(mList.get(position));
if (GetItemsExtendData().size() != 0) {
ItemStatus itemStatus = GetItemsExtendData().get(position);
if (itemStatus != null && vHolder.selectCheckBox != null) {
vHolder.selectCheckBox.setTag(position);
vHolder.selectCheckBox.setOnCheckedChangeListener(this);
vHolder.selectCheckBox
.setVisibility(itemStatus.mIsCheckBoxVisible == true ? View.VISIBLE
: View.INVISIBLE);
vHolder.selectCheckBox
.setChecked(itemStatus.mIsCheckBoxSelected);
}
}
return convertView;
}
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Integer nPosition = (Integer) (buttonView.getTag());
GetItemsExtendData().get(nPosition.intValue()).mIsCheckBoxSelected = isChecked;
// 获取当前的所有选中项个数 -- 如果数据较多的话,不可以这种方式
int nSelectCount = 0;
for (ItemStatus ItemData : GetItemsExtendData()) {
if (ItemData.mIsCheckBoxSelected == true)
nSelectCount += 1;
}
mtextviewShow.setText("已选" + String.valueOf(nSelectCount) + "项");
}
}
// 初始化列表的额外数据
private void InitListViewExtendData() {
GetItemsExtendData().clear();
int nItemCount = mList.size();
for (int i = 0; i < nItemCount; i++) {
ItemStatus itemData = new ItemStatus();
GetItemsExtendData().add(itemData);
}
}
private LinkedList<ItemStatus> GetItemsExtendData() {
if (mItemsExtendData == null)
mItemsExtendData = new LinkedList<ItemStatus>();
return mItemsExtendData;
}
protected ArrayList<Integer> getRemoveCount() {
// 获取需要删除的索引数组
ArrayList<Integer> needRemoveItemsIndexArray = new ArrayList<Integer>();
for (int i = 0; i < GetItemsExtendData().size(); i++) {
ItemStatus itemData = GetItemsExtendData().get(i);
if (itemData.mIsCheckBoxSelected == true)
needRemoveItemsIndexArray.add(new Integer(i));
}
return needRemoveItemsIndexArray;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonEdit:
if (mbStatueShow) {
mbuttonEdit.setText("编辑");
SetShowStatus(false);
findViewById(R.id.layoutBottom).setVisibility(View.GONE);
findViewById(R.id.layoutBottomNew).setVisibility(View.VISIBLE);
}else {
SetShowStatus(true);
mbuttonEdit.setText("完成");
findViewById(R.id.layoutBottomNew).setVisibility(View.GONE);
findViewById(R.id.layoutBottom).setVisibility(View.VISIBLE);
}
break;
case R.id.textviewDetele:
ArrayList<Integer> needRemoveItemsIndexArray = getRemoveCount();
// 删除指定项
for (int i = needRemoveItemsIndexArray.size() - 1; i >= 0; i--) {
mList.remove(needRemoveItemsIndexArray.get(i).intValue());
}
InitListViewExtendData();
SetShowStatus(true);
break;
default:
break;
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.checkBox1:
if (isChecked) {
for (ItemStatus ItemData : GetItemsExtendData()) {
ItemData.mIsCheckBoxSelected = true;
ItemData.mIsCheckBoxVisible = true;
}
mtextviewShow.setText("已选" + String.valueOf(mList.size()) + "项");
}else {
for (ItemStatus ItemData : GetItemsExtendData()) {
ItemData.mIsCheckBoxSelected = false;
ItemData.mIsCheckBoxVisible = true;
}
mtextviewShow.setText("已选" + String.valueOf(0) + "项");
}
if (mListAdapt != null) {
ToolUtil.ReCalListViewHeightBasedOnChildren(mListView);
mListAdapt.notifyDataSetChanged();
}
break;
default:
break;
}
}
}
3、手动计算listview高度工具类
package com.example.listmanage;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ToolUtil {
public static void ReCalListViewHeightBasedOnChildren(ListView listView) {
if (listView == null) return;
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) return;
int nTotalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
nTotalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = nTotalHeight + (listView.getDividerHeight()*(listAdapter.getCount()-1));
}
}
源码地址:下载