• Android实验一 ListView的简单应用


    1.需求:实现图书列表,点击一个图书,可查看详细图书信息

    2.编写listview_main页面(主页面)

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent">
     5     <ListView
     6         android:id="@+id/listview"
     7         android:layout_width="match_parent"
     8         android:layout_height="match_parent" />
     9 
    10 </LinearLayout

    3.编写listview_item列表页

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent">
     5     <ImageView
     6         android:id="@+id/bookImage"
     7         android:layout_width="40dp"
     8         android:layout_height="100dp"
     9         android:layout_gravity="center_vertical"
    10         android:layout_marginLeft="10dp"/>
    11     <TextView android:id="@+id/bookName"
    12         android:layout_width="wrap_content"
    13         android:layout_height="wrap_content"
    14         android:layout_gravity="center_vertical"
    15         android:layout_marginLeft="10dp"
    16         android:textSize="20sp"/>
    17 </LinearLayout>

    4.编写item_detail页面(item详情页)

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
        tools:context=".BookDetailActivity">
    
        <ImageView
            android:id="@+id/bookimage"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="44dp"
            android:layout_marginLeft="44dp"
            android:layout_marginTop="44dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/bookname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    
            android:layout_marginTop="80dp"
    
            android:textSize="50sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.272"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/bookimage" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

    5.建立Book.kt类

    class Book(val name:String,val imageId:Int)

    6.创建自定义图书适配器BookAdapter.kt

     1 class BookAdapter(activity:Activity, val resourceId:Int, data:List<Book>):
     2     ArrayAdapter<Book>(activity,resourceId,data) {
     3     override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
     4 //使用LayoutInflater加载子布局
     5         val view = LayoutInflater.from(context).inflate(resourceId,parent,false)
     6 //调用view的findByID()方法获取ImageView和TextView实例
     7         val bookImage:ImageView=view.findViewById(R.id.bookImage)
     8         val bookName:TextView=view.findViewById(R.id.bookName)
     9 //通过getItem()方法得到当前项的Book实例,并分别调用setImageResource()和setText()方法设置图片和文字
    10         val book=getItem(position)
    11         if(book!=null){
    12             bookImage.setImageResource(book.imageId)
    13             bookName.text=book.name
    14         }
    15         return view
    16     }
    17 
    18 }

    7.创建MainActivity

    class ListViewMainActivity: AppCompatActivity()  {
        private val fruList=ArrayList<Book>()
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.listview_main)
            this.title="listview"
            initBook()
    //建建适配器对象
            val adapter = BookAdapter(this,R.layout.listview_item,fruList)
            listview.adapter=adapter
    //为适配器设置点击事件
            listview.setOnItemClickListener { _, _, position, _ ->
                val book=fruList[position]
    //使用intent传递数据
                val intent = Intent(this,BookDetailActivity::class.java)
                intent.putExtra("book",book.name)
                intent.putExtra("image",book.imageId)
                Log.d("imageId","img数据时$book.imageId")
                startActivity(intent)
            }
    
    
        }
    //初始化数据
        private fun initBook(){
            repeat(1){
                fruList.add(Book("Java编程思想",R.drawable.book1))
                fruList.add(Book("程序员修炼之道",R.drawable.book2))
                fruList.add(Book("零基础入门学习Python",R.drawable.book3))
                fruList.add(Book("Java从入门到精通",R.drawable.book4))
                fruList.add(Book("深入理解Java虚拟机",R.drawable.book5))
                fruList.add(Book("算法导论",R.drawable.book6))
                fruList.add(Book("Android开发艺术探索",R.drawable.book7))
                fruList.add(Book("Android进阶之光",R.drawable.book8))
                fruList.add(Book("疯狂Android",R.drawable.book9))
                fruList.add(Book("Android开发从入门到精通",R.drawable.book10))
            }
        }
    
    }

    6.创建BookDetailActivity.kt

    class BookDetailActivity: AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.item_detail)
    //接受数据并显示数据
            val bookdetail = intent.getStringExtra("book")
            val img = intent.getIntExtra("image",0)
            val imgview:ImageView=findViewById(R.id.bookimage)
            val bootv:TextView=findViewById(R.id.bookname)
            bootv.text=bookdetail
            Log.d("img","img数据时$img")
            Log.d("book","img数据时$bookdetail")
            imgview.setImageResource(img.toInt())
    
        }
    }

    7.配置AndroidManifest.xml

     1 <activity android:name=".ListViewMainActivity">
     2             <intent-filter>
     3                 <action android:name="android.intent.action.MAIN" />
     4                 <category android:name="android.intent.category.LAUNCHER" />
     5             </intent-filter>
     6         </activity>
     7         <activity android:name=".BookDetailActivity">
     8             <intent-filter>
     9                 <action android:name="bookDetail"/>
    10                 <category android:name="android.intent.category.DEFAULT"/>
    11             </intent-filter>

    8.效果图

  • 相关阅读:
    常用jquery
    常用记录
    mysql proxy 读写分离
    Linux 学习笔记
    php 1116
    php 1115
    php 1110
    php 1109
    php 1108
    php 1105
  • 原文地址:https://www.cnblogs.com/Rong-Xiu/p/13919834.html
Copyright © 2020-2023  润新知