卷轴上的底座适配器位置

Base Adapter Position on Scroll

我有这个适配器显示从网络服务检索到的列表中的项目,在滚动时它会膨胀第一个位置布局(它正在考虑第一个可见项目的位置 0 这是不正确的),请帮忙。

这是我的适配器:

private class hwListArrayAdapter extends BaseAdapter {


    private ArrayList<HomeWork> Items;
    private final Activity context;

    public hwListArrayAdapter(Activity context, ArrayList<HomeWork> items) {

        this.context = context;
        this.Items = items;
    }

    @Override
    public int getCount() {
        return Items.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return Items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        View v = view;
        TextView desc, date, course;
        if (position == 0) {

            LayoutInflater vi;
            vi = LayoutInflater.from(context);
            v = vi.inflate(R.layout.homework_list_top, null);

            AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, (int) (MyApplication.screenHeight * 0.05));
            v.setLayoutParams(params);
        } else {
            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(context);
                v = vi.inflate(R.layout.homework_list_item, null);
            }
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, (int) (MyApplication.screenHeight * 0.6) / 8);
            v.setLayoutParams(params);


            desc = (TextView) v.findViewById(R.id.descriptionHW);
            desc.setTypeface(MyApplication.dinar);
            desc.setText(Items.get(position - 1).getDetails());

            course = (TextView) v.findViewById(R.id.courseHW);
            course.setTypeface(MyApplication.dinar);
            course.setText(Items.get(position - 1).getCourseName());

            date = (TextView) v.findViewById(R.id.dateHW);
            date.setTypeface(MyApplication.dinar);
            date.setText(dateConverter(Items.get(position - 1).getDueDate()));


        }
        return v;
    }
}

您必须覆盖 getViewTypeCountgetItemViewType 以获得两个不同的 convertView。例如

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

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

getViewTypeCount 表示您要处理两种不同类型的行,getItemViewType return 是实际类型。请注意,getItemViewType 的 return 类型在内部被 android 用来寻址视图数组,因此请确保 return 始终是 0 之间的值和 getViewTypeCount() -1