亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android ListView 實例講解清晰易懂

 更新時間:2021年09月09日 11:38:53   作者:Amosstan  
這篇文章主要通過實例介紹了Android ListView,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、前言

在某些場景下,單一文字的ListView Item已不適合當(dāng)前需求,因此需要我們自定義Item布局來滿足需求。下面我們來實現(xiàn)一個帶圖標(biāo)和文字的Item。

二、代碼展示

1.定義包含ListView的布局文件activity_main.xmlActivityonCreate()時加載。

<?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對象時使用。

<?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)行效果如下圖:

自定義Item

到此這篇關(guān)于Android ListView 實例講解清晰易懂的文章就介紹到這了,更多相關(guān)Android ListView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android實現(xiàn)多線程斷點下載

    Android實現(xiàn)多線程斷點下載

    大家好,本篇文章主要講的是Android實現(xiàn)多線程斷點下載,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Android中Handler實現(xiàn)倒計時的兩種方式

    Android中Handler實現(xiàn)倒計時的兩種方式

    本篇文章主要介紹了Android中Handler實現(xiàn)倒計時的兩種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Android顏色處理SweepGradient掃描及梯度渲染示例

    Android顏色處理SweepGradient掃描及梯度渲染示例

    這篇文章主要為大家介紹了Android顏色處理SweepGradient掃描渲染及梯度渲染示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Android提高之藍(lán)牙傳感應(yīng)用實例

    Android提高之藍(lán)牙傳感應(yīng)用實例

    這篇文章主要介紹了Android的藍(lán)牙傳感應(yīng)用實例,對于Android項目開發(fā)來說非常實用,需要的朋友可以參考下
    2014-08-08
  • Android 日期和時間的使用實例詳解

    Android 日期和時間的使用實例詳解

    這篇文章主要介紹了Android 日期和時間的使用實例詳解的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • Android設(shè)置Activity背景為透明style的簡單方法(必看)

    Android設(shè)置Activity背景為透明style的簡單方法(必看)

    下面小編就為大家?guī)硪黄狝ndroid設(shè)置Activity背景為透明style的簡單方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • Kotlin中協(xié)程的創(chuàng)建過程詳析

    Kotlin中協(xié)程的創(chuàng)建過程詳析

    使用協(xié)程的專業(yè)開發(fā)者中有超過 50% 的人反映使用協(xié)程提高了工作效率,下面這篇文章主要給大家介紹了關(guān)于Kotlin中協(xié)程創(chuàng)建過程的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01
  • Android打造流暢九宮格抽獎活動效果

    Android打造流暢九宮格抽獎活動效果

    抽獎活動有很多種形式,轉(zhuǎn)盤抽獎,九宮格抽獎,刮刮卡抽獎,這篇文章主要為大家詳細(xì)介紹了如何打造流暢九宮格抽獎活動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android實現(xiàn)監(jiān)聽電話呼叫狀態(tài)的方法

    Android實現(xiàn)監(jiān)聽電話呼叫狀態(tài)的方法

    這篇文章主要介紹了Android實現(xiàn)監(jiān)聽電話呼叫狀態(tài)的方法,涉及Android權(quán)限控制及電話狀態(tài)監(jiān)聽的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Android Studio 引入 aidl 文件的方法匯總

    Android Studio 引入 aidl 文件的方法匯總

    本文給大家分享的是在Android Studio中引入AIDL文件常用的兩種方法,小伙伴們根據(jù)自己的情況自由選擇,希望對大家能夠有所幫助
    2017-11-11

最新評論