一、效果图:
二、代码实现
2.1ListViewActivity
package cn.ac.ucas.yp.uiwidgettest
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
import java.util.ArrayList
import java.util.List
public class ListViewActivity extends AppCompatActivity {
private String[] data = {"Apple", "Orange", "Watermelon", "Pear"}
private List<Fruit> fruitList = new ArrayList<Fruit>()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.listview)
initFruits()
ListView listview1 = (ListView) findViewById(R.id.listview1)
FruitAdapter fruitAdapter = new FruitAdapter(ListViewActivity.this, R.layout.fruit_item, fruitList)
listview1.setAdapter(fruitAdapter)
listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fruit fruit = fruitList.get(position)
Toast.makeText(ListViewActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show()
}
})
}
private void initFruits() {
Fruit apple = new Fruit("Apple", R.drawable.apple, "治下痢、降血脂、滋润皮肤。")
fruitList.add(apple)
Fruit banana = new Fruit("banana", R.drawable.bananas, "香蕉被称为百果之冠,具有润肠通便,清热解毒,健脑益智,通血脉,填精髓,降血压等功效。")
fruitList.add(banana)
Fruit cherry = new Fruit("cherry", R.drawable.cherry, "樱桃补中益气,祛风胜湿")
fruitList.add(cherry)
Fruit grape = new Fruit("grape", R.drawable.grape, "葡萄营养丰富、酸甜可口,因此成为世界四大水果(苹果、葡萄、柑橘和香蕉)之一")
fruitList.add(grape)
Fruit kiwi = new Fruit("kiwi", R.drawable.kiwi, "能促使血液循环顺畅,增进性能力。")
fruitList.add(kiwi)
Fruit lemon = new Fruit("lemon", R.drawable.lemon, "可增强消化、出汗过多、食欲不振、体力倦怠、减肥解酒。")
fruitList.add(lemon)
Fruit mango = new Fruit("mango", R.drawable.mango, "芒果果实营养价值极高,维生素A的含量比杏子还要多出1倍 ")
fruitList.add(mango)
Fruit orange = new Fruit("orange", R.drawable.orange, "桔子含水量高、营养丰富")
fruitList.add(orange)
Fruit pear = new Fruit("pear", R.drawable.pear, "清解热毒、镇咳化痰。")
fruitList.add(pear)
Fruit persimmon = new Fruit("persimmon", R.drawable.persimmon, "柿子营养丰富,但要吃得健康,不过要注意一些与柿子相克的食物 ")
fruitList.add(persimmon)
Fruit pineapple = new Fruit("pineapple", R.drawable.pineapple, "菠萝清香宜人,汁多味甜,广受人们喜爱。")
fruitList.add(pineapple)
Fruit strawberry = new Fruit("strawberry", R.drawable.strawberry,
"去火、解暑、清热;促进胃肠蠕动、帮助消化、改善便秘;预防痔疮、肠癌 ")
fruitList.add(strawberry)
Fruit watermelon = new Fruit("watermelon", R.drawable.watermelon, "西瓜味道甘味多汁,清爽解渴,是盛夏佳果。")
fruitList.add(watermelon)
}
}
2.2ListViewActivity对应的布局文件listview.xml
<?xml version="1.0" encoding="utf-8"?>
<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" 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="cn.ac.ucas.yp.uiwidgettest.ListViewActivity">
<ListView android:id="@+id/listview1" android:layout_width="match_parent" android:layout_height="match_parent"></ListView>
</RelativeLayout>
2.3fruit.java
package cn.ac.ucas.yp.uiwidgettest;
/** * Created by yp on 16/4/15. */
public class Fruit {
private String name;
private int imageId;
private String introduction;
public Fruit(String name, int imageId,String introduction) {
this.name = name;
this.introduction = introduction;
this.imageId = imageId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
2.4FruitAdapter.java
package cn.ac.ucas.yp.uiwidgettest;
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;
/** * Created by yp on 16/4/15. */
public class FruitAdapter extends ArrayAdapter {
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = (Fruit) getItem(position);
View view;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
} else {
view = convertView;
}
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_img);
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
TextView fruitIntroduction = (TextView) view.findViewById(R.id.fruit_introduction);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
fruitIntroduction.setText(fruit.getIntroduction());
return view;
}
}
2.4fruit_item.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">
<LinearLayout android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical">
<ImageView android:id="@+id/fruit_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" />
<TextView android:id="@+id/fruit_name" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginLeft="10dip" android:gravity="center" android:paddingLeft="0dp" />
</LinearLayout>
<LinearLayout android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="2">
<TextView android:id="@+id/fruit_introduction" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>