How to get the position of the last visible item inside an Adapter in Android

Nestor

I intend to create a listview with different layout for each visible row. I also set setStackFromBottom(true).

I know I can use int getItemViewType(int position) in View getView(int position, View convertView, ViewGroup parent) but it uses the fix position and not based on visible positions.

I'd like to use the listview as the following concept shows:

listview order

If I scroll down, the next item should replace the previous one and use its large layout. I tried it with the following code, but it uses fix position so always the last item in the list is the largest, not the last visible item.

 @Override
  public int getItemViewType(int position)
  {
    if(position==values.length-1)
      return 1;
    return 0;
  }

 @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = null;
    type=getItemViewType(position);
    if (type == 0)
      rowView = inflater.inflate(R.layout.list_item, parent, false);
    else
      rowView = inflater.inflate(R.layout.list_item2, parent, false);
    return rowView;
  }

I reckon, I should get the position of the first and last visible item inside the Adapter, but I don't know how to do it.

Can I somehow create a listview that follows the aforementioned concept? Can I get the correct positions?

Rick Falck

You can get a lot of info by setting a scroll listener on the ListView:

mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        // firstVisibleItem + visibleItemCount = last item
    }
}

However, I can't think of how you could use this info effectively. Even if you change getView() to return a larger View when its position is the same as the last item, you can't easily resize the views as they move up, because getView() won't be called again for items already displayed, unless you call notifyDataSetChanged() on the adapter repeatedly, which would kill performance.

My though is to add another View below the ListView that will display the larger View for the item below the last one in the ListView. Make it look like the last row in the ListView. You could change that view in the onScroll() method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get last visible item in QTreeView

From Dev

How to get position in Sectioned Adapter (Android)?

From Dev

How to get item from RecyclerView adapter in Android

From Dev

how to get the row item position number in listview adapter class

From Dev

How to get tapped List Item position in Adapter class on button click

From Dev

How to get the item position on a clickListener set in an Adapter class?

From Dev

How to get the visible list items count in a Xamarin android Array adapter

From Dev

Separated list adapter - get the position of the item

From Dev

How to get First Visible child item of android scrollview on scrollChange event

From Dev

Android RecyclerView - Keep last visible item visible when keyboard opened

From Dev

ListView inside a ListView Item, Adapter Always at position 0

From Dev

how to get list item position in android setOnTouchListener android?

From Dev

how to get list item position in android setOnTouchListener android?

From Dev

Android ListView get current position in visible items

From Dev

How to get position of the clicked item in Flexbox Layout in android?

From Dev

RecyclerView.Adapter get item position of my list

From Dev

Get the position of Item base on the value of TAG of custom view adapter

From Dev

Pager adapter not registering when I get to last item

From Dev

How move first item of arraylist to last position?

From Dev

Android ListView with Custom Adapter shows only last item

From Dev

How to get position of added in adapter object

From Dev

How to get position of added in adapter object

From Dev

how to get the position of an array adapter in another activity

From Dev

android: ViewPager: How to get the scroll position of the view inside the view pager?

From Dev

Android - How to manage position of viewpager in adapter?

From Dev

How to get the last item in an object

From Dev

How to get the last visible element in jQuery?

From Dev

how to get current visible item in Recycler View

From Dev

How to find the last visible word position in a text container?

Related Related

  1. 1

    How to get last visible item in QTreeView

  2. 2

    How to get position in Sectioned Adapter (Android)?

  3. 3

    How to get item from RecyclerView adapter in Android

  4. 4

    how to get the row item position number in listview adapter class

  5. 5

    How to get tapped List Item position in Adapter class on button click

  6. 6

    How to get the item position on a clickListener set in an Adapter class?

  7. 7

    How to get the visible list items count in a Xamarin android Array adapter

  8. 8

    Separated list adapter - get the position of the item

  9. 9

    How to get First Visible child item of android scrollview on scrollChange event

  10. 10

    Android RecyclerView - Keep last visible item visible when keyboard opened

  11. 11

    ListView inside a ListView Item, Adapter Always at position 0

  12. 12

    how to get list item position in android setOnTouchListener android?

  13. 13

    how to get list item position in android setOnTouchListener android?

  14. 14

    Android ListView get current position in visible items

  15. 15

    How to get position of the clicked item in Flexbox Layout in android?

  16. 16

    RecyclerView.Adapter get item position of my list

  17. 17

    Get the position of Item base on the value of TAG of custom view adapter

  18. 18

    Pager adapter not registering when I get to last item

  19. 19

    How move first item of arraylist to last position?

  20. 20

    Android ListView with Custom Adapter shows only last item

  21. 21

    How to get position of added in adapter object

  22. 22

    How to get position of added in adapter object

  23. 23

    how to get the position of an array adapter in another activity

  24. 24

    android: ViewPager: How to get the scroll position of the view inside the view pager?

  25. 25

    Android - How to manage position of viewpager in adapter?

  26. 26

    How to get the last item in an object

  27. 27

    How to get the last visible element in jQuery?

  28. 28

    how to get current visible item in Recycler View

  29. 29

    How to find the last visible word position in a text container?

HotTag

Archive