• 144990

    文章

  • 856

    评论

  • 13

    友链

  • 最近新加了换肤功能,大家多来逛逛吧~~~~
  • 喜欢这个网站的朋友可以加一下QQ群,我们一起交流技术。

安卓实现下拉框--简单版(需要改进的地方很 记录一下


需要改进的地方 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
    )

代码架构

执行结果

 


695856371Web网页设计师②群 | 喜欢本站的朋友可以收藏本站,或者加入我们大家一起来交流技术!

0条评论

Loading...


发表评论

电子邮件地址不会被公开。 必填项已用*标注

自定义皮肤 主体内容背景
打开支付宝扫码付款购买视频教程
遇到问题联系客服QQ:419400980
注册梁钟霖个人博客