滚动上的基本适配器位置

时间:2016-11-10 11:21:36

标签: android listview baseadapter

我有这个适配器显示从Web服务检索到的列表中的项目,在滚动它的第一个位置布局(它正在考虑第一个可见的项目,其位置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;
    }
}

1 个答案:

答案 0 :(得分:2)

您必须覆盖getViewTypeCountgetItemViewType才能获得两个不同的convertView。 E.g。

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

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

getViewTypeCount表示您要处理两种不同类型的行,而getItemViewType则返回实际类型。请注意,getItemViewType的返回类型在内部由android用于处理View数组,因此请确保始终返回0getViewTypeCount() -1之间的值