2015-08-26 2 views
0

У меня есть одно приложение, которое создает строку таблицы из адаптера.
Здесь адаптер:Android - добавить заголовок в строку таблицы (ArrayAdapter)

public class TableAdapter extends ArrayAdapter<Table> { 

    public TableAdapter(Context context, int textViewResourceId) { 
     super(context, textViewResourceId); 
    } 

    public TableAdapter(Context context, int resource, List<Table> items) { 
     super(context, resource, items); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder holder; 

     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_table, parent, false); 

      holder.position = (TextView) convertView.findViewById(R.id.position); 
      holder.progress = (ProgressBar) convertView.findViewById(R.id.progress); 
      holder.image = (ImageView) convertView.findViewById(R.id.image); 
      holder.name = (TextView) convertView.findViewById(R.id.name); 
      holder.points = (TextView) convertView.findViewById(R.id.points); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     if (position != 0) { 
      convertView.findViewById(R.id.header).setVisibility(View.GONE); 
     } 

     holder.position.setText(String.valueOf(getItem(position).getPosition())); 
     holder.image.setVisibility(View.GONE); 
     holder.progress.setVisibility(View.VISIBLE); 
     holder.name.setText(getItem(position).getName()); 
     holder.points.setText(String.valueOf(getItem(position).getPoints())); 

     final ViewHolder tmp = holder; 
     Picasso.with(getContext()).load(getContext().getResources().getString(R.string.team_logo) + getItem(position).getId() + ".png").into(holder.image, new Callback() { 
      @Override 
      public void onSuccess() { 
       tmp.progress.setVisibility(View.GONE); 
       tmp.image.setVisibility(View.VISIBLE); 
      } 

      @Override 
      public void onError() { 

      } 
     }); 

     return convertView; 
    } 

    class ViewHolder { 
     TextView position; 
     ProgressBar progress; 
     ImageView image; 
     TextView name; 
     TextView points; 
    } 
} 

и расположение:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" > 

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

     <TextView 
      android:id="@+id/position" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <RelativeLayout 
      android:id="@+id/imgHolder" 
      android:layout_width="50dp" 
      android:layout_height="50dp" > 

      <ProgressBar 
       android:id="@+id/progress" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:indeterminate="true" 
       android:layout_centerVertical="true" 
       android:layout_centerHorizontal="true" /> 

      <ImageView 
       android:id="@+id/image" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_centerVertical="true" 
       android:layout_centerHorizontal="true" /> 

     </RelativeLayout> 

     <TextView 
      android:id="@+id/name" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:id="@+id/points" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </TableRow> 
</TableLayout> 

До сих пор все работает прекрасно, как и ожидалось.

Но я хотел бы добавить заголовок к таблице. Что-то вроде:

# | Name | Pts 


Я попытался добавить это в макете, и проверить, если position != 0 и установить видимость GONE, но проблема в том, когда я прокручиваю вниз и вверх она исчезает.

Мой вопрос: Как я могу добавить и заголовок в эту таблицу?
Или добавить этот заголовок исправленный, и все остальное прокручивается?

ответ

1

В адаптере, добавьте эти два метода переопределения:

@Override 
public int getViewTypeCount() { 
    return 2; 
} 

@Override 
public int getItemViewType(int position) { 
    return position == 0 ? 0 : 1; 
} 

Это говорит адаптер, что у вас есть два типа расположения строк и строки, которые имеют каждый тип.

+0

ОК, это работает, я считаю, что есть кое-какое обходное решение, которое могло бы работать, вместо добавления заголовка для всех строк, но это прекрасно. Спасибо – BrunoRamalho

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