我有一个应用程序可以通过适配器创建表格行,这里是适配器:

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,但是问题是当我向下滚动并向上滚动时,它消失了。

我的问题是:如何向该表添加标题?
还是添加此标头固定的,其他所有内容都可以滚动?

最佳答案

在您的适配器中,添加以下两个方法替代:

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

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


这告诉适配器您有两种类型的行布局,以及每种类型的行。

关于java - Android-将标题添加到表行(ArrayAdapter),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32237222/

10-09 05:51
查看更多