How to perform vertical scroll using index for android?

I want to scroll in list and want to select element which has index no 5.
All Id, classname, package and content-desc are same only index is different.
I tried below mentioned method but it wasn’t successful.

>  private void selectCard(int _index) {
    int index = _index;
    String id = "";
    List<MobileElement> listItems = driver.findElementsById(id);

    AndroidElement firstElement = (AndroidElement) listItems.get(0);
    AndroidElement lastElement = (AndroidElement) listItems.get(listItems.size() - 1);

    Point fromPoint = lastElement.getCenter();
    Point endPoint = firstElement.getCenter();
    while (index > listItems.size()) {

        actions.swipeOnScreen(fromPoint, endPoint);
        index -= listItems.size();
    AndroidElement selectedItem = (AndroidElement) listItems.get(index);;

    Log.addLog("total: " + listItems.size());

Using xpath


Using classname

AndroidElement index = (AndroidElement) driver.findElementsByClassName(“android.widget.LinearLayout”).get(2);;

Need support to get it solved.

You cant scroll to 4th el if all 4 elelements not fit on screen with Android. So you can only scroll to el with some known text/description/id.

thanks for reply.
one more question is that can we scroll to last element using any method and select any random ele?

use (

e.g. is use:

    public void flingVerticalToBottom_Android() {
        try {
                    "new UiScrollable(new UiSelector()).setAsVerticalList().flingToEnd(2)"));
        } catch (Exception e) {
            // ignore

this reaching end and does 2 tries scroll more for sure

Able to scroll on screen now. Thanks it working.