一、訂製ListView的界面 1.先建立一個Fruit水果類 package com.example.listviewtest; public class Fruit { private String name; private int imageId; public Fruit(Stri ...
一、訂製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.我們通過下麵的註釋內容可以看出,這一段代碼的重構,可以將畫面的流暢程度提升,以及緩存一個ViewHolder對象就可以大幅度提升運行速度。
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.歡迎關註微信公眾號:傅里葉變換,個人公眾號,僅用於學習交流,後臺回覆”禮包“,獲取大數據學習資料