ArrayAdapter中ListView第一行的不同布局

Swa*_*nan 2 android android-arrayadapter android-layout android-listview

我有一个由ArrayAdapter填充的listView.我想要ListView的第一个Row/Cell的不同布局.

另外我发现了一个非常相似的问题,但cldnt在我的代码中添加了headerView,不同的行布局仅用于BaseAdapter中的第一行

我使用下面的代码实现了它:

public class ActorAdapter extends ArrayAdapter<Actors> {
ArrayList<Actors> actorList;
LayoutInflater vi;
int Resource1;
int Resource2;
ViewHolder holder;

public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
    super(context, resource, objects);
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Resource1 = resource;
    Resource2 = resource;
    actorList = objects;
}


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

    View v = convertView;

    if (v == null) {
        holder = new ViewHolder();
        if (position == 0){
            v = vi.inflate(R.layout.first_item, null);
            holder.imageview = (ImageView) v.findViewById(R.id.thumb);
            holder.tvName = (TextView) v.findViewById(R.id.title);

        }
        else{
            v = vi.inflate(R.layout.list_item, null);
            holder.imageview = (ImageView) v.findViewById(R.id.thumb);
            holder.tvName = (TextView) v.findViewById(R.id.title);

        }

        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
    holder.imageview.setImageResource(R.drawable.ic_launcher);
    Picasso.with(getContext()).load(actorList.get(position).getImage()).into(holder.imageview);
    String postTitle = actorList.get(position).getName();
    Spanned deTitle = Html.fromHtml(Html.fromHtml((String) postTitle).toString());
    holder.tvName.setText(String.valueOf(deTitle));

    return v;

}
Run Code Online (Sandbox Code Playgroud)

问题是,我的列表最初提取了10个项目.之后,当用户向下滚动时,我会加载更多文章.现在列表的第11项也获得第1项的布局.

此外,当我滚动回到顶部时,第一项的布局将更改为其他列表项的布局.

请帮忙.

Com*_*are 6

覆盖getViewTypeCount()在适配器返回2.改写getItemViewType()为位置0返回0和所有其他位置返回1.这教导ListView您有两种不同的行布局,其中第一行(位置0)具有与其他行不同的布局.这将确保行回收可以为您的位置提供正确的行布局.