时间:2017-08-16 12:30:16

标签: android horizontallist

我陷入了与android中的水平ListView相关的问题,我创建了一个自定义的水平ListView,其中imageView作为一行,All正在按需要工作,但我面临的问题是第一行和最后一行是否重复。我正在发布我的代码。

Horizo​​nAdapter

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;
    }
}

2 个答案:

答案 0 :(得分:1)

更改为:

@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;
}

答案 1 :(得分:0)

在适配器类中更改此内容

@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;
}