2013-11-26 3 views
1

Я делаю макет немного сложна, так что я не могу исправить ошибку, которая обозначается LINT (... есть черника, чем 80 видов, плохо для производительности) Это расположение:как решить «Макет имеет более 80 просмотров, плохо для производительности»?

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".Activity_schedapunti"> 


    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".7" 
      android:gravity="center_horizontal" 
      android:text="@string/esame_visivo" 
      android:textColor="@android:color/white" 
      android:textStyle="bold" 
      android:background="@android:color/black"/> 

      <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".3" 
      android:gravity="center_horizontal" 
      android:text="@string/punteggio_parziale" 
      android:textColor="@android:color/white" 
      android:textStyle="bold" 
      android:background="@android:color/black"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:orientation="horizontal">    
       <TextView 
        android:id="@+id/textview_aspetto" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/aspetto"/>     
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/coeff_x1" 
        android:textStyle="bold" 
        android:textColor="@color/light_yellow"/> 
       <EditText 
        android:id="@+id/edittext_aspetto" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:inputType="number" 
        android:singleLine="true" /> 
      </LinearLayout> 

      <TextView 
       android:id="@+id/textview_puteggio_parziale_aspetto" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight=".3"/> 
     </TableRow> 
     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:orientation="horizontal">    
       <TextView 
        android:id="@+id/textview_punti_colore" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/colore"/>     
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/coeff_x2" 
        android:textStyle="bold" 
        android:textColor="@color/light_yellow"/> 
       <EditText 
        android:id="@+id/edittext_punti_colore" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:inputType="number" 
        android:singleLine="true" /> 
      </LinearLayout> 

      <TextView 
       android:id="@+id/textview_puteggio_parziale_colore" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight=".3"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:gravity="center_horizontal" 
       android:text="@string/esame_olfattivo" 
       android:textColor="@android:color/white" 
       android:textStyle="bold" 
       android:background="@android:color/black"/> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".3" 
       android:gravity="center_horizontal" 
       android:text="@string/punteggio_parziale" 
       android:textColor="@android:color/white" 
       android:textStyle="bold" 
       android:background="@android:color/black"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:orientation="horizontal">    
       <TextView 
        android:id="@+id/textview_punti_intensita" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/intensita"/>     
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/coeff_x1" 
        android:textStyle="bold" 
        android:textColor="@color/light_yellow"/> 
       <EditText 
        android:id="@+id/edittext_punti_intensita" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:inputType="number" 
        android:singleLine="true" /> 
      </LinearLayout> 
      <TextView 
       android:id="@+id/textview_puteggio_parziale_intensita" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight=".3"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:orientation="horizontal">    
       <TextView 
        android:id="@+id/textview_punti_complessita" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/complessita"/>     
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/coeff_x2" 
        android:textStyle="bold" 
        android:textColor="@color/light_yellow"/> 
       <EditText 
        android:id="@+id/edittext_punti_complessita" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:inputType="number" 
        android:singleLine="true" /> 
      </LinearLayout> 
      <TextView 
       android:id="@+id/textview_puteggio_parziale_complessita" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight=".3"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:orientation="horizontal">    
       <TextView 
        android:id="@+id/textview_punti_qualita" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/qualita"/>     
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/coeff_x3" 
        android:textStyle="bold" 
        android:textColor="@color/light_yellow"/> 
       <EditText 
        android:id="@+id/edittext_punti_qualita" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:inputType="number" 
        android:singleLine="true" /> 
      </LinearLayout> 
      <TextView 
       android:id="@+id/textview_puteggio_parziale_qualita" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight=".3"/> 
     </TableRow> 




     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:gravity="center_horizontal" 
       android:text="@string/esame_gusto_olfattivo" 
       android:textColor="@android:color/white" 
       android:textStyle="bold" 
       android:background="@android:color/black"/> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".3" 
       android:gravity="center_horizontal" 
       android:text="@string/punteggio_parziale" 
       android:textColor="@android:color/white" 
       android:textStyle="bold" 
       android:background="@android:color/black"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:orientation="horizontal">    
       <TextView 
        android:id="@+id/textview_punti_struttura" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/struttura"/>     
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/coeff_x1" 
        android:textStyle="bold" 
        android:textColor="@color/light_yellow"/> 
       <EditText 
        android:id="@+id/edittext_punti_struttura" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:inputType="number" 
        android:singleLine="true" /> 
      </LinearLayout> 
      <TextView 
       android:id="@+id/textview_puteggio_parziale_struttura" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight=".3"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:orientation="horizontal">    
       <TextView 
        android:id="@+id/textview_punti_equilibrio" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/equilibrio"/>     
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/coeff_x1" 
        android:textStyle="bold" 
        android:textColor="@color/light_yellow"/> 
       <EditText 
        android:id="@+id/edittext_punti_equilibrio" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:inputType="number" 
        android:singleLine="true" /> 
      </LinearLayout> 
      <TextView 
       android:id="@+id/textview_puteggio_parziale_equilibrio" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight=".3"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:orientation="horizontal">    
       <TextView 
        android:id="@+id/textview_punti_intensita_gusto_olfattivo" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/intensita"/>     
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/coeff_x1" 
        android:textStyle="bold" 
        android:textColor="@color/light_yellow"/> 
       <EditText 
        android:id="@+id/edittext_punti_intensita_gusto_olfattivo" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:inputType="number" 
        android:singleLine="true" /> 
      </LinearLayout> 
      <TextView 
       android:id="@+id/textview_puteggio_parziale_intensita_gusto_olfattivo" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight=".3"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:orientation="horizontal">    
       <TextView 
        android:id="@+id/textview_punti_persistenza" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/persistenza"/>     
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/coeff_x2" 
        android:textStyle="bold" 
        android:textColor="@color/light_yellow"/> 
       <EditText 
        android:id="@+id/edittext_punti_persistenza" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:inputType="number" 
        android:singleLine="true" /> 
      </LinearLayout> 
      <TextView 
       android:id="@+id/textview_puteggio_parziale_persistenza" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight=".3"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:orientation="horizontal">    
       <TextView 
        android:id="@+id/textview_punti_qualita_gusto_olfattivo" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/qualita"/>     
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/coeff_x3" 
        android:textStyle="bold" 
        android:textColor="@color/light_yellow"/> 
       <EditText 
        android:id="@+id/edittext_punti_qualita_gusto_olfattivo" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:inputType="number" 
        android:singleLine="true" /> 
      </LinearLayout> 
      <TextView 
       android:id="@+id/textview_puteggio_parziale_qualita_gusto_olfattivo" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight=".3"/> 
     </TableRow> 


     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:orientation="horizontal">    
       <TextView 
        android:id="@+id/textview_punti_armonia" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/armonia"/>     
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/coeff_x3" 
        android:textStyle="bold" 
        android:textColor="@color/light_yellow"/> 
       <EditText 
        android:id="@+id/edittext_punti_armonia" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:inputType="number" 
        android:singleLine="true" /> 
      </LinearLayout> 
      <TextView 
       android:id="@+id/textview_puteggio_parziale_armonia" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight=".3"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".7" 
       android:gravity="center_horizontal" 
       android:text="@string/punteggio_totale" 
       android:textColor="@android:color/white" 
       android:textStyle="bold" 
       android:background="@android:color/black"/> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight=".3" 
       android:gravity="center_horizontal" 
       android:textColor="@color/light_yellow" 
       android:textStyle="bold" 
       android:background="@android:color/black"/> 
     </TableRow> 
    </TableLayout> 
</ScrollView> 

Я прочитал, чтобы использовать ListView, но как мне это сделать? Не все строки равны или в любом случае в середине есть некоторые элементы, которые не равны всем остальным строкам!

Изображение:

enter image description here

+3

Можете ли вы разместить изображение того, как оно должно выглядеть? Это, вероятно, поможет.С помощью «ListView» вы можете использовать пользовательский «макет» в своем «адаптере» и принять решение показывать/не показывать определенные 'View' в зависимости от некоторого значения, если это работает для вас. – codeMagic

+0

Какой макет вы пытаетесь реализовать? Снимок экрана был бы приятным. Кроме того, использование этого множества просмотров замедлит ваше приложение –

+0

@codeMagic Я добавляю изображение;) – user2263764

ответ

2

Это определенно выглядит хорошим кандидатом на ListView. Как я сказал в своем комментарии, вы можете использовать пользовательский layout в своем Adapter. Я не знаю, как вы получаете данные для этого, поэтому я не могу точно сказать, как проверить, но в каждом раунде getView() вы можете проверить данные и решить, какие View s показать/скрыть.

Просто создайте layout с TextView с, и т.д ..., что каждая строка может понадобиться, похоже, вероятно, внутри вертикальной LinearLayout с горизонтальными LinearLayout с внутри или просто RelativeLayout затем изменить visibility к visible/gone в зависимости от того, что тебе нужно.

Если вы не знакомы с этим, вы можете увидеть this tutorial на этом.

Adapter Docs

ListView Docs

Как было отмечено в комментарии, можно надуть различные layout с в вашем getView() методе вашего Adapter, если у вас есть очень разные layouts, которые могут сделать переключая visibility плохой подход. Это зависит только от ваших данных и от layout.

+1

Или 'ListView' также поддерживает несколько типов строк, вместо show hide вы можете добавить suppport для нескольких типов строк и раздуть соответствующую строку из' getView() '. –

+0

Я решил! Благодаря!! Я не знаю, правильно ли я решил .... но ... Я решил с внедрением ListView – user2263764

+0

Ха-ха ... добро пожаловать! Если вы решили с помощью «ListView», и он работает, то вы, вероятно, решили правильно. – codeMagic

0

Вау это плохо, использовать ListView вместо TableView и динамически добавлять строки на основе какой раздел вы в

Я продлит BaseAdapter к. достигните этого

0

вы должны использовать MergeAdapter, где вы можете объединить адаптер differents только с одним. Например, названия и значения. Я получил код из StackOverflow, но я не могу найти тему, поэтому я вставляю здесь код, который я нашел.

package com.mydocum.adapters; 

import java.util.ArrayList; 

import android.database.DataSetObserver; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ListAdapter; 
import android.widget.SectionIndexer; 
import android.widget.TextView; 

/** 
* Adapter that merges multiple child adapters and views into a single 
* contiguous whole. 
* 
* Adapters used as pieces within MergeAdapter must have view type IDs 
* monotonically increasing from 0. Ideally, adapters also have distinct ranges 
* for their row ids, as returned by getItemId(). 
* 
*/ 
public class MergeAdapter extends BaseAdapter implements SectionIndexer { 
    protected ArrayList<ListAdapter> pieces = new ArrayList<ListAdapter>(); 
    protected String noItemsText; 

    /** 
    * Stock constructor, simply chaining to the superclass. 
    */ 
    public MergeAdapter() { 
     super(); 
    } 

    /** 
    * Adds a new adapter to the roster of things to appear in the aggregate 
    * list. 
    * 
    * @param adapter 
    *   Source for row views for this section 
    */ 
    public void addAdapter(ListAdapter adapter) { 
     pieces.add(adapter); 
     adapter.registerDataSetObserver(new CascadeDataSetObserver()); 
    } 

    /** 
    * Get the data item associated with the specified position in the data set. 
    * 
    * @param position 
    *   Position of the item whose data we want 
    */ 
    public Object getItem(int position) { 
     for (ListAdapter piece : pieces) { 
      int size = piece.getCount(); 

      if (position < size) { 
       return (piece.getItem(position)); 
      } 

      position -= size; 
     } 

     return (null); 
    } 

    public void setNoItemsText(String text){ 
     noItemsText = text; 
    } 

    /** 
    * Get the adapter associated with the specified position in the data set. 
    * 
    * @param position 
    *   Position of the item whose adapter we want 
    */ 
    public ListAdapter getAdapter(int position) { 
     for (ListAdapter piece : pieces) { 
      int size = piece.getCount(); 

      if (position < size) { 
       return (piece); 
      } 

      position -= size; 
     } 

     return (null); 
    } 

    /** 
    * How many items are in the data set represented by this Adapter. 
    */ 
    public int getCount() { 
     int total = 0; 

     for (ListAdapter piece : pieces) { 
      total += piece.getCount(); 
     } 

     if(total == 0 && noItemsText != null){ 
      total = 1; 
     } 

     return (total); 
    } 

    /** 
    * Returns the number of types of Views that will be created by getView(). 
    */ 
    @Override 
    public int getViewTypeCount() { 
     int total = 0; 

     for (ListAdapter piece : pieces) { 
      total += piece.getViewTypeCount(); 
     } 

     return (Math.max(total, 1)); // needed for setListAdapter() before 
             // content add' 
    } 

    /** 
    * Get the type of View that will be created by getView() for the specified 
    * item. 
    * 
    * @param position 
    *   Position of the item whose data we want 
    */ 
    @Override 
    public int getItemViewType(int position) { 
     int typeOffset = 0; 
     int result = -1; 

     for (ListAdapter piece : pieces) { 
      int size = piece.getCount(); 

      if (position < size) { 
       result = typeOffset + piece.getItemViewType(position); 
       break; 
      } 

      position -= size; 
      typeOffset += piece.getViewTypeCount(); 
     } 

     return (result); 
    } 

    /** 
    * Are all items in this ListAdapter enabled? If yes it means all items are 
    * selectable and clickable. 
    */ 
    @Override 
    public boolean areAllItemsEnabled() { 
     return (false); 
    } 

    /** 
    * Returns true if the item at the specified position is not a separator. 
    * 
    * @param position 
    *   Position of the item whose data we want 
    */ 
    @Override 
    public boolean isEnabled(int position) { 
     for (ListAdapter piece : pieces) { 
      int size = piece.getCount(); 

      if (position < size) { 
       return (piece.isEnabled(position)); 
      } 

      position -= size; 
     } 

     return (false); 
    } 

    /** 
    * Get a View that displays the data at the specified position in the data 
    * set. 
    * 
    * @param position 
    *   Position of the item whose data we want 
    * @param convertView 
    *   View to recycle, if not null 
    * @param parent 
    *   ViewGroup containing the returned View 
    */ 
    public View getView(int position, View convertView, ViewGroup parent) { 
     for (ListAdapter piece : pieces) { 
      int size = piece.getCount(); 

      if (position < size) { 

       return (piece.getView(position, convertView, parent)); 
      } 

      position -= size; 
     } 

     if(noItemsText != null){ 
      TextView text = new TextView(parent.getContext()); 
      text.setText(noItemsText); 
      return text; 
     } 

     return (null); 
    } 

    /** 
    * Get the row id associated with the specified position in the list. 
    * 
    * @param position 
    *   Position of the item whose data we want 
    */ 
    public long getItemId(int position) { 
     for (ListAdapter piece : pieces) { 
      int size = piece.getCount(); 

      if (position < size) { 
       return (piece.getItemId(position)); 
      } 

      position -= size; 
     } 

     return (-1); 
    } 

    public int getPositionForSection(int section) { 
     int position = 0; 

     for (ListAdapter piece : pieces) { 
      if (piece instanceof SectionIndexer) { 
       Object[] sections = ((SectionIndexer) piece).getSections(); 
       int numSections = 0; 

       if (sections != null) { 
        numSections = sections.length; 
       } 

       if (section < numSections) { 
        return (position + ((SectionIndexer) piece) 
          .getPositionForSection(section)); 
       } else if (sections != null) { 
        section -= numSections; 
       } 
      } 

      position += piece.getCount(); 
     } 

     return (0); 
    } 

    public int getSectionForPosition(int position) { 
     int section = 0; 

     for (ListAdapter piece : pieces) { 
      int size = piece.getCount(); 

      if (position < size) { 
       if (piece instanceof SectionIndexer) { 
        return (section + ((SectionIndexer) piece) 
          .getSectionForPosition(position)); 
       } 

       return (0); 
      } else { 
       if (piece instanceof SectionIndexer) { 
        Object[] sections = ((SectionIndexer) piece).getSections(); 

        if (sections != null) { 
         section += sections.length; 
        } 
       } 
      } 

      position -= size; 
     } 

     return (0); 
    } 

    public Object[] getSections() { 
     ArrayList<Object> sections = new ArrayList<Object>(); 

     for (ListAdapter piece : pieces) { 
      if (piece instanceof SectionIndexer) { 
       Object[] curSections = ((SectionIndexer) piece).getSections(); 

       if (curSections != null) { 
        for (Object section : curSections) { 
         sections.add(section); 
        } 
       } 
      } 
     } 

     if (sections.size() == 0) { 
      return (null); 
     } 

     return (sections.toArray(new Object[0])); 
    } 

    private class CascadeDataSetObserver extends DataSetObserver { 
     @Override 
     public void onChanged() { 
      notifyDataSetChanged(); 
     } 

     @Override 
     public void onInvalidated() { 
      notifyDataSetInvalidated(); 
     } 
    } 
} 

Вот как вам нужно использовать:

MergeAdapter adapter = new MergeAdapter(); 
AdapterA adapterA = new AdapterA(...); 
AdapterB adapterB = new AdapterB(...); 

adapter.addAdapter(adapterA); 
adapter.addAdapter(adapterB); 

listview.setAdapter(adapter); 

В ListView вы будете иметь adapterA и andapterB один после другого и душа correcly. Два адаптера могут иметь diffente-объект.

До свидания.

Смежные вопросы