Android ListView 實(shí)例講解清晰易懂
一、前言
在某些場(chǎng)景下,單一文字的ListView Item已不適合當(dāng)前需求,因此需要我們自定義Item布局來滿足需求。下面我們來實(shí)現(xiàn)一個(gè)帶圖標(biāo)和文字的Item。
二、代碼展示
1.定義包含ListView的布局文件activity_main.xml,Activity在onCreate()時(shí)加載。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFE4C4"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#E4DDDD">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
2.定義Item布局文件listview_item.xml,創(chuàng)建SimpleAdapter對(duì)象時(shí)使用。
<?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"
android:orientation="horizontal"
android:background="#F0FFF0">
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="7"
android:textColor="#FF6E40"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
3.完善MainActivity.java代碼。
package com.example.listviewdemo2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private ListView mListView = null;
private List<Map<String, Object>> mListItems = null;
private Map<String, Object> mMap = null;
private SimpleAdapter mAdapter = null;
/* 圖片ID數(shù)組 */
private int[] mImageId = new int[] {R.drawable.num_0, R.drawable.num_1, R.drawable.num_2, R.drawable.num_3, R.drawable.num_4,
R.drawable.num_5, R.drawable.num_6, R.drawable.num_7, R.drawable.num_8, R.drawable.num_9, };
/* 文字列表數(shù)組 */
private String[] mTitle = new String[] {"數(shù)字 0", "數(shù)字 1", "數(shù)字 2", "數(shù)字 3", "數(shù)字 4", "數(shù)字 5", "數(shù)字 6", "數(shù)字 7", "數(shù)字 8", "數(shù)字 9", };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
mListView = findViewById(R.id.listview);
mListItems = new ArrayList<>();
for (int i = 0; i < mImageId.length; i++) {
mMap = new HashMap<>();
mMap.put("image", mImageId[i]);
mMap.put("title", mTitle[i]);
mListItems.add(mMap);
}
mAdapter = new SimpleAdapter(this, mListItems, R.layout.listview_item, new String[]{"title", "image"}, new int[]{R.id.textview, R.id.imageview});
mListView.setAdapter(mAdapter);
}
}
三、運(yùn)行效果
運(yùn)行效果如下圖:

到此這篇關(guān)于Android ListView 實(shí)例講解清晰易懂的文章就介紹到這了,更多相關(guān)Android ListView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android?APP瘦身shrinkResources使用問題詳解
這篇文章主要為大家介紹了Android?APP瘦身shrinkResources使用問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android實(shí)現(xiàn)使用微信登錄第三方APP的方法
這篇文章主要介紹了Android實(shí)現(xiàn)使用微信登錄第三方APP的方法,結(jié)合實(shí)例形式分析了Android微信登錄APP的操作步驟與具體功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-11-11
Android編程實(shí)現(xiàn)修改標(biāo)題欄位置使其居中的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)修改標(biāo)題欄位置使其居中的方法,涉及Android布局設(shè)置的簡單實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android如何使用圓形揭露動(dòng)畫巧妙地隱藏或顯示View詳解
Android開發(fā)中會(huì)遇到不少顯示和隱藏的問題,下面這篇文章主要給大家介紹了關(guān)于Android如何使用圓形揭露動(dòng)畫巧妙地隱藏或顯示View的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
實(shí)例詳解Android文件存儲(chǔ)數(shù)據(jù)方式
總體的來講,數(shù)據(jù)存儲(chǔ)方式有三種:一個(gè)是文件,一個(gè)是數(shù)據(jù)庫,另一個(gè)則是網(wǎng)絡(luò)。下面通過本文給大家介紹Android文件存儲(chǔ)數(shù)據(jù)方式,對(duì)android文件存儲(chǔ)數(shù)據(jù)相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01

