如何在Listview中重用图像?

时间:2015-12-16 10:28:41

标签: android listview

例如:

我有200件物品,但只有3张图片。我想要:

位置0处的

项目 - 显示图像1

位置1的

项目 - 显示图像2

位置2的

项目 - 显示图像3

位置4的

项目 - 显示图像1 ....

但是当我滚动时,项目没有按正确的顺序显示图像。这是我的适配器:

public class MyAdapter extends BaseAdapter {

    private Context mContext;
    private int[] mResourceId;
    private ImageLoader mImageLoader;
    private int dummycount1 = 0;
    private int dummycount2 = 1;
    private int dummycount3 = 2;
    private int dummycount4 = 3;
    private ViewHolder viewHolder;

    public MyAdapter(Context context, int[] resourceId) {
        // TODO Auto-generated constructor stub
        mContext = context;
        mResourceId = resourceId;
        mImageLoader = new ImageLoader(mContext);
    }

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

    @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

        if (convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            convertView = layoutInflater.inflate(R.layout.item_listview, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.img1 = (ImageView) convertView.findViewById(R.id.imageView1);
            viewHolder.img2 = (ImageView) convertView.findViewById(R.id.imageView2);
            viewHolder.img3 = (ImageView) convertView.findViewById(R.id.imageView3);
            viewHolder.pb1 = (ProgressBar) convertView.findViewById(R.id.progressBar1);
            viewHolder.pb2 = (ProgressBar) convertView.findViewById(R.id.progressBar2);
            viewHolder.pb3 = (ProgressBar) convertView.findViewById(R.id.progressBar3);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Log.e("POSITION", "" + position);
        if (position == 0 || position == dummycount1 + 4) {
            myFunction(0);
            dummycount1 += 4;
        }
        else if (position == 1 || position == dummycount2 + 4) {
            myFunction(3);
            dummycount2 += 4;
        }
        else if (position == 2 || position == dummycount3 + 4) {
            myFunction(6);
            dummycount3 += 4;
        }
        else if (position == 3 || position == dummycount4 + 4) {
            myFunction(9);
            dummycount4 += 4;
        }
        return convertView;
    }

1 个答案:

答案 0 :(得分:0)

你可以使用下面的检查 -

if(position%3 == 0){
    //show image 1
}else if(position%3 == 1){
    //show image 2
}else{
    //show image 3
}