Android滾動(dòng)菜單ListView實(shí)例詳解
本文實(shí)例為大家分享了Android使用ListView實(shí)現(xiàn)滾動(dòng)菜單的具體代碼,供大家參考,具體內(nèi)容如下
說(shuō)明:滾動(dòng)菜單ListView及點(diǎn)擊事件
代碼結(jié)構(gòu):
1、創(chuàng)建一個(gè)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、創(chuàng)建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、創(chuàng)建適配器
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、主內(nèi)容
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();//初始化水果數(shù)據(jù) FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); //響應(yīng)點(diǎn)擊事件 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); } } }
運(yùn)行展示:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)圖片左右滑動(dòng)效果
現(xiàn)在滑動(dòng)效果用的比較多,尤其是在手機(jī)端上面,本文介紹了Android實(shí)現(xiàn)圖片左右滑動(dòng)效果,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。2016-10-10Android Activity與Fragment實(shí)現(xiàn)底部導(dǎo)航器
這篇文章主要介紹了Android Activity與Fragment實(shí)現(xiàn)底部導(dǎo)航器的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下2016-11-11Android?線程死鎖場(chǎng)景與優(yōu)化解決
線程死鎖是老生常談的問(wèn)題,線程池死鎖本質(zhì)上屬于線程死鎖的一部分,線程池造成的死鎖問(wèn)題往往和業(yè)務(wù)場(chǎng)景相關(guān),本文主要介紹了Android?線程死鎖場(chǎng)景與優(yōu)化,感興趣的可以了解一下2023-12-12Android使用Matrix旋轉(zhuǎn)圖片模擬碟片加載過(guò)程
這篇文章主要為大家詳細(xì)介紹了Android使用Matrix旋轉(zhuǎn)圖片模擬碟片加載過(guò)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03基于GridView和ActivityGroup實(shí)現(xiàn)的TAB分頁(yè)(附源碼)
今天為大家介紹下使用GridView和ActivityGroup實(shí)現(xiàn)的分頁(yè),這里需要將Activity轉(zhuǎn)換成Window,然后再換成成View添加到容器中,具體實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈2013-06-06Android編程實(shí)現(xiàn)應(yīng)用自動(dòng)更新、下載、安裝的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)應(yīng)用自動(dòng)更新、下載、安裝的方法,涉及Android針對(duì)應(yīng)用程序包的讀取,屬性判斷與更新操作的相關(guān)技巧,需要的朋友可以參考下2016-02-02Android 手勢(shì) 正則匹配圖片實(shí)例代碼
這篇文章主要介紹了Android 手勢(shì) 正則匹配圖片實(shí)例代碼,需要的朋友可以參考下2017-07-07