您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關ListView控件怎么在Android中使用,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
一、ListView的簡單用法
首先新建一個ListViewTest項目,并讓Android Studio自動創建好活動。然后修改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); } }
數組中的數據無法直接傳遞給ListView,需要借助適配器來實現。
ArrayAdapter的構造函數中依次傳入當前上下文,ListView子項布局的id,以及要適配的數據;
調用ListView的setAdapter()方法,將構建好的適配器對象傳遞進去,這樣ListView和數據之間的關聯就建立完成。
運行程序,可以通過滾動的方式來查看屏幕外的數據。
二、定制ListView的界面
首先準備一組圖片,分別對應上面提供的每一種水果(注意圖片大小盡量一致),放在drawable目錄下,注意命名不能出現大寫字母(比如Apple不合法);
在com.example.administrator.listviewtest下新建.java文件,定義一個實體類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的子項指定一個自定義布局,在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>
接下來創建一個自定義的適配器,同樣在com.example.administrator.listviewtest下新建.java文件,重寫了父類的一組構造函數,用于將上下文、ListView子項布局的id和數據都傳遞進來,命名為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); //獲取當前項的實例 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(); //初始化水果數據 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); } } }
以上就是ListView控件怎么在Android中使用,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。