List View changes position of ImageView On scroll

半腔热情 提交于 2019-12-23 06:11:40

问题


I am using List View with custom array adapter the adapter sets data from Sqlite. But when the list items reach over 10 (or list becomes scroll able) the Image View changes position automatically if i scrolls continue. here is my custom adapter's code.Thanks

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:


put this linesqView.img.setVisibility(View.INVISIBLE) after sqView.img.setTag("null"); and Make Visible back the imageView in the else statment like 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);............} this is tricked only.



来源:https://stackoverflow.com/questions/29317434/list-view-changes-position-of-imageview-on-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!