千家信息网

如何实现Android滚动菜单ListView

发表于:2025-01-22 作者:千家信息网编辑
千家信息网最后更新 2025年01月22日,本篇内容介绍了"如何实现Android滚动菜单ListView"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学
千家信息网最后更新 2025年01月22日如何实现Android滚动菜单ListView

本篇内容介绍了"如何实现Android滚动菜单ListView"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

具体内容如下

说明:滚动菜单ListView及点击事件

代码结构:

1、创建一个list展示模型

app\src\main\res\layout\fruit_item.xml

         

2、主界面引用一下

app\src\main\res\layout\activity_main.xml

      

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 {    private int resourceId;    public FruitAdapter(Context context,                        int textViewResourceId,                        List 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 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);        }     }}

运行展示:

"如何实现Android滚动菜单ListView"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0