一、订制ListView的界面
1.先建立一个Fruit水果类
package com.example.listviewtest; ? ? 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.然后建立一个展示水果的LayOut文件
<?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" android:orientation="vertical" > <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" android:layout_marginLeft="10dip" /> </LinearLayout>
?
然后新建一个FruitAdapter适配器,来继承ArrayAdapter
package com.example.listviewtest; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import android.content.Context; import java.util.List; ? import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; ? ? public class FruitAdapter extends ArrayAdapter<Fruit> { 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 = getItem(position);//获取当前项的Fruit实例 View view = LayoutInflater.from(getContext()).inflate(resourceId,null); ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image); TextView fruitName = (TextView) view.findViewById(R.id.fruit_name); fruitImage.setImageResource(fruit.getImageId()); fruitName.setText(fruit.getName()); return view; } }
最后我们完善我们的主程序
package com.example.listviewtest; ? import java.util.List; ? import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.*; //import java.lang.ArrayAdapter; ? public class MainActivity extends Activity { private List<Fruit> fruitList = new ArrayList<Fruit>(); // private String[] data = {"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruits();//初始化水果数据 FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList); // ArrayAdapter<String> adapter = new ArrayAdapter<String>( // MainActivity.this,android.R.layout.simple_list_item_1,data); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); } private void initFruits() { Fruit apple = new Fruit("Apple",R.drawable.apple_pic); fruitList.add(apple); Fruit banana = new Fruit("Banana",R.drawable.banana_pic); fruitList.add(banana); Fruit orange =new Fruit("Orange",R.drawable.orange_pic); fruitList.add(orange); } }
?
这样运行我们的代码,得到如下的结果
二、提升ListView的运行效率
1.我们通过下面的注释内容可以看出,这一段代码的重构,可以将画面的流畅程度提升,以及缓存一个View?Holder对象就可以大幅度提升运行速度。
package com.example.listviewtest; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import android.content.Context; import java.util.List; ? import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; ? ? public class FruitAdapter extends ArrayAdapter<Fruit> { 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 = getItem(position);//获取当前项的Fruit实例 // View view = LayoutInflater.from(getContext()).inflate(resourceId,null); //上面我们注释掉一个View实例,我们下一行先初始化一个View实例 View view; ViewHolder viewHolder; if(convertView == null) {//如果这个View不存在的话,我们就直接建一个View view = LayoutInflater.from(getContext()).inflate(resourceId,null); viewHolder = new ViewHolder(); viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image); viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name); view.setTag(viewHolder);//将ViewHolder存储在View中 }else { view = convertView;//如果View已经存在了,那么我们就复用已经存在了的View,这样就可以避免再次重新生成View,节省了运行时间 viewHolder = (ViewHolder) view.getTag();//调用getTag方法,把ViewHolder重新取出。 } //这里我们建立了一个内部类,用来存储已经生成了的fruitImage和fruieName的实例,就省的每次都需要通过findViewById的方法,来去内存中查找目标实例 viewHolder.fruitImage.setImageResource(fruit.getImageId()); viewHolder.fruitName.setText(fruit.getName()); ? // ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image); // TextView fruitName = (TextView) view.findViewById(R.id.fruit_name); // fruitImage.setImageResource(fruit.getImageId()); // fruitName.setText(fruit.getName()); return view; } class ViewHolder{ ImageView fruitImage; TextView fruitName; } }
?
三、ListView的点击事件
如果只列出了几条内容,我们不能通过点击?List来来获取我们想要知道的信息,那将是毫无意义的。
接下来就在MainActivity?中来注册我们的点击事件。
package com.example.listviewtest; ? import java.util.List; ? import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; ? import java.util.*; //import java.lang.ArrayAdapter; ? public class MainActivity extends Activity { private List<Fruit> fruitList = new ArrayList<Fruit>(); // private String[] data = {"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruits();//初始化水果数据 FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList); // ArrayAdapter<String> adapter = new ArrayAdapter<String>( // MainActivity.this,android.R.layout.simple_list_item_1,data); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); //接下来就来注册点击事件 listView.setOnItemClickListener(new 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(); } }); } private void initFruits() { Fruit apple = new Fruit("Apple",R.drawable.apple_pic); fruitList.add(apple); Fruit banana = new Fruit("Banana",R.drawable.banana_pic); fruitList.add(banana); Fruit orange =new Fruit("Orange",R.drawable.orange_pic); fruitList.add(orange); } }
?
四、源码:
1.项目地址
https://github.com/ruigege66/Android/tree/master/ListViewTest
2.CSDN:https://blog.csdn.net/weixin_44630050
3.博客园:https://www.cnblogs.com/ruigege0000/
4.欢迎关注微信公众号:傅里叶变换,个人公众号,仅用于学习交流,后台回复”礼包“,获取大数据学习资料