列表视图更改ImageView的位置滚动

时间:2015-03-28 12:56:53

标签: android android-listview

我正在使用List View和自定义数组适配器,适配器从Sqlite设置数据。但是当列表项超过10(或列表变为可滚动)时,如果我滚动继续,图像视图会自动更改位置。这是我的自定义适配器代码。谢谢

public class CustomeAdapter extends ArrayAdapter<String>{
 public Context context;

 public ArrayList<String> titleArrayList, descArrayList, timeArrayList, imgArrayList, IdArrayList;
    List data;
    int layoutResID;
    private Bitmap myBitmap;

    public CustomeAdapter(Context context, int layoutResourceId, ArrayList<String> titleArrayList, ArrayList<String> descArrayList, ArrayList<String> timeArrayList, ArrayList<String> imgArrayList, ArrayList<String> IdArrayList) {
        super(context, layoutResourceId, titleArrayList);
        this.context = context;
        this.titleArrayList = titleArrayList;
        this.descArrayList = descArrayList;
        this.timeArrayList = timeArrayList;
        this.imgArrayList = imgArrayList;
        this.IdArrayList = IdArrayList;
        this.data = data;
        this.layoutResID = layoutResourceId;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        final StockQuoteView sqView;

        if (rowView == null) {


            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            rowView = inflater.inflate(layoutResID, parent, false);
            sqView = new StockQuoteView();
            sqView.tag = (TextView) rowView.findViewById(R.id.tv_fullnote);

            sqView.desc = (TextView) rowView.findViewById(R.id.tv_title);
            sqView.time = (TextView) rowView.findViewById(R.id.tv_time);
            sqView.id = (TextView) rowView.findViewById(R.id.id);
            sqView.img = (ImageView) rowView.findViewById(R.id.imageView3);

            sqView.button1 = (ImageView) rowView.findViewById(R.id.swipe_button1);
            sqView.button2 = (ImageView) rowView.findViewById(R.id.swipe_button2);
            sqView.button3 = (ImageView) rowView.findViewById(R.id.swipe_button3);
            sqView.button4 = (ImageView) rowView.findViewById(R.id.swipe_button4);
            rowView.setTag(sqView);
        } else {
            sqView = (StockQuoteView) rowView.getTag();
        }

        try {
            sqView.tag.setText(titleArrayList.get(position));
            sqView.desc.setText(descArrayList.get(position));
            sqView.time.setText(timeArrayList.get(position));
            sqView.id.setText(IdArrayList.get(position));
            sqView.id.setTag(IdArrayList.get(position).toString());
            if (imgArrayList.get(position).contains("null")) {
                sqView.img.setImageBitmap(null);
                sqView.img.setTag("null");
            } else {
                myBitmap = BitmapFactory.decodeFile(imgArrayList.get(position));
                //sqView.img.setImageBitmap((position & 1) == 1 ? myBitmap : myBitmap);
                String TAG = "tag";
                String id = String.valueOf(position);

                sqView.img.setTag(imgArrayList.get(position).toString());
                int width = myBitmap.getWidth();
                int height = myBitmap.getHeight();
                int newWidth = (height > width) ? width : height;
                int newHeight = (height > width) ? height - (height - width) : height;
                int crop = (width - height) / 2;
                crop = (crop < 0) ? 0 : crop;
                Bitmap cropImg = Bitmap.createBitmap(myBitmap, crop, 0, newWidth, newHeight);
                sqView.img.setImageBitmap(cropImg);
            }
        } catch (Exception e) {
            System.out.print(e.toString());
        }


        return rowView;
    }

    protected static class StockQuoteView {
        protected TextView tag;
        protected TextView desc;
        protected TextView time;
        protected ImageView img;
        protected ImageView button1;
        protected ImageView button2;
        protected ImageView button3;
        protected ImageView button4;
        public TextView id;
    }


}
`

1 个答案:

答案 0 :(得分:0)

将此行sqView.img.setVisibility(View.INVISIBLE)放在sqView.img.setTag("null");之后,并使其隐藏在像else {sqView.img.setVisibility(View.VISIBLE) myBitmap = BitmapFactory.decodeFile(imgArrayList.get(position)); //sqView.img.setImageBitmap((position & 1) == 1 ? myBitmap : myBitmap); String TAG = "tag"; String id = String.valueOf(position);............}这样的其他参数中的imageView 这只是骗过。