[Android studio] Section 16 ListView Control

Table of contents

1. What is ListView?

2. Usage steps

1. What is ListView?

ListView is a common control on the Android platform, used to display scrollable lists on the screen.

The following commonly used methods are provided:

  1. setAdapter(Adapter): Sets the adapter to bind the data source to the ListView and display it in the list.

  2. getAdapter(): Get the currently set adapter object.

  3. setOnItemClickListener(AdapterView.OnItemClickListener): Set the click event listener for the list item.

  4. setOnItemLongClickListener(AdapterView.OnItemLongClickListener): Set the long press event listener for the list item.

  5. setEmptyView(View): Sets the empty view displayed when the data is empty.

  6. smoothScrollToPosition(int position): Smoothly scrolls the list item to the specified position.

  7. setSelection(int position): Set the list item at the specified position to the selected state.

  8. getFirstVisiblePosition(): Gets the position of the first visible list item in the current visible area.

  9. getLastVisiblePosition(): Gets the position of the last visible list item in the current visible area.

  10. getCount(): Gets the number of items in the data source in the adapter.

  11. getItemAtPosition(int position): Get the data object corresponding to the list item at the specified position.

These methods can be used to set up the adapter, listen to list item events, control scroll position, etc., and get information about the list state and data. By using these methods, you can operate and manage ListView.

2. Usage steps

Here is a simple example that demonstrates how to use a ListView in an XML layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

In code, you need to provide data to the ListView through an adapter and define the layout of each list item. The following is a simple example showing how to use an ArrayAdapter in an Activity to provide data to a ListView:

import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private ArrayAdapter<String> adapter;
    private String[] data = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.listView);

        // 创建适配器并设置给 ListView
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data);
        listView.setAdapter(adapter);
    }
}

In this example, we pass an array of strings (data) as the data source to the ListView through the ArrayAdapter. Use android.R.layout.simple_list_item_1as layout style for each list item. We then setAdapter()set the adapter to the ListView using the method.

By using adapters, you can customize more complex list item layouts, as well as handle click events and other interactions in the ListView. ListView also supports functions such as scrolling, paging loading, and data updating. You can choose appropriate methods and components according to your needs to achieve the desired list display effect.

The adapter is explained in detail in the next chapter.

Guess you like

Origin blog.csdn.net/AA2534193348/article/details/131483211