最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android ListView 實(shí)例講解清晰易懂

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

一、前言

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

二、代碼展示

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

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

相關(guān)文章

  • Android中html.fromhtml的使用方法

    Android中html.fromhtml的使用方法

    這篇文章主要介紹了Android中html.fromhtml的使用方法的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 解析android中ProgressBar的用法

    解析android中ProgressBar的用法

    本篇文章是對android中ProgressBar的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Android studio 禁用AndroidX方式

    Android studio 禁用AndroidX方式

    這篇文章主要介紹了Android studio 禁用AndroidX方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Android?APP瘦身shrinkResources使用問題詳解

    Android?APP瘦身shrinkResources使用問題詳解

    這篇文章主要為大家介紹了Android?APP瘦身shrinkResources使用問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android實(shí)現(xiàn)使用微信登錄第三方APP的方法

    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í)現(xiàn)修改標(biāo)題欄位置使其居中的方法,涉及Android布局設(shè)置的簡單實(shí)現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Android仿微信滑動退出Activity

    Android仿微信滑動退出Activity

    這篇文章主要介紹了Android仿微信滑動退出Activity的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2016-12-12
  • Android如何使用圓形揭露動畫巧妙地隱藏或顯示View詳解

    Android如何使用圓形揭露動畫巧妙地隱藏或顯示View詳解

    Android開發(fā)中會遇到不少顯示和隱藏的問題,下面這篇文章主要給大家介紹了關(guān)于Android如何使用圓形揭露動畫巧妙地隱藏或顯示View的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • Android Fragment的使用方法(翻譯)

    Android Fragment的使用方法(翻譯)

    這篇文章主要介紹了Android Fragment的使用方法,官方文檔的翻譯,需要的朋友可以參考下
    2015-03-03
  • 實(shí)例詳解Android文件存儲數(shù)據(jù)方式

    實(shí)例詳解Android文件存儲數(shù)據(jù)方式

    總體的來講,數(shù)據(jù)存儲方式有三種:一個是文件,一個是數(shù)據(jù)庫,另一個則是網(wǎng)絡(luò)。下面通過本文給大家介紹Android文件存儲數(shù)據(jù)方式,對android文件存儲數(shù)據(jù)相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01

最新評論

景泰县| 海门市| 剑阁县| 年辖:市辖区| 镇平县| 方山县| 望城县| 雷波县| 鄂州市| 塘沽区| 奉节县| 朔州市| 天镇县| 霍邱县| 桂东县| 建水县| 河津市| 咸阳市| 庄河市| 丹凤县| 巴里| 进贤县| 桂平市| 专栏| 淮阳县| 广水市| 拉孜县| 凤翔县| 进贤县| 吴忠市| 周至县| 怀宁县| 讷河市| 托克托县| 酒泉市| 山阳县| 新宾| 三河市| 佛学| 南丰县| 七台河市|