Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Android scrolling menu ListView

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to achieve Android scrolling menu ListView". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The details are as follows

Description: scroll menu ListView and click event

Code structure:

1. Create a list display model

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

2. Quote the main interface.

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

3. Create a fruit class

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. Create an adapter

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 ()) .propagate (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. Main content

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 (); / / initialize fruit data FruitAdapter adapter = new FruitAdapter (MainActivity.this,R.layout.fruit_item,fruitList); ListView listView = (ListView) findViewById (R.id.list_view); listView.setAdapter (adapter) / / respond to the click event 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 iTuno

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report