Android控件ListView使用方法詳解
Android控件ListView使用方法介紹,具體如下
一、ListView的簡單用法
首先新建一個(gè)ListViewTest項(xiàng)目,并讓Android Studio自動(dòng)創(chuàng)建好活動(dòng)。然后修改activity_main.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"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout>
接下來修改MainActivity中的代碼:
public class MainActivity extends AppCompatActivity {
private String[] data={"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango","Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
}
}
數(shù)組中的數(shù)據(jù)無法直接傳遞給ListView,需要借助適配器來實(shí)現(xiàn)。
ArrayAdapter的構(gòu)造函數(shù)中依次傳入當(dāng)前上下文,ListView子項(xiàng)布局的id,以及要適配的數(shù)據(jù);
調(diào)用ListView的setAdapter()方法,將構(gòu)建好的適配器對象傳遞進(jìn)去,這樣ListView和數(shù)據(jù)之間的關(guān)聯(lián)就建立完成。
運(yùn)行程序,可以通過滾動(dòng)的方式來查看屏幕外的數(shù)據(jù)。

二、定制ListView的界面
首先準(zhǔn)備一組圖片,分別對應(yīng)上面提供的每一種水果(注意圖片大小盡量一致),放在drawable目錄下,注意命名不能出現(xiàn)大寫字母(比如Apple不合法);
在com.example.administrator.listviewtest下新建.java文件,定義一個(gè)實(shí)體類Fruit,作為ListView適配器的適配類型。
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;
}
}
然后為ListView的子項(xiàng)指定一個(gè)自定義布局,在layout目錄下新建fruit_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="wrap_content"> <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>
接下來創(chuàng)建一個(gè)自定義的適配器,同樣在com.example.administrator.listviewtest下新建.java文件,重寫了父類的一組構(gòu)造函數(shù),用于將上下文、ListView子項(xiàng)布局的id和數(shù)據(jù)都傳遞進(jìn)來,命名為FruitAdapter,代碼如下:
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); //獲取當(dāng)前項(xiàng)的實(shí)例
View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
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;
}
}
下面修改MainActivity中的代碼:
package com.example.administrator.listviewtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList=new ArrayList<>();
// private String[] data={"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango","Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits(); //初始化水果數(shù)據(jù)
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(){
for(int i=0;i<2;i++){
Fruit apple=new Fruit("Apple",R.drawable.apple);
fruitList.add(apple);
Fruit orange=new Fruit("Orange",R.drawable.orange);
fruitList.add(orange);
Fruit banana=new Fruit("Banana",R.drawable.banana);
fruitList.add(banana);
Fruit waterlenmo=new Fruit("Waterlemon",R.drawable.waterlemon);
fruitList.add(waterlenmo);
Fruit pear=new Fruit("Pear",R.drawable.pear);
fruitList.add(pear);
Fruit grape=new Fruit("Grape",R.drawable.grape);
fruitList.add(grape);
Fruit pineapple=new Fruit("Pineapple",R.drawable.pineapple);
fruitList.add(pineapple);
Fruit strawberry=new Fruit("Strawberry",R.drawable.straw);
fruitList.add(strawberry);
Fruit cherry=new Fruit("Cherry",R.drawable.cherry);
fruitList.add(cherry);
Fruit mango=new Fruit("mango",R.drawable.mango);
fruitList.add(mango);
}
}
}
運(yùn)行程序后,效果圖如下(圖片大小后來才意識到,懶得換了……):

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解xamarin Android 實(shí)現(xiàn)ListView萬能適配器
- Android 中通過ViewDragHelper實(shí)現(xiàn)ListView的Item的側(cè)拉劃出效果
- Android 實(shí)現(xiàn)ListView的點(diǎn)擊變色的實(shí)例
- Android Adapter里面嵌套ListView實(shí)例詳解
- Android ListView實(shí)現(xiàn)ImageLoader圖片加載的方法
- Android ListView滑動(dòng)改變標(biāo)題欄背景漸變效果
- Android使用ListView實(shí)現(xiàn)滾輪的動(dòng)畫效果實(shí)例
- Android ListView中headerview的動(dòng)態(tài)顯示和隱藏的實(shí)現(xiàn)方法
相關(guān)文章
Android利用爬蟲實(shí)現(xiàn)模擬登錄的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Android利用爬蟲實(shí)現(xiàn)模擬登錄的實(shí)現(xiàn)實(shí)例的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
Android開發(fā)之TabActivity用法實(shí)例詳解
這篇文章主要介紹了Android開發(fā)之TabActivity用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android擴(kuò)展Activity實(shí)現(xiàn)標(biāo)簽頁效果的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-03-03
Android開發(fā)實(shí)例之登錄界面的實(shí)現(xiàn)
本文主要介紹Android 登錄界面實(shí)現(xiàn),這里主要講解類似Twitter的登錄界面的實(shí)現(xiàn),有興趣的小伙伴可以參考下2016-08-08
Android App開發(fā)中將View或Drawable轉(zhuǎn)為Bitmap的方法
這篇文章主要介紹了Android App開發(fā)中將View或Drawable轉(zhuǎn)為Bitmap的方法,其中View轉(zhuǎn)換時(shí)作者特別提到了getDrawingCache=null問題的解決方法,需要的朋友可以參考下2016-03-03
Android編程實(shí)現(xiàn)為ListView創(chuàng)建上下文菜單(ContextMenu)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)為ListView創(chuàng)建上下文菜單(ContextMenu)的方法,簡單分析了上下文菜單的功能及ListView創(chuàng)建上下文菜單(ContextMenu)的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-02-02

