欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

李代桃僵-列表里横竖展示图片动态调整高宽因复用引起的错位问题

程序员文章站 2022-05-26 11:11:56
...
 <RelativeLayout
            android:id="@+id/rl_pic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/view_top"
            android:layout_below="@id/item_taskstatus_title"
            >
            <FrameLayout
                android:id="@+id/fl_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone" />
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_pic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"
                />
        </RelativeLayout>
 //单图、多图显示

            final ArrayList<String> imageSourceList = new ArrayList<>();
            List<PictureBean> picture = taskStatusBeans.get(groupPosition).getSource_list();
            for (int index = 0; index < picture.size(); index++) {
                PictureBean pictureBean = picture.get(index);
                String url = CommonUtils.changeHeaderUrl(pictureBean.getPicid(), app.getTokenBean().getAccessToken(), "img",
                        String.valueOf((int) (app.getWidthPixels() * 0.36f)),
                        String.valueOf((int) (app.getWidthPixels() * 0.36f)));
                imageSourceList.add(url);
            }

            if (imageSourceList.size() > 1) {
                InfoPicAdapter personAdapter = new InfoPicAdapter(context,imageSourceList,imageLoaderUtil);
                    holder.rv_pic.setLayoutManager(new GridLayoutManager(context,3));
                holder.rv_pic.setAdapter(personAdapter);
                holder.rv_pic.setVisibility(View.VISIBLE);
                holder.fl_image.setVisibility(View.GONE);
            } else if(imageSourceList.size() > 0){
                holder.rv_pic.setVisibility(View.GONE);
                holder.fl_image.setVisibility(View.VISIBLE);
                PictureBean pictureBean = picture.get(0);
                if (TextUtils.isEmpty(pictureBean.getWidth()) || TextUtils.equals(pictureBean.getWidth(), "0") || TextUtils.isEmpty(pictureBean.getHeight()) || TextUtils.equals(pictureBean.getHeight(), "0")) {
                    //设置图片宽高
                    app.setMLayoutParam(holder.fl_image, 0.36f, 0.36f);
                } else {
                    int mw = Integer.parseInt(pictureBean.getWidth());
                    int mh = Integer.parseInt(pictureBean.getHeight());
                    int maxSize = (int) (app.getWidthPixels() * 0.36f);
                    if (mw > mh) {
                        //计算比例
                        BigDecimal proportion = new BigDecimal(maxSize).divide(new BigDecimal(mw), 3, BigDecimal.ROUND_HALF_UP);
                        //按照比例换算图片高度
                        float h = new BigDecimal(mh).multiply(proportion).divide(new BigDecimal(app.getWidthPixels()), 3, BigDecimal.ROUND_HALF_UP).floatValue();
                        //设置图片宽高
                        app.setMLayoutParam(holder.fl_image, 0.36f, h);
                    } else{
                        //计算比例
                        BigDecimal proportion = new BigDecimal(maxSize).divide(new BigDecimal(mh), 3, BigDecimal.ROUND_HALF_UP);
                        //按照比例换算图片高度
                        float w = new BigDecimal(mw).multiply(proportion).divide(new BigDecimal(app.getWidthPixels()), 3, BigDecimal.ROUND_HALF_UP).floatValue();
                        //设置图片宽高
                        app.setMLayoutParam(holder.fl_image, w, 0.36f);
                    }
                }
                ImageView iv = new ImageView(getMyActivity());
                iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                holder.fl_image.addView(iv);
                ImageLoader imageLoader1 = new ImageLoader.Builder()
                        .url(imageSourceList.get(0))
                        .imgView(iv)
                        .placeHolder(R.mipmap.ic_loading)
                        .errorHolder(R.mipmap.ic_load_fail)
                        .scaleType(1)
                        .build();
                imageLoaderUtil.loadImage(context, imageLoader1);
                holder.fl_image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, ZoomViewPagerActivity.class);
                        intent.putExtra("index", 0);
                        intent.putStringArrayListExtra("imageUrls", imageSourceList);
                        intent.putExtra("max", imageSourceList.size());
                        intent.putExtra("isShow", 1);
                        context.startActivity(intent);
                    }
                });
            }else {
                holder.rv_pic.setVisibility(View.GONE);
                holder.fl_image.setVisibility(View.GONE);
            }
复用布局,不复用iamgeview,用framelayout代替imageview的位置,动态添加imageview,这样就不会显示错乱了

相关标签: 复用