1、Adapter是ListView与数据之间的桥梁
2、ListView在开始绘制的时候,系统首先调用getCount()方法,根据返回值得到ListView的长度,然后根据这个长度,调用getView()一行一行地绘制ListView的每一项。
3、当ListView的每一项将要显示时,都会调用Adapter的getView()方法返回一个View。
4、ListView中有多少项,就调用多少次getView()方法去创建每一项的View,这一过程是耗时操作。
public class MyListViewActivity extends AppCompatActivity {
private ListView mListView;
private List<Student> mData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_list_view);
mListView= (ListView) findViewById(R.id.listview);
initData();
LayoutInflater inflater=getLayoutInflater();
Studentadapter adapter=new Studentadapter(inflater,mData);
mListView.setAdapter(adapter);
}
private void initData() {
mData=new ArrayList<>();
Student zhangsan=new Student("张三","20","男","吃",R.mipmap.uqiwei);
Student lisi=new Student("李四","22","女","睡觉",R.mipmap.yangmi);
Student wangwu=new Student("王五","25","男","打游戏",R.mipmap.ic_launcher);
Student zhaoliu=new Student("赵六","23","女","逛街",R.mipmap.uqiwei);
for(int i=0;i<50;i++) {
mData.add(zhangsan);
mData.add(lisi);
mData.add(wangwu);
mData.add(zhaoliu);
}
}
"****************************************************"
public class Studentadapter extends BaseAdapter {
private List<Student> mData;
private LayoutInflater mInflater;
public Studentadapter(LayoutInflater inflater, List<Student> data) {
mInflater = inflater;
mData = data;
}
@Override
public int getCount() {
//得到ListView要显示数据的条数
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) {
//LayoutInflater
Student student = mData.get(position);
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.simple_adapter, null);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageview_photo);
viewHolder.textView_name = (TextView) convertView.findViewById(R.id.textview_name);
viewHolder.textView_age = (TextView) convertView.findViewById(R.id.textview_age);
viewHolder.textView_sex = (TextView) convertView.findViewById(R.id.textview_sex);
viewHolder.textView_hobby = (TextView) convertView.findViewById(R.id.textview_hobby);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView_name.setText(student.getName());
viewHolder.textView_age.setText(student.getAge());
viewHolder.textView_hobby.setText(student.getHobby());
viewHolder.textView_sex.setText(student.getSex());
viewHolder.imageView.setImageResource(student.getImg());
return convertView;
}
class ViewHolder {
TextView textView_name;
TextView textView_age;
TextView textView_sex;
TextView textView_hobby;
ImageView imageView;
}
}
"*******************************************"
<?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">
<ImageView
android:id="@+id/imageview_photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/yangmi"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="20dp">
<TextView
android:id="@+id/textview_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<TextView
android:id="@+id/textview_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄"/>
<TextView
android:id="@+id/textview_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别"/>
</LinearLayout>
<TextView
android:id="@+id/textview_hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好"/>
</LinearLayout>
</LinearLayout>
"********************************************************"
public class Student {
private String name;
private String hobby;
private String age;
private String sex;
private int img;
public Student(String name,String age,String sex,String hobby,int img){
this.name=name;
this.age=age;
this.sex=sex;
this.hobby=hobby;
this.img=img;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public void setAge(String age) {
this.age = age;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public String getHobby() {
return hobby;
}
public String getAge() {
return age;
}
}
<?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">
<ListView
android:id="@+id/listview_fruit"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
public class MyFruitActivity extends AppCompatActivity {
private ListView mListView;
private List<Fruit> mFruits;
private Fruitadapter adapter;
private LayoutInflater Inflater;
private View mHeaderView;
private Button mBtnCheckedAll;
private View mFooterView;
private Button mBtnCheckedOver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_fruit);
mListView= (ListView) findViewById(R.id.listview_fruit);
Inflater=getLayoutInflater();
initData();
mFooterView=Inflater.inflate(R.layout.fruit_footer, null);
mHeaderView=Inflater.inflate(R.layout.fruit_header,null);
mBtnCheckedOver= (Button) mFooterView.findViewById(R.id.button_overChecked);
mBtnCheckedAll= (Button) mHeaderView.findViewById(R.id.button_allChecked);
mBtnCheckedOver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.checkedOver();
}
});
mBtnCheckedAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//此处要全选
adapter.checkedAll();
}
});
mListView.addHeaderView(mHeaderView);
mListView.addFooterView(mFooterView);
adapter=new Fruitadapter(Inflater,mFruits);
mListView.setAdapter(adapter);
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());
adapter.itemSelect(position-1);
}
});
}
private void initData() {
mFruits=new ArrayList<>();
for(int i=0;i<50;i++) {
Fruit apple=new Fruit("苹果",R.mipmap.pineapple);
Fruit banana=new Fruit("香蕉",R.mipmap.fbanana);
Fruit strawberry=new Fruit("葡萄",R.mipmap.fstrawberry);
mFruits.add(apple);
mFruits.add(banana);
mFruits.add(strawberry);
}
}
<?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">
<Button
android:id="@+id/button_allChecked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"/>
</LinearLayout>
"********************************************************"
<?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">
<Button
android:id="@+id/button_overChecked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="反选"/>
</LinearLayout>
<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:gravity="center"
android:orientation="horizontal"
tools:context="com.example.administrator.mywiget.MyFruitActivity">
<CheckBox
android:id="@+id/checkbox_choose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:text="选择水果"/>
<ImageView
android:id="@+id/imageview_fruit"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/peach"/>
<TextView
android:id="@+id/textview_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="苹果"
/>
</LinearLayout>
public class Fruit {
private String name;
private int Img;
private boolean isChecked;
public boolean isChecked() {
return isChecked;
}
public void setIsChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImg() {
return Img;
}
public void setImg(int img) {
Img = img;
}
public Fruit(String name, int img) {
this.name = name;
Img = img;
}
}
public class Fruitadapter extends BaseAdapter {
private List<Fruit> mFruits;
private LayoutInflater mInflater;
private boolean[] mManagerAllCheckBox;
public Fruitadapter(LayoutInflater Inflater, List<Fruit> Fruits) {
mInflater = Inflater;
mFruits = Fruits;
//有多少条数据就有多少个checkBox
mManagerAllCheckBox = new boolean[Fruits.size()];
}
/** * 全部选择 */
public void checkedAll(){
for(int i=0;i<mManagerAllCheckBox.length;i++){
mManagerAllCheckBox[i]=true;
}
notifyDataSetChanged();
}
public void checkedOver(){
for (int i=0;i<mManagerAllCheckBox.length;i++){
mManagerAllCheckBox[i]=!mManagerAllCheckBox[i];
}
notifyDataSetChanged();
}
public void itemSelect(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 viewHolder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.activity_my_fruit, null);
viewHolder = new ViewHolder();
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox_choose);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageview_fruit);
viewHolder.textView = (TextView) convertView.findViewById(R.id.textview_name);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Fruit fruit = mFruits.get(position);
viewHolder.imageView.setImageResource(fruit.getImg());
viewHolder.textView.setText(fruit.getName());
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("myFruit", "选择框发生的变化:" + isChecked);
mManagerAllCheckBox[position] = isChecked;
notifyDataSetChanged();
}
});
viewHolder.checkBox.setChecked(mManagerAllCheckBox[position]);
return convertView;
}
class ViewHolder {
CheckBox checkBox;
ImageView imageView;
TextView textView;
}
}