亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android滾動菜單ListView實例詳解

 更新時間:2021年10月03日 17:01:37   作者:蘭葉書  
這篇文章主要為大家詳細介紹了Android滾動菜單ListView實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android使用ListView實現(xiàn)滾動菜單的具體代碼,供大家參考,具體內(nèi)容如下

說明:滾動菜單ListView及點擊事件

代碼結(jié)構(gòu):

1、創(chuàng)建一個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)點擊事件
        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);
        }
 
    }
}

運行展示:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Room的使用詳解

    Android Room的使用詳解

    這篇文章主要介紹了Android Room的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-04-04
  • Android實現(xiàn)圖片左右滑動效果

    Android實現(xiàn)圖片左右滑動效果

    現(xiàn)在滑動效果用的比較多,尤其是在手機端上面,本文介紹了Android實現(xiàn)圖片左右滑動效果,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧。
    2016-10-10
  • Android剪貼板用法詳解

    Android剪貼板用法詳解

    這篇文章主要介紹了Android剪貼板用法詳解,以實例的形式對Android中剪貼板的各類傳值方法做了較為詳細的講述,需要的朋友可以參考下
    2014-10-10
  • Android Activity與Fragment實現(xiàn)底部導(dǎo)航器

    Android Activity與Fragment實現(xiàn)底部導(dǎo)航器

    這篇文章主要介紹了Android Activity與Fragment實現(xiàn)底部導(dǎo)航器的相關(guān)資料,并附實例代碼,需要的朋友可以參考下
    2016-11-11
  • Android?線程死鎖場景與優(yōu)化解決

    Android?線程死鎖場景與優(yōu)化解決

    線程死鎖是老生常談的問題,線程池死鎖本質(zhì)上屬于線程死鎖的一部分,線程池造成的死鎖問題往往和業(yè)務(wù)場景相關(guān),本文主要介紹了Android?線程死鎖場景與優(yōu)化,感興趣的可以了解一下
    2023-12-12
  • Android Adapter的幾個常用方法

    Android Adapter的幾個常用方法

    這篇文章主要為大家詳細介紹了Android Adapter的幾個常用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android使用Matrix旋轉(zhuǎn)圖片模擬碟片加載過程

    Android使用Matrix旋轉(zhuǎn)圖片模擬碟片加載過程

    這篇文章主要為大家詳細介紹了Android使用Matrix旋轉(zhuǎn)圖片模擬碟片加載過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 基于GridView和ActivityGroup實現(xiàn)的TAB分頁(附源碼)

    基于GridView和ActivityGroup實現(xiàn)的TAB分頁(附源碼)

    今天為大家介紹下使用GridView和ActivityGroup實現(xiàn)的分頁,這里需要將Activity轉(zhuǎn)換成Window,然后再換成成View添加到容器中,具體實現(xiàn)代碼如下,感興趣的朋友可以參考下哈
    2013-06-06
  • Android編程實現(xiàn)應(yīng)用自動更新、下載、安裝的方法

    Android編程實現(xiàn)應(yīng)用自動更新、下載、安裝的方法

    這篇文章主要介紹了Android編程實現(xiàn)應(yīng)用自動更新、下載、安裝的方法,涉及Android針對應(yīng)用程序包的讀取,屬性判斷與更新操作的相關(guān)技巧,需要的朋友可以參考下
    2016-02-02
  • Android 手勢 正則匹配圖片實例代碼

    Android 手勢 正則匹配圖片實例代碼

    這篇文章主要介紹了Android 手勢 正則匹配圖片實例代碼,需要的朋友可以參考下
    2017-07-07

最新評論