Android滚动菜单ListView实例详解
程序员文章站
2022-06-24 19:16:22
本文实例为大家分享了android使用listview实现滚动菜单的具体代码,供大家参考,具体内容如下说明:滚动菜单listview及点击事件代码结构:1、创建一个list展示模型app\src\ma...
本文实例为大家分享了android使用listview实现滚动菜单的具体代码,供大家参考,具体内容如下
说明:滚动菜单listview及点击事件
代码结构:
1、创建一个list展示模型
app\src\main\res\layout\fruit_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="match_parent"> <imageview android:id="@+id/fruit_image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:id="@+id/fruit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_marginleft="10dp"/> </linearlayout>
2、主界面引用一下
app\src\main\res\layout\activity_main.xml
<?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="match_parent" tools:context=".mainactivity"> <listview android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout>
3、创建fruit类
app\src\main\java\com\example\listviewtest\fruit.java
package com.example.listviewtest; public class fruit { private string name; private int imageid; public fruit(string name,int imageid){ this.name = name; this.imageid = imageid; } public string getname(){ return name; } public int getimageid(){ return imageid; } }
4、创建适配器
app\src\main\java\com\example\listviewtest\fruitadapter.java
package com.example.listviewtest; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.textview; import java.util.list; public class fruitadapter extends arrayadapter<fruit> { private int resourceid; public fruitadapter(context context, int textviewresourceid, list<fruit> object){ super(context,textviewresourceid,object); resourceid = textviewresourceid; } public view getview(int position, view converview, viewgroup parent){ fruit fruit = getitem(position); view view = layoutinflater.from(getcontext()).inflate(resourceid,parent,false); imageview fruitimage = (imageview) view.findviewbyid(r.id.fruit_image); textview fruitname = (textview) view.findviewbyid(r.id.fruit_name); fruitimage.setimageresource(fruit.getimageid()); fruitname.settext(fruit.getname()); return view; } }
5、主内容
app\src\main\java\com\example\listviewtest\mainactivity.java
package com.example.listviewtest; import androidx.appcompat.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listview; import android.widget.toast; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity { private list<fruit> fruitlist = new arraylist<>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initfruits();//初始化水果数据 fruitadapter adapter = new fruitadapter(mainactivity.this,r.layout.fruit_item,fruitlist); listview listview = (listview) findviewbyid(r.id.list_view); listview.setadapter(adapter); //响应点击事件 listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { fruit fruit = fruitlist.get(i); toast.maketext(mainactivity.this,fruit.getname(),toast.length_short).show(); } }); } private void initfruits(){ for (int i=0;i<2;i++){ fruit apple = new fruit("apple",r.drawable.apple_pic); fruitlist.add(apple); fruit banana = new fruit("banana",r.drawable.banana_pic); fruitlist.add(banana); fruit cherry = new fruit("cherry",r.drawable.cherry_pic); fruitlist.add(cherry); fruit grape = new fruit("grape",r.drawable.grape_pic); fruitlist.add(grape); fruit mango = new fruit("mango",r.drawable.mango_pic); fruitlist.add(mango); fruit orange = new fruit("orange",r.drawable.orange_pic); fruitlist.add(orange); fruit pear = new fruit("pear",r.drawable.pear_pic); fruitlist.add(pear); fruit pineapple = new fruit("pineapple",r.drawable.pineapple_pic); fruitlist.add(pineapple); fruit strawberry = new fruit("strawberry",r.drawable.strawberry_pic); fruitlist.add(strawberry); fruit watermeion = new fruit("watermeion",r.drawable.watermeion_pic); fruitlist.add(watermeion); } } }
运行展示:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Go语言中io包核心接口示例详解