Android超詳細(xì)講解組件AdapterView的使用
概述
在Android應(yīng)用開發(fā)中,AdapterView是一類常用且非常重要的組件。我們常見的以列表的形式顯示信息的組件就是AdapterView的子類,稱為L(zhǎng)istview;我們經(jīng)常以網(wǎng)格方式瀏覽圖片縮略圖的組件也是AdapterView的子類,被稱為GridView;以下拉列表形式顯示可選項(xiàng)的組件也是AdapterView的子類,稱為Spinner;還有等等它們都是AdapterView的子類。
介紹AdapterView的編程模式
用Android的ListView組件以列表顯示Android系統(tǒng)中已經(jīng)安裝的所有程序信息。ListView,顧名思義,就是通過列表的形式向用戶展示信息。就像你手機(jī)設(shè)置里面的列表,它包含了你所有應(yīng)用程序的圖標(biāo), 應(yīng)用程序名稱(類似下圖)和入口Activity的類名。當(dāng)顯示的內(nèi)容超出物理屏幕可用區(qū)域時(shí),它還可以進(jìn)行滾動(dòng),就跟上期我們說的ScrollView的效果一樣。
如下圖:黃色大框的部分是一個(gè)ListView,深藍(lán)色小框框住的部分是一個(gè)列表中的一個(gè)列表項(xiàng),因此在程序中要使用ListView顯示信息,必須要做一下的工作。
(1)在界面布局中包含一個(gè)ListView組件
(2)對(duì)在列表中顯示的列表項(xiàng)進(jìn)行布局
(3)設(shè)計(jì)一個(gè)實(shí)現(xiàn)了Adapter接口的類,用于為L(zhǎng)istView組件提供需要顯示的數(shù)據(jù)。
Adapter
剛剛提到的列表組件(ListView),網(wǎng)格組件(GridView)和下拉列表組件(Spinner),它們都是Adapter的子類,這些組件只負(fù)責(zé)顯示數(shù)據(jù),而對(duì)于這些要顯示的數(shù)據(jù)則必須通過稱為Adapter的接口來進(jìn)行管理。以使用ListView顯示數(shù)據(jù)為例,AdapterView和Adapter接口的關(guān)系如下圖:
Adapter常用方法及其含義:
方法名字 | 含義 |
---|---|
int getCount() | 返回要顯示的數(shù)據(jù)集中的數(shù)據(jù)總數(shù) |
Object getItem(int position) | 返回?cái)?shù)據(jù)集中指定位置的數(shù)據(jù)對(duì)象 |
long getItemId(int position) | 返回?cái)?shù)據(jù)集中指定位置的數(shù)據(jù)的ID |
View getView(int position, View convertView, ViewGroup parent) | 將指定位置的數(shù)據(jù)構(gòu)建成一個(gè)可以顯示在AdapterView中的組件,并返回AdapterView進(jìn)行顯示 |
ListView使用
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:stretchColumns="2" > <ListView android:id="@+id/lv1" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
選擇New→Layout resoure file 創(chuàng)建
item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:stretchColumns="2" > <ListView android:id="@+id/lv1" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
myAdapater.java
package com.example.demo03_22; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class myAdapater extends BaseAdapter { private String[] books={"Java程序設(shè)計(jì)","Android應(yīng)用開發(fā)","oracle數(shù)據(jù)庫管理指南","JavaWeb程序設(shè)計(jì)","軟件工程之系統(tǒng)工程師之路"}; LayoutInflater inflater; int id_item; public myAdapater(Context context,int id_item){ this.id_item=id_item; inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return books.length; } @Override public Object getItem(int i) { return books[i]; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { TextView tv; tv=(TextView) inflater.inflate(id_item,viewGroup,false); tv.setText(books[i]); return tv; } }
MainActivity.java
package com.example.demo03_22; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv=(ListView) this.findViewById(R.id.lv1); myAdapater myAdapater=new myAdapater(this, R.layout.item); lv.setAdapter(myAdapater); } }
測(cè)試結(jié)果:
改進(jìn):添加圖片
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:stretchColumns="2" > <ListView android:id="@+id/lv1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </RelativeLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/id_booknames" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/book_phone" android:layout_width="80dp" android:layout_height="80dp" /> <TextView android:id="@+id/book_name" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" /> </LinearLayout>
myAdapater.java
package com.example.demo03_22; import android.annotation.SuppressLint; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class myAdapater extends BaseAdapter { private BookItem[] books={new BookItem("陳某人",R.drawable.dog), new BookItem("周某人",R.drawable.dog), new BookItem("鐘某人", R.drawable.dog), new BookItem("林某人",R.drawable.dog), new BookItem("濤某人",R.drawable.dog)}; LayoutInflater inflater; int id_item; public myAdapater(Context context,int id_item){ this.id_item=id_item; inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return books.length; } @Override public Object getItem(int position) { return books[position]; } @Override public long getItemId(int position) { return position; } @SuppressLint("ViewHolder")@Override public View getView(int position, View view, ViewGroup parent) { LinearLayout LL=(LinearLayout)inflater.inflate(id_item,parent,false) ; ImageView iv=(ImageView)LL.findViewById(R.id.book_phone); iv.setImageResource(books[position].photo); TextView tv; tv=(TextView)LL.findViewById(R.id.book_name); tv.setText(books[position].name); return LL; } /** * 定義一個(gè)圖片類 */ private class BookItem{ String name; int photo; public BookItem(String name,int photo){ this.name=name; this.photo=photo; } } }
MainActivity.java
package com.example.demo03_22; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv=(ListView) this.findViewById(R.id.lv1); myAdapater myAdapater=new myAdapater(this, R.layout.item); lv.setAdapter(myAdapater); lv.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView textView=(TextView)view.findViewById(R.id.book_name); String name=(String)textView.getText(); String text="確定選擇"+name+"今晚打火鍋嗎"; Toast.makeText(this,text,Toast.LENGTH_LONG).show(); } }
到此這篇關(guān)于Android超詳細(xì)講解組件AdapterView的使用的文章就介紹到這了,更多相關(guān)Android AdapterView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android基于名稱、修改時(shí)間、大小實(shí)現(xiàn)文件夾排序
這篇文章主要為大家詳細(xì)介紹了Android基于名稱、修改時(shí)間、大小實(shí)現(xiàn)文件夾排序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09Android軟鍵盤顯示模式及打開和關(guān)閉方式(推薦)
這篇文章主要介紹了Android軟鍵盤顯示模式及打開和關(guān)閉方式(推薦),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02InputFilter實(shí)現(xiàn)EditText文本輸入過濾器實(shí)例代碼解析
EditText是Android的文本輸入框控件。這篇文章給大家介紹 InputFilter實(shí)現(xiàn)EditText文本輸入過濾器實(shí)例代碼解析,需要的朋友一起看看吧2016-11-11Android Studio 透明狀態(tài)欄的實(shí)現(xiàn)示例
這篇文章主要介紹了Android Studio 透明狀態(tài)欄的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Android實(shí)現(xiàn)在屏幕上移動(dòng)圖片的方法
這篇文章主要介紹了Android實(shí)現(xiàn)在屏幕上移動(dòng)圖片的方法,實(shí)例分析了Android操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-06-06Android中@id和@+id及@android:id的區(qū)別介紹
這篇文章主要給大家介紹了關(guān)于Android中@id和@+id及@android:id的區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Android自定義橫向滑動(dòng)菜單的實(shí)現(xiàn)
這篇文章主要介紹了Android自定義橫向滑動(dòng)菜單的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05Android Studio一直處于Building的兩種解決方法
很多朋友都遇到過打開別人的項(xiàng)目一直處于Building‘XXX’Gradle project info的情況。下面小編給大家?guī)砹薃ndroid Studio一直處于Building的解決方法,感興趣的朋友一起看看吧2018-08-08Flutter實(shí)現(xiàn)抽屜動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)抽屜動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03