Question

I am having listview of contactNames and trying to move to the next alphabet of the contact when user swipe to the right. Ex:when anthony is there on the screen and when user swipes it should show the contact name starting with "b" .

So i am doing this:

 public void onSwipeRight() {
        final int offset = (int) (osList.getHeight() * .035f);
        //osList.smoothScrollToPositionFromTop(20,offset);
        Log.d("SwipeRight", "onFling: ");
        int lastPosition =osList.getLastVisiblePosition();
        viewHolder temp = (viewHolder)osList.getChildAt(lastPosition).getTag();
        String textValue = (String) temp.name.getText();
        osList.smoothScrollToPositionFromTop(getindexofnextAlphabet("textValue"),offset);

    }

But for the first time when i swipe getchildat is working but from second time onwards its returning null.

No correct solution

OTHER TIPS

First you must calculate the position (position means, position of first contact starting with "b" in ArrayList). For that you can store the number of contacts starting with "a", "b" and so on in an ArrayList.

So suppose on swipe you want to jump to the first contact starting with "b", then its position in the ArrayList is equal to the number of contacts starting with "a".

Now we have the position we want to jump to. I assume that osList is your ListView.

  1. First calculate the number of views that can be occupied on screen. For that we need to calculate pos1, which is the position of Top Visible View on screen and pos2, which is the position of Last Visible View on screen:

    pos1 = osList.getFirstVisiblePosition()
    pos2 = osList.getLastVisiblePosition()
    

    Now the number of views that can be occupied (not accurate) on screen is numScreenViews = pos2 - pos1 - 1. here -1 is to eliminate the possibility that any view is partially visible. After all we have a rough idea.

  2. Now inside osSwipeRight():

    public void onSwipeRight() {
      osList.smoothScrollToPostion(min(noOfContact -1, 
                                       position + numScreenViews));
    }
    

    You don't need to use getChildAt() here, however if you want to get the posOnScreen(I am calling it posOnScreen because it's not the position in the ArrayList as clarified by @Joel) of first visible contact starting with "b", again calculate pos1, then calculate posOnScreen = position of your view (here position of first contact starting with alphabet "b" in arrayList) - pos1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top