欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android控件ListView使用方法詳解

 更新時間:2017年07月17日 15:27:50   作者:啊嗯哼  
這篇文章主要為大家詳細(xì)介紹了Android控件ListView的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Android控件ListView使用方法介紹,具體如下

一、ListView的簡單用法

首先新建一個ListViewTest項(xiàng)目,并讓Android Studio自動創(chuà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)行程序,可以通過滾動的方式來查看屏幕外的數(shù)據(jù)。

二、定制ListView的界面

首先準(zhǔn)備一組圖片,分別對應(yīng)上面提供的每一種水果(注意圖片大小盡量一致),放在drawable目錄下,注意命名不能出現(xiàn)大寫字母(比如Apple不合法);

在com.example.administrator.listviewtest下新建.java文件,定義一個實(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)指定一個自定義布局,在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)建一個自定義的適配器,同樣在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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android利用爬蟲實(shí)現(xiàn)模擬登錄的實(shí)現(xiàn)實(shí)例

    Android利用爬蟲實(shí)現(xiàn)模擬登錄的實(shí)現(xiàn)實(shí)例

    這篇文章主要介紹了Android利用爬蟲實(shí)現(xiàn)模擬登錄的實(shí)現(xiàn)實(shí)例的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-09-09
  • MUI整合上拉下拉的寫法

    MUI整合上拉下拉的寫法

    在制作APP的時候下拉刷新和上拉加載幾乎都是一起使用的,今天以MUI的寫法為例給大家分享一下整合的寫法。
    2017-11-11
  • Flutter常用的布局和事件示例詳解

    Flutter常用的布局和事件示例詳解

    這篇文章主要給大家介紹了關(guān)于Flutter常用的布局和事件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Android開發(fā)之TabActivity用法實(shí)例詳解

    Android開發(fā)之TabActivity用法實(shí)例詳解

    這篇文章主要介紹了Android開發(fā)之TabActivity用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android擴(kuò)展Activity實(shí)現(xiàn)標(biāo)簽頁效果的具體步驟與相關(guān)技巧,需要的朋友可以參考下
    2016-03-03
  • Android自定義星星可滑動評分控件

    Android自定義星星可滑動評分控件

    這篇文章主要介紹了Android自定義星星可滑動評分控件,通過線性布局結(jié)合ImageView實(shí)現(xiàn)評分控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Android仿百度福袋紅包界面

    Android仿百度福袋紅包界面

    過年了,紅包來襲,紅包是怎么來的大家知道嗎?這篇文章主要介紹了Android仿百度福袋紅包界面,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Android開發(fā)實(shí)例之登錄界面的實(shí)現(xiàn)

    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的方法

    這篇文章主要介紹了Android App開發(fā)中將View或Drawable轉(zhuǎn)為Bitmap的方法,其中View轉(zhuǎn)換時作者特別提到了getDrawingCache=null問題的解決方法,需要的朋友可以參考下
    2016-03-03
  • Android 實(shí)現(xiàn)加載大圖片的方法

    Android 實(shí)現(xiàn)加載大圖片的方法

    對于超大的圖片,如果不縮放的話,容易導(dǎo)致內(nèi)存溢出。而經(jīng)過處理后,無論多大的圖片,都能夠在手機(jī)屏幕上加載出來,不會導(dǎo)致內(nèi)存溢出。本文將對Android 加載大圖片的實(shí)現(xiàn)方法進(jìn)行介紹,下面跟著小編一起來看下吧
    2017-04-04
  • Android編程實(shí)現(xiàn)為ListView創(chuàng)建上下文菜單(ContextMenu)的方法

    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

最新評論