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 = "com.nra.productmarketingmaker:id/clickView";
    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()) {
        Log.addLog("swipe");

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

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

Using xpath

driver.findElementByXPath("(//android.widget.LinearLayout)[4]").click();

Using classname

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

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 (https://developer.android.com/reference/androidx/test/uiautomator/UiScrollable.html#flingToEnd(int))

e.g. is use:

    public void flingVerticalToBottom_Android() {
        try {
            driver.findElements(MobileBy.AndroidUIAutomator(
                    "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.