[Android development] Detailed explanation of the actual use of ListView, have you got it?

table of Contents

1. What is ListView?

Second, the simple usage of ListView

Three, ListView click response event


Hello, I’m the little gray ape! A super programmer who can write BUG!

Today, I will record a tutorial on the use of Listview in Android development, and it is also a consolidation of the knowledge learned!

1. What is ListView?

ListView is a list box in Android development. It can display the information you want to display in the form of a horizontal list to see an effect:

Second, the simple usage of ListView

In xml, Listview has the following basic attributes

android:divider="#f00"                The color of the dividing line

android:dividerHeight="2dp"        the height of the dividing bar

android:cacheColorHint="#0fff"   When the background image is used in the list, there will be a problem with the drop-down or pull-up: the background image is missing

android:scrollingCache="false"     When the list uses the background image, there will be a problem with the drop-down or pull-up: black blocks appear

android:fadingEdge="none"         When found to pull up and down, there will be shadows on the top and bottom

android:listSelector="#0000"        remove the background when clicking ltem

So how should it be used in actual development?

1) First create a new project and add the listview control to the activity_main.xml file, as shown below:

Set the width and height of the control to match_parent to occupy the entire space

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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=".moveice_view">     <ListView        android:id="@+id/moveiceList"        android:layout_width="match_parent"        android:layout_height="match_parent"        tools:ignore="MissingConstraints">     </ListView></androidx.constraintlayout.widget.ConstraintLayout>

2) Configure MainActivity code

The use of ListView is to create a character array, which stores the content of each line to be displayed, but the parameters set in the listview are of the Adapter type. Obviously, the string array cannot be directly passed in as a parameter, so we need to use an ArrayAdapter class As a bridge, to convert the string array,

Maybe the logic is a bit strong and not easy to understand. Just look at the following code:

package com.example.summarizepj; 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; public class moveice_view extends AppCompatActivity {     ListView moveiceList;    String [] moveice_arr = {"肖申克的救赎","阿甘正传","明天会更好","速度与激情","建军大业","你好李焕英"};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_moveice_view);         moveiceList = findViewById(R.id.moveiceList);        ArrayAdapter<String> adapter = new ArrayAdapter<String>(moveice_view.this,android.R.layout.simple_list_item_1,moveice_arr);        moveiceList.setAdapter(adapter);     }}

Now to explain the ArrayAdapter,

Here I used a TextView layout file included in the system : android.R.layout.simple_expandable_list_item_1, it is more convenient to call this, it has the following five parameters

  • android.R.layout.simple_list_item_1   一行text
  • android.R.layout.simple_list_item_2    one line of title , one line of text
  • android.R.layout.simple_list_item_single_choice    radio button
  • android.R.layout.simple_list_item_multiple_choice    multiple choice button
  • android.R.layout.simple_list_item_checked    checkbox

ArrayAdapter<String> adapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, data); means: the code to create an array adapter, there are three parameters, the first parameter is the context, It is the current Activity. The second parameter is a built-in layout in the android sdk. There is only one TextView in it. This parameter indicates that the layout of each data in our array is this view, which means that each data is displayed in this Above view; the third parameter is the data we want to display. ListView will traverse each piece of data in data according to these three parameters, read one out, and display it in the layout corresponding to the second parameter, thus forming the listView we see.

After running the above code, you get this effect chart:

Three, ListView click response event

But the above code is unresponsive when the corresponding button is clicked. As we usually see on the app, events are triggered after clicking, so let's add event response to the listview control.

It should be noted here that the event listener added to the listview control is different from the usual method used to add listeners to the button control. The setOnItemClickListener method is used here . The specific use is as follows:

package com.example.summarizepj; 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; public class moveice_view extends AppCompatActivity {     ListView moveiceList;    String [] moveice_arr = {"肖申克的救赎","阿甘正传","明天会更好","速度与激情","建军大业","你好李焕英"};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_moveice_view);         moveiceList = findViewById(R.id.moveiceList);        ArrayAdapter<String> adapter = new ArrayAdapter<String>(moveice_view.this,android.R.layout.simple_list_item_1,moveice_arr);        moveiceList.setAdapter(adapter);         moveiceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                String result = parent.getItemAtPosition(position).toString();                switch (result){                    case "肖申克的救赎":{                        Toast.makeText(moveice_view.this, "您点击了" + result, Toast.LENGTH_SHORT).show();                    }                    break;                    case "阿甘正传":{                        Toast.makeText(moveice_view.this,"您点击了" + result,Toast.LENGTH_SHORT).show();                    }                    break;                    case "明天会更好":{                        Toast.makeText(moveice_view.this,"您点击了" + result,Toast.LENGTH_SHORT).show();                    }                    break;                }            }        });    }}

When the corresponding list control is clicked, the displayed effect is as follows:

At this point, the use of listview and click response is complete,

If you have questions, remember to leave a message in the comment area!

Feel good, remember to like and follow!