Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>These handle the case where you want different <strong>types</strong> of view for different rows. For instance, in a contacts application you may want even rows to have pictures on the left side and odd rows to have pictures on the right. In that case, you would use:</p> <pre><code>@Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { return position % 2; } </code></pre> <p>The framework uses your view type to decide <strong>which views to hand you via <code>convertView</code> in your <code>getView</code> method</strong>. In other words, in the above example, your even rows will only get recycled views with pictures on the left side to reuse, and odd rows will only get ones with pictures on the right.</p> <p>If every row in your list has the same layout, you don't need to worry about view types. In fact, <a href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/widget/BaseAdapter.java;h=532fd766ec66ae54a6e4b3def4b8bdc839c1db7a;hb=refs/heads/master" rel="noreferrer">BaseAdapter.java</a> provides a default behavior for all adapters:</p> <pre><code>public int getItemViewType(int position) { return 0; } public int getViewTypeCount() { return 1; } </code></pre> <p>This indeed provides you with the same view type for every row.</p> <p><strong>Edit</strong> - to outline the general flow:</p> <ol> <li>You bind data to your <code>AdapterView</code> using an adapter.</li> <li>The <code>AdapterView</code> tries to display items that are visible to the user.</li> <li>The framework calls <code>getItemViewType</code> for row <code>n</code>, the row it is about to display.</li> <li>The framework checks its recycled views pool for views of row <code>n</code>'s type. It doesn't find any because no views have been recycled yet.</li> <li><code>getView</code> is called for row <code>n</code>.</li> <li>You call <code>getItemViewType</code> for row <code>n</code> to determine what type of view you should use.</li> <li>You use an if/switch statement to inflate a different xml file depending on which view type is required.</li> <li>You fill the view with information.</li> <li>You return the view, exiting <code>getView</code>, and your row's view is displayed to the user.</li> </ol> <p>Now, when a view is recycled by scrolling off the screen it goes into a recycled views pool that is managed by the framework. These are essentially organized by view type so that a view of the correct type is given to you in <code>convertView</code> parameter in your <code>getView</code> method:</p> <ol> <li>The framework again calls <code>getItemViewType</code> for the row it wants to display.</li> <li>This time, there is a view in the recycled pool of the appropriate type.</li> <li>The recycled view is passed to you as the <code>convertView</code> parameter to your <code>getView</code> method.</li> <li>You fill the recycled view with new information and return it.</li> </ol>
 

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