SoFunction
Updated on 2025-03-11

ListView - Add an event listening instance of item

1. Click event (OnItemClickListener)

onItemClick(AdapterView<?> parent, View view, int position, long id)

parent:The official explanation is: The AdapterView where the click happened, that is, the AdapterView clicked by the user, this parameter is generally not used.

view:The layout View object corresponding to the currently clicked list item can be obtained through this parameter and then operated on it. For example, suppose there is a ListView that contains 4 list items and you click the second one, then through the view you can operate the components of TextView, ImageView, etc. in the second list item (assuming that there exists).

position:The position of the currently clicked list item starts from 0, that is, click on the nth one, and position is n-1.

id:The serial number of the currently clicked list item also starts from 0, so most of the time the position and id are the same. As for the differences between these two parameters, if you are interested, you can take a closer look.

public class MainActivity extends Activity implements {

  private ListView myListView;
  private SimpleAdapter simpleAdapter;
  private List&lt;Map&lt;String, Object&gt;&gt; data;

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

    data = new ArrayList&lt;Map&lt;String, Object&gt;&gt;();
    simpleAdapter = new SimpleAdapter(this, getData(), , new String[]{"img", "text"}, new int[]{, });
    myListView = (ListView) findViewById();
    //Set the listener    (simpleAdapter);
    (this);
  }

  private List&lt;Map&lt;String, Object&gt;&gt; getData() {
    for (int i = 0; i &lt; 20; i++) {
      Map&lt;String, Object&gt;map = new HashMap&lt;String, Object&gt;();
      ("img", .ic_launcher);
      ("text", "Initial simpleAdapter"+(i+1));
      (map);
    }

    return data;
  }

  @Override
  public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
    //Get internal components through view and then perform operations    String text = (String) ((TextView)()).getText();
    //In most cases, position and id are the same and both start from 0    String showText = "Click the line" + position + "Item, the text content is:" + text + ", ID is:" + id;
    (this, showText, Toast.LENGTH_LONG).show();
  }
}

2. Scroll event (OnScrollListener)

void onScrollStateChanged(AbsListView view, int scrollState)

Listen to changes in scrolling state.

view:The view being scrolled, that is, the current ListView.

scrollState:There are several scrolling states:

SCROLL_STATE_TOUCH_SCROLL:In sliding state, the fingers slide in the view, and the fingers stay on the screen without leaving.

SCROLL_STATE_FLING:In throwing state, before leaving the view, fingers slash hard (imagine the feeling of throwing the view out), and the view will slide according to inertia to stop.

SCROLL_STATE_IDLE:Idle state, nothing is done. When the throwing state or the touch scroll state ends, the idle state enters.

void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)

Listen to non-scrolling states, as long as ListView does not scroll, it will be called continuously.

view:The view being scrolled, that is, the current ListView.

firstVisibleItem:The index value of the first list item that has been loaded.

visibleItemCount:The total number of list items loaded.

totalItemCount:The total number of list items corresponding to the data source in the adapter.

public class MainActivity extends Activity implements {

  private ListView myListView;
  private SimpleAdapter simpleAdapter;
  private List&lt;Map&lt;String, Object&gt;&gt; data;

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

    data = new ArrayList&lt;Map&lt;String, Object&gt;&gt;();
    simpleAdapter = new SimpleAdapter(this, getData(), , new String[]{"img", "text"}, new int[]{, });
    myListView = (ListView) findViewById();
    (simpleAdapter);

    //Set the listener    (this);
  }

  private List&lt;Map&lt;String, Object&gt;&gt; getData() {
    for (int i = 0; i &lt; 20; i++) {
      Map&lt;String, Object&gt;map = new HashMap&lt;String, Object&gt;();
      ("img", .ic_launcher);
      ("text", "Initial simpleAdapter"+(i+1));
      (map);
    }

    return data;
  }

  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {
    switch (scrollState) {
      // Throwing status      case SCROLL_STATE_FLING:
        (this, "SCROLL_STATE_FLING", Toast.LENGTH_SHORT).show();
        break;
      //Idle status      case SCROLL_STATE_IDLE:
        (this, "SCROLL_STATE_IDLE", Toast.LENGTH_SHORT).show();
        break;
      //Sliding status      case SCROLL_STATE_TOUCH_SCROLL:
        (this, "SCROLL_STATE_TOUCH_SCROLL", Toast.LENGTH_SHORT).show();
        break;
    }
  }

  @Override
  public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// (this, "Scrolling has stopped, the index of the first item that has been loaded is: " + firstVisibleItem +// ", the total number of loaded list items is: " + visibleItemCount + ", the total number of existing list items is: " +//            totalItemCount, Toast.LENGTH_LONG).show();
  }
}

The above ListView-added item event monitoring example is all the content I share with you. I hope you can give you a reference and I hope you can support me more.