ListView - Adding item event listening

Keywords: Android

1. Click Event (OnItemClickListener)

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

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

View: The layout View object corresponding to the list item currently clicked can get the components inside the list item through this parameter, and then operate on them. For example, if you have a ListView with four list items and you click on the second, you can manipulate the TextView, ImageView, etc. components in the second list item (assuming they exist).

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

_id: The serial number of the list item clicked on at present also starts from 0, so most of the time the position and ID are the same. As for the difference between the two parameters, the interested children's shoes can be further studied.

public class MainActivity extends Activity implements AdapterView.OnItemClickListener{

    private ListView myListView;
    private SimpleAdapter simpleAdapter;
    private List<Map<String, Object>> data;

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

        data = new ArrayList<Map<String, Object>>();
        simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"img", "text"}, new int[]{R.id.img, R.id.text});
        myListView = (ListView) findViewById(R.id.myListView);
        myListView.setAdapter(simpleAdapter);
        //Setting up listeners
        myListView.setOnItemClickListener(this);
    }

    private List<Map<String, Object>> getData() {
        for (int i = 0; i < 20; i++) {
            Map<String, Object>map = new HashMap<String, Object>();
            map.put("img", R.mipmap.ic_launcher);
            map.put("text", "initial simpleAdapter"+(i+1));
            data.add(map);
        }

        return data;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Get its internal components through view, and then operate on them
        String text = (String) ((TextView)view.findViewById(R.id.text)).getText();
        //In most cases, position and id are the same, and they all start at 0.
        String showText = "Click first" + position + "Item, text content is:" + text + ",ID For:" + id;
        Toast.makeText(this, showText, Toast.LENGTH_LONG).show();
    }
}

2. OnScrollListener

void onScrollStateChanged(AbsListView view, int scrollState)

Monitor the change of rolling state.

View: The view being scrolled is the current ListView.

_scroll state: scroll state, there are several kinds of rolling state, respectively: scroll state, scroll state, scroll state, scroll state, scroll state, scroll state, scroll state, scroll state, scroll state, scroll

_SCROLL_STATE_TOUCH_SCROLL: Sliding state, finger sliding in view, and finger staying on the screen without leaving.

SCROLL_STATE_FLING: In the throwing state, the finger strokes hard before leaving the view (imagine the feeling of throwing the view out), and the view slides to stop according to inertia.

__SCROLL_STATE_IDLE: idle state, state of doing nothing. When the throwing state or touch rolling state ends, it enters the idle state.

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

_listens for NON-SCROLLING state, and as long as ListView does not scroll, it will be called continuously.

View: The view being scrolled is the current ListView.

_first VisibleItem: Index value of the first list item loaded.

visibleItemCount: The total number of list items loaded.

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

public class MainActivity extends Activity implements AbsListView.OnScrollListener{

    private ListView myListView;
    private SimpleAdapter simpleAdapter;
    private List<Map<String, Object>> data;

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

        data = new ArrayList<Map<String, Object>>();
        simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"img", "text"}, new int[]{R.id.img, R.id.text});
        myListView = (ListView) findViewById(R.id.myListView);
        myListView.setAdapter(simpleAdapter);

        //Setting up listeners
        myListView.setOnScrollListener(this);
    }

    private List<Map<String, Object>> getData() {
        for (int i = 0; i < 20; i++) {
            Map<String, Object>map = new HashMap<String, Object>();
            map.put("img", R.mipmap.ic_launcher);
            map.put("text", "initial simpleAdapter"+(i+1));
            data.add(map);
        }

        return data;
    }

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

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//        Toast.makeText(this, "scroll stopped, index of the first item loaded is:" + first Visible Item+
//                        ". The total number of list items loaded is:"+visibleItemCount +". The total number of list items that exist is:""+
//                        totalItemCount, Toast.LENGTH_LONG).show();
    }
}

Posted by pauleth on Wed, 17 Apr 2019 23:42:34 -0700