baseAdapter
作者:互联网
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/myList" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>View Code
利用代码添加baseAdapter
package com.example.adapter import android.graphics.Color import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.view.View import android.view.ViewGroup import android.widget.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val myList = findViewById<ListView>(R.id.myList) val adapter = object : BaseAdapter() { override fun getCount(): Int { // 指定一共包含40个选项 return 40 } override fun getItem(position: Int): Any? { return null } // 重写该方法,该方法的返回值将作为列表项的ID override fun getItemId(position: Int): Long { return position.toLong() } // 重写该方法,该方法返回的View将作为列表框 override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { // 创建一个LinearLayout,并向其中添加两个组件 val linearLinearLayout = LinearLayout(this@MainActivity) linearLinearLayout.orientation = LinearLayout.HORIZONTAL val image = ImageView(this@MainActivity) image.setImageResource(R.mipmap.ic_launcher) val text = TextView(this@MainActivity) text.text = "第" + (position + 1) + "个列表项" text.textSize = 20f text.setTextColor(Color.RED) linearLinearLayout.addView(image) linearLinearLayout.addView(text) // 返回LinearLayout实例 return linearLinearLayout } } myList.adapter = adapter } }View Code
标签:linearLinearLayout,baseAdapter,text,override,import,android,View 来源: https://www.cnblogs.com/superxuezhazha/p/11481662.html