simpleAdapter


 

 

這個adapter用起來稍微復雜一點。

首先我們看main_activity的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!-- 定義一個ListView -->
    <ListView android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

在界面上只有一個listView

然后,我們在主函數里面利用simpleAdapter向這個listView填充內容

package com.example.adapter

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.SimpleAdapter

class MainActivity : AppCompatActivity() {

    private val names = arrayOf("虎頭", "弄玉", "李清照", "李白")
    private val descs = arrayOf("可愛的小孩", "一個擅長音樂的女孩", "一個擅長文學的女性", "浪漫主義詩人")
    private val imageIds = intArrayOf(R.drawable.tiger,
        R.drawable.nongyu, R.drawable.qingzhao, R.drawable.libai)
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // 創建一個List集合,List集合的元素是Map
        val listItems = ArrayList<Map<String, Any>>()
        for (i in names.indices)
        {
            val listItem = HashMap<String, Any>()
            listItem["header"] = imageIds[i]
            listItem["personName"] = names[i]
            listItem["desc"] = descs[i]
            listItems.add(listItem)
        }
        // 創建一個SimpleAdapter
        val simpleAdapter = SimpleAdapter(this, listItems, R.layout.simple_item, arrayOf("personName", "header", "desc"), intArrayOf(R.id.name, R.id.header, R.id.desc))
        val list = findViewById<ListView>(R.id.mylist)
        // 為ListView設置Adapter
        list.adapter = simpleAdapter
        // 為ListView的列表項的單擊事件綁定事件監聽器
        list.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            Log.i("-CRAZYIT-", names[position] + "被單擊了")
        }
        // 為ListView的列表項的選中事件綁定事件監聽器
        list.onItemSelectedListener = object : AdapterView.OnItemSelectedListener
        {
            // 第position項被選中時激發該方法
            override fun onItemSelected(parent: AdapterView<*>, view: View,
                                        position: Int, id: Long)
            {
                Log.i("-CRAZYIT-", names[position] + "被選中了")
            }

            override fun onNothingSelected(parent: AdapterView<*>)
            {
            }
        }
    }
}
View Code

主要看simpleAdapter的使用

 val simpleAdapter = SimpleAdapter(this, listItems, R.layout.simple_item, arrayOf("personName", "header", "desc"), intArrayOf(R.id.name, R.id.header, R.id.desc))

這里傳入5個參數:

  • context
  • List<?extends Map<String,?>>
  • 指定界面布局的ID
  • string[] 指定從第二個參數中提取哪些值,填充到布局界面中
  • 指定填充的對應關系

 最后我們看一下指定的布局文件 simple_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!-- 定義一個ImageView,用於作為列表項的一部分。 -->
    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <!-- 定義一個TextView,用於作為列表項的一部分。 -->
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:textColor="#f0f"
            android:textSize="20dp" />
        <!-- 定義一個TextView,用於作為列表項的一部分。 -->
        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:textSize="14dp" />
    </LinearLayout>
</LinearLayout>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM