图像在 android 的水平 ListView 中重复

Images are repeated in horizontal ListView in android

我在 android 中遇到了与水平 ListView 相关的问题,我创建了一个自定义水平 ListView,其中 imageView 为一行,一切都按我的需要工作,但我面临的问题是第一个最后一行 duplicated.I 正在发布我的代码。

水平适配器

public class HorizonAdapter extends BaseAdapter {
    private Context mContext;
    private final ArrayList<String> imgs;

    public HorizonAdapter(Context c, ArrayList<String> imgs) {
        mContext = c;
        this.imgs = imgs;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return imgs.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_product, null );
            ImageView imageView = (ImageView) grid.findViewById( R.id.iv_product );
            System.out.print( "======IMAGE=====>"+imgs.get( position ) );
            Picasso.with( mContext )
                    .load( imgs.get( position ).replaceAll(" ", "%20") )
                    .placeholder( R.drawable.ic_no_img )
                    .error( R.drawable.ic_no_img )
                    .into( imageView );
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}

在您的适配器中更改此设置 class

@Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return 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_product, null );
    } else {
        grid = (View) convertView;
    }
    ImageView imageView = (ImageView) grid.findViewById( R.id.iv_product );
        System.out.print( "======IMAGE=====>"+imgs.get( position ) );
        Picasso.with( mContext )
                .load( imgs.get( position ).replaceAll(" ", "%20") )
                .placeholder( R.drawable.ic_no_img )
                .error( R.drawable.ic_no_img )
                .into( imageView );

    return grid;
}

更改为:

@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_product, null );
    } else {
        grid = (View) convertView;
    }
    ImageView imageView = (ImageView) grid.findViewById( R.id.iv_product );
        System.out.print( "======IMAGE=====>"+imgs.get( position ) );
        Picasso.with( mContext )
                .load( imgs.get( position ).replaceAll(" ", "%20") )
                .placeholder( R.drawable.ic_no_img )
                .error( R.drawable.ic_no_img )
                .into( imageView );

    return grid;
}