Android超詳細(xì)講解組件AdapterView的使用
概述
在Android應(yīng)用開發(fā)中,AdapterView是一類常用且非常重要的組件。我們常見的以列表的形式顯示信息的組件就是AdapterView的子類,稱為Listview;我們經(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)對在列表中顯示的列表項(xiàng)進(jìn)行布局
(3)設(shè)計(jì)一個(gè)實(shí)現(xiàn)了Adapter接口的類,用于為ListView組件提供需要顯示的數(shù)據(jù)。

Adapter
剛剛提到的列表組件(ListView),網(wǎng)格組件(GridView)和下拉列表組件(Spinner),它們都是Adapter的子類,這些組件只負(fù)責(zé)顯示數(shù)據(jù),而對于這些要顯示的數(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ù)對象 |
| 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);
}
}測試結(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)在一個(gè)activity中添加多個(gè)listview的方法
這篇文章主要介紹了Android實(shí)現(xiàn)在一個(gè)activity中添加多個(gè)listview的方法,分析了Activity中添加listview的原理與具體實(shí)現(xiàn)方法,需要的朋友可以參考下2016-08-08
解決Android webview設(shè)置cookie和cookie丟失的問題
這篇文章主要介紹了解決Android webview設(shè)置cookie和cookie丟失的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android仿淘寶詳情頁面viewPager滑動(dòng)到最后一張圖片跳轉(zhuǎn)的功能
需要做一個(gè)仿淘寶客戶端ViewPager滑動(dòng)到最后一頁,再拖動(dòng)的時(shí)候跳到詳情的功能,剛開始我也迷糊了,通過查閱相關(guān)資料發(fā)現(xiàn)有好多種實(shí)現(xiàn)方法,下面小編給大家分享實(shí)例代碼,感興趣的朋友一起看看吧2017-03-03
非常實(shí)用的小功能 Android應(yīng)用版本的更新實(shí)例
這篇文章主要為大家詳細(xì)介紹了一個(gè)非常實(shí)用的小功能,Android應(yīng)用版本的更新實(shí)例,感興趣的小伙伴們可以參考一下2016-08-08
Android 自定義組件成JAR包的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 自定義組件成JAR包的實(shí)現(xiàn)方法的相關(guān)資料,偶爾會(huì)用到這樣的功能,如果你自己自定義的組件很好,需要的朋友可以參考下2016-11-11
Android編程調(diào)節(jié)屏幕亮度(背景燈)及保持背景燈常亮的方法
這篇文章主要介紹了Android編程調(diào)節(jié)屏幕亮度(背景燈)及保持背景燈常亮的方法,涉及Android屏幕相關(guān)屬性涉及技巧,需要的朋友可以參考下2016-01-01
解決Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問題
對于現(xiàn)在的 App 來說,布局頁面基本都會(huì)用到沉浸式狀態(tài)欄,單純的沉浸式狀態(tài)欄很容易解決,但是在華為手機(jī)上存在一個(gè)底部虛擬按鍵的問題,會(huì)導(dǎo)致頁面底部和頂部出現(xiàn)很大的問題,下面通過本文給大家分享Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問題,一起看看吧2017-07-07

