gpt4 book ai didi

android - ArrayAdapter 中 ListView 第一行的不同布局

转载 作者:太空狗 更新时间:2023-10-29 16:01:44 25 4
gpt4 key购买 nike

我有一个由 ArrayAdapter 填充的 listView。我想要 ListView 的第一行/单元格的不同布局。

我还发现了一个非常相似的问题,但 cldnt 在我的代码中添加 headerView Android 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com