//定义了一个水果的实类
public class Fruit {
private int imageView;
private String textView;
public Fruit(int imageView, String textView) {
this.imageView = imageView;
this.textView = textView;
}
public int getImageView() {
return imageView;
}
public String getTextView() {
return textView;
}
}
3、fruit_activity.xml(子布局)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
LinearLayout>
4、 FruitAdapter.java(自定义的适配器)
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
//定义一个适配器(泛型)
public class FruitAdapter extends ArrayAdapter<Fruit> {
int textViewId;//在MainActivity中实例化传来的子布局ID
public FruitAdapter(Context context, int resource, List objects) {
super(context, resource, objects);
textViewId = resource;
}
@Override
//重写getView()每个子项滚动到屏幕是被调用
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position);//获取Fruit的对象
//利用from()生成LayoutInflater实例,inflate第一的是子布局既是(在MainActivity中实例化传来的子布局ID),第二个是当前布局
View view = LayoutInflater.from(getContext()).inflate(textViewId, parent, false);
//获得子布局的实例设置值。
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
TextView textView = (TextView) view.findViewById(R.id.textView);
imageView.setImageResource(fruit.getImageView());
textView.setText(fruit.getTextView());
return view;
}
}
5、activity_main.xml(父布局既是ListView)
<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"
tools:context="com.example.jcs.myfruitlistview.MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
ListView>
LinearLayout>
6、MainActivity.java(添加适配器)
package com.example.jcs.myfruitlistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List list = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
FruitAdapter fruitAdapter = new FruitAdapter(MainActivity.this, R.layout.fruit_activity, list);
ListView listView = (ListView) findViewById(R.id.list);
//ListView增加适配器
listView.setAdapter(fruitAdapter);
}
//实例化数组
private void initList() {
for (int i = 0; i < 10; i++) {
Fruit fruit1 = new Fruit(R.drawable.guan, "guan");
Fruit fruit2 = new Fruit(R.drawable.hua, "hua");
Fruit fruit3 = new Fruit(R.drawable.qin, "qin");
list.add(fruit1);
list.add(fruit2);
list.add(fruit3);
}
}
}
7、文件位置
8、下面是优化(定制的ListView的界面)
主要是在getView()中进行判断View是否存在?
**import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
//定义一个适配器(泛型)
public class FruitAdapter extends ArrayAdapter<Fruit> {
int textViewId;//在MainActivity中实例化传来的子布局ID
public FruitAdapter(Context context, int resource, List objects) {
super(context, resource, objects);
textViewId = resource;
}
@Override
//重写getView()每个子项滚动到屏幕是被调用
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder viewHolder;
Fruit fruit = getItem(position);//获取Fruit的对象
//判断加载布局是否为空(null加载,不为null不加载)
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(textViewId, parent, false);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) view.findViewById(R.id.imageView);
viewHolder.textView = (TextView) view.findViewById(R.id.textView);
//将ViewHolder存放到View中
view.setTag(viewHolder);
} else {
view = convertView;
//重新获取ViewHolder
viewHolder = (ViewHolder) view.getTag();
}
//用内部类的组件设置
viewHolder.imageView.setImageResource(fruit.getImageView());
viewHolder.textView.setText(fruit.getTextView());
return view;
}
//定义内部类存放组件
class ViewHolder {
ImageView imageView;
TextView textView;
}
}**
9、增加点击事件(定制的ListView的界面)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List list = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
FruitAdapter fruitAdapter = new FruitAdapter(MainActivity.this, R.layout.fruit_activity, list);
final ListView listView = (ListView) findViewById(R.id.list);
//ListView增加适配器
listView.setAdapter(fruitAdapter);
//注册setOnItemClickListener()监听器
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
//在数组中获取Fruit的实例
Fruit fruit = list.get(i);
Toast.makeText(MainActivity.this, fruit.getTextView(), Toast.LENGTH_SHORT).show();
}
});
}
//实例化数组
private void initList() {
for (int i = 0; i < 10; i++) {
Fruit fruit1 = new Fruit(R.drawable.guan, "guan");
Fruit fruit2 = new Fruit(R.drawable.hua, "hua");
Fruit fruit3 = new Fruit(R.drawable.qin, "qin");
list.add(fruit1);
list.add(fruit2);
list.add(fruit3);
}
}
}