安卓实现下拉框--简单版(需要改进的地方很 记录一下
程序员文章站
2022-06-27 19:50:44
需要改进的地方 1.点击不同 实现对不同页面的跳转 这个简单 判断一下就行了2.搜索框 自动补全 这个难 首先搜索框 然后自动补全 这个地方完善了就好了 做的跟Taobao一样 (goal)基础版MainActivitypackage com.example.listviewtestimport android.content.Intentimport android.os.Bundleimport androidx.appcompat.app.AppCompatActivity....
需要改进的地方 1.点击不同 实现对不同页面的跳转 这个简单 判断一下就行了
2.搜索框 自动补全 这个难 首先搜索框 然后自动补全 这个地方完善了就好了 做的跟Taobao一样 (goal)
基础版
MainActivity
package com.example.listviewtest
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
//val dataImage: ImageView =findViewById(R.id.dataImage)
private val dataList=ArrayList<Rabbit>()
/*private val data=listOf("Rabbit1","Rabbit2","Rabbit3","Rabbit4","Rabbit5","Rabbit6","Rabbit7","Rabbit8",
"Rabbit9","Rabbit10","Rabbit11","Rabbit12","Rabbit1","Rabbit2","Rabbit3","Rabbit4","Rabbit5","Rabbit6","Rabbit7","Rabbit8",
"Rabbit9","Rabbit10","Rabbit11","Rabbit12")*/
override fun onCreate(savedInstanceState: Bundle?) {
//initRabbits()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initRabbits()
val adapter=RabbitAdapter(this,R.layout.rabbit_item,dataList)
listView.adapter=adapter
listView.setOnItemClickListener{ parent,view,position,id->
val rabbit=dataList[position]
var intent= Intent(this,CatInfo::class.java)
Toast.makeText(this,rabbit.name,Toast.LENGTH_SHORT).show()
startActivity(intent)
}
}
private fun initRabbits(){
repeat(10){
dataList.add(Rabbit("小猫咪",R.drawable.pic1))
dataList.add(Rabbit("眼巴巴",R.drawable.pic3))
}
}
}
Adapater
package com.example.listviewtest
import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
class RabbitAdapter(activity:Activity,val resourceId:Int,data:List<Rabbit>)
:ArrayAdapter<Rabbit>(activity, resourceId,data) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view=LayoutInflater.from(context).inflate(resourceId,parent,false)
//view这个出问题 没有映射到我想要的那一个xml
//val dataView:ListView=view.findViewById(R.id.listView)
val dataImage: ImageView =view.findViewById(R.id.dataImage)
val dataName: TextView =view.findViewById(R.id.dataName)
val data=getItem(position)
if(data!=null){
dataImage.setImageResource(data.imageId)
dataName.text=data.name
}
return view
}
}
rabbit_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dp"
tools:context="RabbitAdapter">
<ImageView
android:id="@+id/dataImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="@+id/dataName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
activity_main.xml
<?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">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"/>
</LinearLayout>
Rabbit.xml
package com.example.listviewtest
class Rabbit (
val name:String,
val imageId:Int
)
代码架构
执行结果
本文地址:https://blog.csdn.net/Dnisdream/article/details/110494206
上一篇: AndroidStudio中导入module(简单版)
下一篇: 经典的java EE分层架构