Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My colleague Joe, and I believe we have found a simpler way to solve the same problem. In our solution though instead of extending BaseAdapter we extend ArrayAdapter. </p> <p>The code is as fallows : </p> <pre><code> public class CircularArrayAdapter extends ArrayAdapter { public static final int HALF_MAX_VALUE = Integer.MAX_VALUE/2; public final int MIDDLE; private T[] objects; public CircularArrayAdapter(Context context, int textViewResourceId, T[] objects) { super(context, textViewResourceId, objects); this.objects = objects; MIDDLE = HALF_MAX_VALUE - HALF_MAX_VALUE % objects.length; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public T getItem(int position) { return objects[position % objects.length]; } } </code></pre> <p>So this creates a class called CircularArrayAdapter which take an object type T (which may be anything) and uses it to create an array list. T is commonly a string though may be anything. </p> <p>The constructor is the same as is for ArrayAdapter though initializes a constant called middle. This is the middle of the list. No matter what the length of the array MIDDLE can be used to center the ListView in the mid of the list. </p> <p>getCount is overrided to return a huge value as is done above creating a huge list.</p> <p>getItem is overrided to return the fake position on the array. Thus when filling the list the list is filled with objects in a looping manner.</p> <p>At this point CircularArrayAdapter simply replaces ArrayAdapter in the file creating the ListView. </p> <p>To center the ListView the fallowing line must be inserted in your file creating the ListView after the ListView object has been initialized:</p> <p><code> listViewObject.setSelectionFromTop(nameOfAdapterObject.MIDDLE, 0); </code></p> <p>and using the MIDDLE constant previously initialized for the list the view is centered with the top item of the list at the top of the screen.</p> <p>: ) ~ Cheers, I hope this solution is useful.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload