Listitems在Android中的ListView中滚动时更改位置

时间:2017-10-11 06:08:12

标签: android listview adapter android-adapter

我是Android的新手,我正在使用包含图片和一些文字的 customlistitems 进行ListView,当我向上/向下滚动列表视图时,我发现了一个问题listItems的位置正在改变。那么有人可以帮助我吗?我在这里发布我的适配器以获取您的信息。希望你能帮我弄明白。

OfferAdapter

public class OfferAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<Offer> OfferList;


    public OfferAdapter(Context c, ArrayList<Offer> OfferList) {
        mContext = c;
        this.OfferList = OfferList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return OfferList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.raw_offer, null);
            TextView tv_ofr_hdr = (TextView) grid.findViewById(R.id.tv_ofr_hdr);
            ImageView iv_pic = (ImageView) grid.findViewById(R.id.iv_pic);
            TextView tv_ofr_desc = (TextView) grid.findViewById(R.id.tv_ofr_desc);
            TextView tv_date = (TextView) grid.findViewById(R.id.tv_date);
            tv_ofr_desc.setText(OfferList.get(position).getDescription());
            tv_ofr_hdr.setText(OfferList.get(position).getHeadline());
            Date from = new Date();
            Date to = new Date();
            SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");
            try {
                from = input.parse(OfferList.get(position).getStart_date());
                to = input.parse(OfferList.get(position).getEnd_date());      // parse input

            } catch (ParseException e) {
                e.printStackTrace();
            }

            tv_date.setText(output.format(from) + " TO " + output.format(to));

            Picasso.with(mContext)
                    .load(OfferList.get(position).getPhoto().replaceAll(" ", "%20"))
                    .placeholder(R.drawable.ic_no_img)
                    .error(R.drawable.ic_no_img)
                    .into(iv_pic);
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}

4 个答案:

答案 0 :(得分:1)

存在多个问题:

1。而不是

@Override
public Object getItem(int position) {
    return null;
}

你应该写

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

2。而不是

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {

        grid = new View(mContext);
        grid = inflater.inflate(R.layout.raw_offer, null);

        // [all that initializing stuff]

    } else {
        grid = (View) convertView;
    }

    return grid;
}

你应该写

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(mContext);

    View grid;
    if (convertView == null) {
        grid = inflater.inflate(R.layout.raw_offer, parent, false);
    } else {
        grid = convertView;
    }

    // [all that initializing stuff]

    return grid;
}

您还可以查看ViewHolder概念,以提高列表的性能:

答案 1 :(得分:0)

您需要修改getView()方法,如下所示 -

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid = convertView;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (grid == null) {
        grid = inflater.inflate(R.layout.raw_offer, parent, false);
    }
    TextView tv_ofr_hdr = (TextView) grid.findViewById(R.id.tv_ofr_hdr);
    ImageView iv_pic = (ImageView) grid.findViewById(R.id.iv_pic);
    TextView tv_ofr_desc = (TextView) grid.findViewById(R.id.tv_ofr_desc);
    TextView tv_date = (TextView) grid.findViewById(R.id.tv_date);
    tv_ofr_desc.setText(OfferList.get(position).getDescription());
    tv_ofr_hdr.setText(OfferList.get(position).getHeadline());
    Date from = new Date();
    Date to = new Date();
    SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");
    try {
            from = input.parse(OfferList.get(position).getStart_date());
            to = input.parse(OfferList.get(position).getEnd_date());      // parse input

    } catch (ParseException e) {
            e.printStackTrace();
    }

    tv_date.setText(output.format(from) + " TO " + output.format(to));

    Picasso.with(mContext)
                .load(OfferList.get(position).getPhoto().replaceAll(" ", "%20"))
                .placeholder(R.drawable.ic_no_img)
                .error(R.drawable.ic_no_img)
                .into(iv_pic);    
    return grid;
}

并且您还需要覆盖这两个方法 -

@Override
public int getViewTypeCount() {

   return getCount();
}

@Override
public int getItemViewType(int position) {

   return position;
}

答案 2 :(得分:0)

更改一些适配器方法,如 -

@Override
    public Offer getItem(int position) {
        // TODO Auto-generated method stub
        return OfferList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

所以你的适配器是 -

public class OfferAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<Offer> OfferList;


    public OfferAdapter(Context c, ArrayList<Offer> OfferList) {
        mContext = c;
        this.OfferList = OfferList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return OfferList.size();
    }

    @Override
    public Offer getItem(int position) {
        // TODO Auto-generated method stub
        return OfferList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.raw_offer, null);
            TextView tv_ofr_hdr = (TextView) grid.findViewById(R.id.tv_ofr_hdr);
            ImageView iv_pic = (ImageView) grid.findViewById(R.id.iv_pic);
            TextView tv_ofr_desc = (TextView) grid.findViewById(R.id.tv_ofr_desc);
            TextView tv_date = (TextView) grid.findViewById(R.id.tv_date);
            tv_ofr_desc.setText(OfferList.get(position).getDescription());
            tv_ofr_hdr.setText(OfferList.get(position).getHeadline());
            Date from = new Date();
            Date to = new Date();
            SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");
            try {
                from = input.parse(OfferList.get(position).getStart_date());
                to = input.parse(OfferList.get(position).getEnd_date());      // parse input

            } catch (ParseException e) {
                e.printStackTrace();
            }

            tv_date.setText(output.format(from) + " TO " + output.format(to));

            Picasso.with(mContext)
                    .load(OfferList.get(position).getPhoto().replaceAll(" ", "%20"))
                    .placeholder(R.drawable.ic_no_img)
                    .error(R.drawable.ic_no_img)
                    .into(iv_pic);
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}

答案 3 :(得分:0)

您的问题出在您的getView()实施中,并且误解了方法参数的作用以及此方法如何填充ListView。这是完全可以理解的; ListView有点复杂。

要理解的重要一点是ListView将构建一个“池”视图,然后回收它们,这样每次滚动时都不需要重新创建它们列表。您之前创建并从getView()返回的视图可以作为convertView参数传入;在这些情况下您需要做的就是更新该视图的UI。

考虑到这一点,您可以按如下方式重写getView()

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

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        grid = inflater.inflate(R.layout.raw_offer, parent, false);
    } else {
        grid = convertView;
    }

    TextView tv_ofr_hdr = (TextView) grid.findViewById(R.id.tv_ofr_hdr);
    ImageView iv_pic = (ImageView) grid.findViewById(R.id.iv_pic);
    TextView tv_ofr_desc = (TextView) grid.findViewById(R.id.tv_ofr_desc);
    TextView tv_date = (TextView) grid.findViewById(R.id.tv_date);
    tv_ofr_desc.setText(OfferList.get(position).getDescription());
    tv_ofr_hdr.setText(OfferList.get(position).getHeadline());
    Date from = new Date();
    Date to = new Date();
    SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");
    try {
        from = input.parse(OfferList.get(position).getStart_date());
        to = input.parse(OfferList.get(position).getEnd_date());      // parse input
    } catch (ParseException e) {
        e.printStackTrace();
    }

    tv_date.setText(output.format(from) + " TO " + output.format(to));

    Picasso.with(mContext)
            .load(OfferList.get(position).getPhoto().replaceAll(" ", "%20"))
            .placeholder(R.drawable.ic_no_img)
            .error(R.drawable.ic_no_img)
            .into(iv_pic);

    return grid;
}

您还可以进行其他优化。最简单的是摆脱View grid变量;你可以直接使用convertView。更复杂的是使用“查看持有者”模式来缓存各种findViewById()调用的结果。