我有一个由arrayadapter填充的列表视图。我想要ListView的第一行/单元格的不同布局。
我也发现了一个非常相似的问题,但是cldnt在我的代码中添加headerviewAndroid different Row layout only for first row in 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;
}
问题是,我的列表最初会获取10个项目。之后,当用户向下滚动时,我会加载更多的文章。现在列表的第11项也得到了第一项的布局。
另外,当我滚动到顶部时,第一个项目的布局将更改为其他列表项目的布局。
请帮忙。
最佳答案
覆盖适配器中的getViewTypeCount()
以返回2。覆盖getItemViewType()
以返回0(位置0)并返回1(所有其他位置)。这告诉ListView
您有两种不同的行布局,其中第一行(位置0)的布局与其他行不同。这将确保行回收为您的位置提供正确的行布局。
关于android - ArrayAdapter中ListView第一行的不同布局,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29796242/