使用listview

时间:2016-08-08 06:30:42

标签: android listview baseadapter android-glide

有一个列表视图,显示图像和文本。图像和文本正在列表视图中正确显示。但是当点击图像时,它应该以全屏模式显示在对话框中。图像显示在对话框中,但显示的图像错误。每当在列表视图中单击图像时,它总是显示错误的图像(列表底部的图像)。下面我提供了我使用的适配器。

  public class DescAdapter extends BaseAdapter implements Filterable {

    private final listdisplay ds;
    private ArrayList<descusers> dusers;
    private ArrayList<descusers> orig;
    private Activity listdisplay;
    PhotoViewAttacher p;
    String Limage,profile;



    public DescAdapter(listdisplay ds, ArrayList<descusers> dusers,Activity listdisplay) {
        this.ds = ds;
        this.dusers = dusers;
        this.listdisplay = listdisplay;

    }

    @Override
    public int getCount() {
        return dusers.size();
    }

    @Override
    public Object getItem(int position) {
        return dusers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        descusers du = dusers.get(position);
        String username = du.loginname;
        String descCrip=du.descCrip;
        Limage = du.image;
        long Ptime = du.Ptime;
        profile = du.profile;
        Viewholder viewholder = null;
        if(convertView==null) {
            viewholder = new Viewholder();
            convertView = LayoutInflater.from(ds).inflate(R.layout.customlist, null);
            viewholder.uname = (TextView) convertView.findViewById(R.id.username);
            viewholder.desc = (TextView) convertView.findViewById(R.id.description);
            viewholder.time = (TextView)convertView.findViewById(R.id.time);
            viewholder.iview = (ImageView) convertView.findViewById(R.id.imageList);
            viewholder.civ = (CircleImageView)convertView.findViewById(R.id.profile_image);
            viewholder.progress = (ProgressBar)convertView.findViewById(R.id.progress);

            Glide.with(convertView.getContext()).load(profile).centerCrop().diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true)
                    .into(viewholder.civ);
          convertView.setTag(viewholder);
        }else {

            viewholder = (Viewholder) convertView.getTag();

        }

        viewholder.uname.setText(username);
        viewholder.desc.setText(descCrip);

        if (!Patterns.WEB_URL.matcher(Limage).matches()) {
            viewholder.progress.setVisibility(View.GONE);
            viewholder.iview.setVisibility(View.GONE);
        } else {
            final Viewholder finalViewholder1 = viewholder;
            Glide.with(convertView.getContext()).load(Limage).centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL).listener(new RequestListener<String, GlideDrawable>() {
                           @Override
                           public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                               return false;
                           }

                           @Override
                           public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                           finalViewholder1.progress.setVisibility(View.GONE);
                               return false;
                           }
                       }).into(viewholder.iview);
            viewholder.iview.setVisibility(View.VISIBLE);
        }



        final View finalConvertView = convertView;

        viewholder.iview.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Dialog builder = new Dialog(listdisplay, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
                builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
                builder.getWindow().setBackgroundDrawable(
                        new ColorDrawable(Color.BLACK));
                 builder.setContentView(R.layout.imager);

                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialogInterface) {
                        //nothing;
                    }
                });


              ImageView image = (ImageView) builder.findViewById(R.id.image);
                Glide.with(finalConvertView.getContext()).load(Limage)
                         .diskCacheStrategy(DiskCacheStrategy.ALL).into(image);

              builder.show();
            }
        });


        return convertView;
    }

例如:

如果listview显示3张图片:

文本1

image1的

文本2

IMAGE2

文本3

图像3

如果我点击图像3,它会在对话框中正确显示。如果我点击图像2,则显示的图像3不是图像2。然后单击image1,然后在对话框中显示image2。请帮忙。

1 个答案:

答案 0 :(得分:0)

在bindView调用中,如果不向其中启动新加载,则必须清除()先前可能已加载的任何视图或目标。你有两个案例,你没有这样做。第一个是:

if (convertView == null) {
    ...
} else {
    // Clear or reload viewholder.civ
}

第二个是:

if (!Patterns.WEB_URL.matcher(Limage).matches()) {
    // Clear or reload viewholder.iview
} else {
    ...
}