【问题标题】:listview custom adapter duplicate itemslistview 自定义适配器重复项
【发布时间】:2016-11-04 13:17:53
【问题描述】:

为什么这会返回一些重复的项目?

当我在开始时将 convertView 设置为 null 时,它可以工作,但我失去了平滑滚动

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

    ViewHoilder viewhoilder = null;
    if(convertView == null){
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.post_unit, null, true);
        viewhoilder = new ViewHoilder(convertView);
        convertView.setTag(viewhoilder);
    }else{
        viewhoilder = (ViewHoilder) convertView.getTag();
    }



    final Post_Unit post_unit = getItem(position);
    if (post_unit.getPost_unit_type() == 10) {



        Load_text(post_unit,viewhoilder);

        Load_images(post_unit, viewhoilder);

        Load_videos(post_unit, viewhoilder);




    return convertView;
}

这里是我的 ViewHolder

 public class  ViewHoilder{
    TextView post_username,post_date,post_content,video_title;
    LinearLayout video_layout;
    FrameLayout post_unit_btn_add_like;
    ImageView video_thumbnail,img_post_unit_content_midea_img_imageview;
    ScrollView post_unit_content_midea_imgs;
    LinearLayout post_unit_content_midea_imgs_layout;
    public ViewHoilder(View v) {
        this.post_username = (TextView)v.findViewById(R.id.post_unit_username);
        this.post_date = (TextView)v.findViewById(R.id.post_unit_date);
        this.post_content = (TextView)v.findViewById(R.id.post_unit_content_text);
        this.video_layout = (LinearLayout) v.findViewById(R.id.post_unit_content_midea_video);
        this.video_title = (TextView) v.findViewById(R.id.post_unit_video_title);
        this.video_thumbnail = (ImageView) v.findViewById(R.id.post_unit_video_logo);
        this.img_post_unit_content_midea_img_imageview = (ImageView) v.findViewById(R.id.post_unit_content_midea_img_imageview);
        this.post_unit_btn_add_like = (FrameLayout) v.findViewById(R.id.post_unit_content_midea_img);
        this.post_unit_content_midea_imgs = (ScrollView) v.findViewById(R.id.post_unit_content_midea_imgs);
        this.post_unit_content_midea_imgs_layout = (LinearLayout) v.findViewById(R.id.post_unit_content_midea_imgs_layout);

    }
}

【问题讨论】:

    标签: android listview android-adapter


    【解决方案1】:

    替换您的代码 来自

    layoutInflater.inflate(R.layout.post_unit, null, true);
    

    layoutInflater.inflate(R.layout.post_unit, parent, false);
    

    【讨论】:

    • 然后检查你的数组列表
    【解决方案2】:

    在适配器中添加 onDetachedFromWindow() @Override 方法并添加 holder.setIsRecyclable(false); 示例:

    @Override onDetachedFromWindow(){
    holder.setIsRecyclable(false);
    }

    注意:listView 已弃用,因此请使用 recyclerview 视图 https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

    【讨论】:

    • ArrayAdapter 中没有这样的@method ??
    • 参考此文档:link
    【解决方案3】:

    根据,作为生成层次结构的父级的可选视图(如果 attachToRoot 为真),或者只是为返回的层次结构的根提供一组 LayoutParams 值的对象(如果 attachToRoot 为假。)

    请更新你的线路,

    convertView = layoutInflater.inflate(R.layout.post_unit, null, true);

    作为

    convertView = layoutInflater.inflate(R.layout.post_unit, null, false);

    希望对你有帮助

    【讨论】:

      【解决方案4】:

      问题是否是加载图像和视频以及在获取视图中为什么工作的逻辑问题,,在图像加载中检查图像是否为空,因此当它返回视图以进行回收时返回一个带有回收的东西的视图,当它检查图像是否为空时,它会失败,因为它获得了一个回收的视图,因此它在没有图像的项目中插入了一个图像。

      解决方案是重置所有具有检查条件的图像和东西来设置它,这样当检查失败时你就不会得到回收的东西

      【讨论】:

        【解决方案5】:

        试试这个

        public View getView(int position, View convertView, ViewGroup parent) {
        
        if (null == convertView) {
            LinearLayout view = (LinearLayout) LinearLayout.inflate(this.context, 
                R.layout.message, null);
            Log.d("SeenDroid", String.format("Get view %d", position));
            TextView title = new TextView(view.getContext());
            title.setText(this.items.get(position).getTitle());
            view.addView(title);
            return view;
        } else {
            LinearLayout view = (LinearLayout) convertView;
            TextView title = (TextView) view.getChildAt(0);
            title.setText(this.items.get(position).getTitle());
            return convertView;
        }
        

        }

        【讨论】:

        • 我解决了这个问题,检查我上面的答案,谢谢
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-05
        • 2014-05-20
        • 2014-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多