最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android滾動菜單ListView實(shí)例詳解

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

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

說明:滾動菜單ListView及點(diǎn)擊事件

代碼結(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)點(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)行展示:

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

相關(guān)文章

  • android實(shí)現(xiàn)背景平鋪的三種方法

    android實(shí)現(xiàn)背景平鋪的三種方法

    這篇文章主要介紹了Android的圖片平鋪效果的實(shí)現(xiàn)方法,主要有使用系統(tǒng)API、使用XML配置、自定義繪制三種方法,需要的朋友可以參考下
    2014-02-02
  • Android 再按一次返回鍵退出程序?qū)崿F(xiàn)思路

    Android 再按一次返回鍵退出程序?qū)崿F(xiàn)思路

    用戶退出應(yīng)用前給出一個提示是很有必要的,因?yàn)榭赡苁怯脩舨⒉徽娴南胪顺?,而只是一不小心按下了返回鍵,大部分應(yīng)用的做法是在應(yīng)用退出去前給出一個Dialog提示框;個人覺得再按一次返回鍵退出程序很有必要,接下來介紹一些簡單實(shí)現(xiàn)
    2013-01-01
  • Android?Studio實(shí)現(xiàn)音樂播放器2.0的全過程

    Android?Studio實(shí)現(xiàn)音樂播放器2.0的全過程

    音樂帶給人的聽覺享受是無可比擬的,動聽的音樂能夠愉悅?cè)说纳硇?讓人更加積極地去熱愛生活,下面這篇文章主要給大家介紹了關(guān)于Android?Studio實(shí)現(xiàn)音樂播放器2.0的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • Handler實(shí)現(xiàn)倒計時功能

    Handler實(shí)現(xiàn)倒計時功能

    這篇文章主要為大家詳細(xì)介紹了Handler實(shí)現(xiàn)倒計時功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Android繪制簡單條形圖

    Android繪制簡單條形圖

    這篇文章主要為大家詳細(xì)介紹了Android繪制簡單條形圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • android ocr——身份證識別的功能實(shí)現(xiàn)

    android ocr——身份證識別的功能實(shí)現(xiàn)

    本篇文章主要介紹了android ocr——身份證識別的功能實(shí)現(xiàn),具有一定的參考價值,有需要的可以了解一下。
    2016-11-11
  • Android自定義View實(shí)現(xiàn)風(fēng)車效果

    Android自定義View實(shí)現(xiàn)風(fēng)車效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)風(fēng)車效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • android studio編譯jar包或者aar包的方法教程詳解

    android studio編譯jar包或者aar包的方法教程詳解

    這篇文章主要介紹了android studio編譯jar包或者aar包的方法教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Android上使用grpc的方法教程

    Android上使用grpc的方法教程

    這篇文章主要給大家介紹了在Android上使用grpc的方法教程,文中通過示例代碼給大家詳細(xì)介紹了在android上使用grpc的方法以及可能遇到的種種問題的解決方法,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-07-07
  • Kotlin使用滾動控件RecyclerView實(shí)例教程

    Kotlin使用滾動控件RecyclerView實(shí)例教程

    RecyclerView是Android一個更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實(shí)現(xiàn)數(shù)據(jù)縱向滾動,也可以實(shí)現(xiàn)橫向滾動(ListView做不到橫向滾動)。接下來講解RecyclerView的用法
    2022-12-12

最新評論

桂阳县| 图木舒克市| 翼城县| 五原县| 海淀区| 荔浦县| 高阳县| 新安县| 紫云| 龙海市| 绍兴市| 炉霍县| 鲁甸县| 沙田区| 曲水县| 安顺市| 上犹县| 呼和浩特市| 沧源| 阿拉尔市| 临夏市| 南召县| 湖口县| 阜平县| 焦作市| 皮山县| 达州市| 油尖旺区| 荆门市| 时尚| 丹江口市| 鄂托克旗| 汤阴县| 恩施市| 西昌市| 陵川县| 河间市| 安新县| 西乌珠穆沁旗| 堆龙德庆县| 新绛县|