2015-08-26 46 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