1.Fruit.class 定义为水果实体类
public class Fruit { private String name; private int imageId; public Fruit(String name,int imageId) { this.name = name; this.imageId = imageId; } public String getName(){ return name; } public int getImageId() { return imageId; } }
2.fruit_item 为listview的子项定义一个布局
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/fruit_image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/fruit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" /> LinearLayout>
3.FruitAdapter.class 定义一个自定义的适配器,并将泛型指定为Fruit类
/** * 自定义的适配器 */ public class FruitAdapter extends ArrayAdapter{ private int resourceId; public FruitAdapter(Context context,int textViewResourceId, List objects) { super(context,textViewResourceId,objects); resourceId = textViewResourceId; } @Override public View getView(int postion, View convertView, ViewGroup parent ) { Fruit fruit = getItem(postion);//获取当前的的实例 View view; ViewHolder viewHolder; if (convertView ==null) { view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false); //false表不增父布局 viewHolder = new ViewHolder(); viewHolder.fruitImageId = (ImageView)view.findViewById(R.id.fruit_image); viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name); view.setTag(viewHolder);//将viewHolder储存在view中 } else { view = convertView; viewHolder = (ViewHolder) view.getTag();//重新获取ViewHolder; } viewHolder.fruitImageId.setImageResource(fruit.getImageId()); viewHolder.fruitName.setText(fruit.getName()); return view; } class ViewHolder{ ImageView fruitImageId; TextView fruitName; } }
4.MainActivity.java 主活动
public class MainActivity extends AppCompatActivity { private ListfruitList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruit(); FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList); ListView listView = (ListView)findViewById(R.id.list_view); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { Fruit fruit = fruitList.get(position); Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show(); } }); } public void initFruit() { for (int i =0;i< 4 ; i ++ ){ Fruit apple = new Fruit("apple",R.drawable.apple); fruitList.add(apple); Fruit banana = new Fruit("banana",R.drawable.banana); fruitList.add(banana); Fruit orange = new Fruit("orange",R.drawable.orange); fruitList.add(orange); Fruit pear = new Fruit("pear",R.drawable.pear); fruitList.add(pear); Fruit watermelon = new Fruit("pear",R.drawable.watermelon); fruitList.add(watermelon); } } }
6.activity_layout.xml 主布局文件
<LinearLayout android:id="@+id/activity_main" 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.sh.customlistviewtest.MainActivity"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content"> ListView> LinearLayout>