先来看看一些基本属性和常见问题。
listview 常用属性
cachecolorhint:缓存默认颜色一般给全透明
android:cacheColorHint=”#00ffffff”
divder:分隔线
android:divider=”#000000”
android:dividerHeight=”1dp”
改变按下的颜色:
item的背景改为drawable中的pressed.xml
加头部和底部:
默认position会加一
必须在setadapter之前加载
checkbox问题
。。。因为是同一个对象
adapter中有一个及时刷新的方法,在不滑动的情况下就可以刷新界面
notifyDataSetchange
一般在设置点击事件之后,都要使用该方法
点击事件问题:点击listview没有反应。只有点击checkbox才有反应
事件传递
屏幕 activity viewGroup
checkbox button
因为focusable默认为true
设为false则可以点击以外的区域
<RelativeLayout 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"
tools:context="com.lingzhuo.test4.FriutActivity">
<ListView
android:id="@+id/mListViewFruit"
android:cacheColorHint="#00ffffff"
android:divider="#000000"
android:dividerHeight="1dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
ListView>
RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<CheckBox
android:id="@+id/mCheckBoxFruit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:text="选择水果"
/>
<ImageView
android:id="@+id/mImageViewFruit"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<TextView
android:id="@+id/mTextViewFruit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水果的名字"
/>
LinearLayout>
package com.lingzhuo.test4.modle;
/**
* Created by heinika on 2015/8/25.
*/
public class Fruit {
private String name;
private int image;
private boolean isChecked;
public boolean changeChecked(){
isChecked = !isChecked;
return isChecked;
}
public boolean getIsChecked() {
return isChecked;
}
public void setIsChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public Fruit(String name, int image) {
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
package com.lingzhuo.test4.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.lingzhuo.test4.R;
import com.lingzhuo.test4.modle.Fruit;
import java.util.List;
/**
* Created by heinika on 2015/8/25.
*/
public class FruitAdapter extends BaseAdapter{
private List fruits;
private LayoutInflater inflater;
private boolean isChecked;
public FruitAdapter(LayoutInflater inflater,List data){
this.inflater = inflater;
fruits = data;
}
@Override
public int getCount() {
return fruits.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) {
Fruit fruit = fruits.get(position);
FruitHold fruitHold=null;
if(convertView==null){
fruitHold = new FruitHold();
convertView = inflater.inflate(R.layout.item_fruit_adapter,null);
fruitHold.mCheckBoxFruit = (CheckBox) convertView.findViewById(R.id.mCheckBoxFruit);
fruitHold.mImageViewFruit = (ImageView) convertView.findViewById(R.id.mImageViewFruit);
fruitHold.mTextViewFruit = (TextView) convertView.findViewById(R.id.mTextViewFruit);
convertView.setTag(fruitHold);
}else{
fruitHold = (FruitHold) convertView.getTag();
}
fruitHold.mImageViewFruit.setImageResource(fruit.getImage());
fruitHold.mTextViewFruit.setText(fruit.getName());
fruitHold.mCheckBoxFruit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//局部变量只有转变为常量才能传入匿名内部类
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
fruits.get(position).setIsChecked(isChecked);
}
});
fruitHold.mCheckBoxFruit.setChecked(fruit.getIsChecked());
return convertView;
}
// public void changeCheck(int position){
// fruits.get(position).setIsChecked(!fruits.get(position).getIsChecked());
// notifyDataSetChanged(); //刷新数据
// }
public void allSelect(){
for(int i=0;itrue);
}
notifyDataSetChanged();
}
public void allRevrse(){
for(int i=0;i
package com.lingzhuo.test4;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import com.lingzhuo.test4.adapter.FruitAdapter;
import com.lingzhuo.test4.modle.Fruit;
import java.util.ArrayList;
public class FriutActivity extends Activity {
private ArrayList fruits;
private LayoutInflater inflater;
private ListView mListViewFruit;
private FruitAdapter fruitAdapter;
private View mHeaderView;
private Button mBtnAllSelect;
private Button mBtnAllRevrse;
private View mFootView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friut);
mListViewFruit = (ListView) findViewById(R.id.mListViewFruit);
inflater = getLayoutInflater(); //只有activity才有getLayoutInflater()方法
fruits = initFruits();
fruitAdapter = new FruitAdapter(inflater,fruits);
mHeaderView = inflater.inflate(R.layout.all_select_button,null);
mListViewFruit.addHeaderView(mHeaderView); //会导致position加一
mBtnAllSelect = (Button) mHeaderView.findViewById(R.id.mBtnAllSelect);
mBtnAllSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fruitAdapter.allSelect();
}
});
mFootView = inflater.inflate(R.layout.all_revrse_button,null);
mListViewFruit.addFooterView(mFootView);
mBtnAllRevrse = (Button) mFootView.findViewById(R.id.mBtnAllRevrse);
mBtnAllRevrse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fruitAdapter.allRevrse();
}
});
mListViewFruit.setAdapter(fruitAdapter);
mListViewFruit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
fruits.get(position-1).setIsChecked(fruits.get(position-1).changeChecked()); //当点击item
fruitAdapter.notifyDataSetChanged(); //刷新数据
}
});
}
private ArrayList initFruits() {
fruits = new ArrayList();
for(int i =0;i<10;i++) {
fruits.add(new Fruit("apple", R.mipmap.apple));
fruits.add(new Fruit("pineapple", R.mipmap.pineapple));
fruits.add(new Fruit("strawberry", R.mipmap.strawberry));
fruits.add(new Fruit("watermelon", R.mipmap.watermelon));
fruits.add(new Fruit("apple", R.mipmap.apple));
}
return fruits;
}
}
package com.lingzhuo.test4.adapter;
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.lingzhuo.test4.R;
import com.lingzhuo.test4.modle.Student;
import java.util.List;
/**
* Created by Heinika on 2015/8/24.
*/
public class StudentAdapter extends BaseAdapter{
private List mData;
private LayoutInflater mInflater;
//将inflater和数据传到适配器中
public StudentAdapter(LayoutInflater inflater,List data){
this.mInflater = inflater;
this.mData = data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Student student = mData.get(position);
ViewHold viewHold=null;
if(convertView==null){ //只有充满的的时候才不为空
viewHold = new ViewHold();
convertView = mInflater.inflate(R.layout.item_simple_adapter,null); //避免每次重复加载
viewHold.mImageViewPhoto = (ImageView) convertView.findViewById(R.id.mImageViewPhoto); //findviewbyid为耗时操作
viewHold.textView_name = (TextView) convertView.findViewById(R.id.tv_name); //只当convertView为空时才需要findviewbyid
viewHold.textView_age = (TextView) convertView.findViewById(R.id.tv_age);
viewHold.textView_sex = (TextView) convertView.findViewById(R.id.tv_sex);
convertView.setTag(viewHold); //将viewHold存到convertView
}else{ //convertView不为空的加载,convertView创建到一定个数就不再创建
viewHold = (ViewHold) convertView.getTag();
}
viewHold.mImageViewPhoto.setImageResource(student.getImgId());
viewHold.textView_name.setText(student.getName());
viewHold.textView_age.setText(student.getAge()+"");
viewHold.textView_sex.setText(student.getSex());
return convertView;
}
class ViewHold{
ImageView mImageViewPhoto;
TextView textView_name;
TextView textView_age;
TextView textView_sex;
}
}