实现以上效果需解决
1选择苹果时并不是所有的苹果都被选中
2解决如果是几千条数据,等大数据带来的程序崩溃问题
3实现添加头部和脚部
4全选和反选
java源码部分
FruitAdapter
public class FruitAdapter extends BaseAdapter {
private List<Fruit> mFruits;
private LayoutInflater Inflater;
private boolean[] mManagerAllCheckBox;
public FruitAdapter(LayoutInflater Inflater, List fruits) {
this.Inflater = Inflater;
this.mFruits = fruits;
mManagerAllCheckBox = new boolean[fruits.size()];
}
/**
* 全选
*/
public void selectAll() {
for (int i = 0; i < mFruits.size(); i++) {
mManagerAllCheckBox[i] = true;
notifyDataSetChanged();
}
}
public void selectFan() {
for (int i = 0; i < mFruits.size(); i++) {
mManagerAllCheckBox[i] = !mManagerAllCheckBox[i];
notifyDataSetChanged();
}
}
/**
* 点击Item选择CheckBox
*/
public void select(int position) {
mManagerAllCheckBox[position] = !mManagerAllCheckBox[position];
notifyDataSetChanged();
}
@Override
public int getCount() {
return mFruits.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//通过ViewHolder不必每一个item都生成view,使得运行内存大大减少
ViewHolder vh = null;
Fruit fruit = mFruits.get(position);
if (convertView == null) {
//别忘了生成对象,否则报空指针
vh = new ViewHolder();
//LayoutInflater把XML转化为view
convertView = Inflater.inflate(R.layout.list_fruit, null);
vh.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
vh.imageView = (ImageView) convertView.findViewById(R.id.image_fruit);
vh.textView = (TextView) convertView.findViewById(R.id.textview_name);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.textView.setText(fruit.getName());
vh.imageView.setImageResource(fruit.getImg());
vh.checkBox.setChecked(mManagerAllCheckBox[position]);
return convertView;
}
class ViewHolder {
TextView textView;
ImageView imageView;
CheckBox checkBox;
}
}
MyFruitAdapterActivity
public class MyFruitAdapterActivity extends AppCompatActivity {
private ListView mListView;
private List<Fruit> mFruits;
private FruitAdapter fruitAdapter;
private LayoutInflater inflater;
private View mListViewFoot;
private View mListViewHead;
private Button mButtonSeleckAll;
private Button mButtonSeleckFan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
mListView = (ListView) findViewById(R.id.listview1);
//LayoutInflater作用是将layout的xml布局文件实例化为View类对象。
inflater = getLayoutInflater();
//调用初始化数据方法
initData();
//FruitAdapter需要传递两个参数类型LayoutInflater , List
fruitAdapter = new FruitAdapter(inflater, mFruits);
//把适配器添加到ListView
//通过LayoutInflater的inflater对象实例化为XML文件的View类对象
mListViewFoot = inflater.inflate(R.layout.list_foot, null);
mListViewHead = inflater.inflate(R.layout.list_head, null);
//通过View类对象使用findViewById
mButtonSeleckAll= (Button) mListViewHead.findViewById(R.id.select_all);
mButtonSeleckFan= (Button) mListViewFoot.findViewById(R.id.select_fan);
//添加首尾,传入View类型参数
mListView.addFooterView(mListViewFoot);
mListView.addHeaderView(mListViewHead);
//将适配器添加到ListView
//注意此行的位置
mListView.setAdapter(fruitAdapter);
//全选按钮点击事件
mButtonSeleckAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fruitAdapter.selectAll();
}
});
//反选按钮点击事件
mButtonSeleckFan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fruitAdapter.selectFan();
}
});
//给ListView的每一个item添加点击事件
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("myfruit", "选中了" + mFruits.get(position - 1).getName());
fruitAdapter.select(position-1 );
}
});
}
//这样是重复的对象,当选中的时候会都选中
// public void initData() {
// mFruits = new ArrayList<>();
// Fruit apple = new Fruit("苹果", R.mipmap.a_logo1);
// Fruit banana = new Fruit("苹果", R.mipmap.a_logo1);
// Fruit grape = new Fruit("苹果", R.mipmap.a_logo1);
// for (int i = 0; i < 5; i++) {
// mFruits.add(apple);
// mFruits.add(banana);
// mFruits.add(grape);
// }
// }
//这样
public void initData() {
mFruits = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Fruit apple = new Fruit("苹果", R.mipmap.apple);
Fruit banana = new Fruit("香蕉", R.mipmap.banana);
Fruit grape = new Fruit("葡萄", R.mipmap.grape);
mFruits.add(apple);
mFruits.add(banana);
mFruits.add(grape);
}
}
}
Fruit
public class Fruit {
private String name;
private int img;
public Fruit( String name, int img){
this.name=name;
this.img=img;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
}
XML源码部分
listview.xml
list_fruit.xml
list_foot.xml
list_head.xml
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview1">
</ListView>
</LinearLayout>
list_fruit.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">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:focusable="false"/> <!---->
<ImageView
android:id="@+id/image_fruit"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="@+id/textview_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"/>
</LinearLayout>
list_foot.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/select_fan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="反选"/>
</LinearLayout>
list_head.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/select_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="全选"/>
</LinearLayout>