listview自定义适配器重复项

时间:2016-11-04 13:17:53

标签: android listview android-adapter

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

当我在开始时将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);

    }
}

5 个答案:

答案 0 :(得分:0)

替换您的代码 从

<script type="text/javascript">

    window.addEventListener('load', function () {

        navigator.serviceWorker.register('sw.js');

        Notification.requestPermission(function(result) {
            if (Notification.permission !== result) {
                Notification.permission = result;
            }
          if (result === 'granted') {
            navigator.serviceWorker.ready.then(function(registration) {
                    var fecha = Date.now("Y-m-d");

                    registration.showNotification("Usted ha recibido una solicitud web! ", {
                        tag: 'soManyNotification',
                        body: 'Nombre: juanito \nMonto: $300 USD \nDestino: Miami',
                        icon: 'icon.png',
                        timestamp: fecha
                    });
            });
          }
        });
    });
    </script>

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

答案 1 :(得分:0)

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

  

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

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

答案 2 :(得分:0)

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

请更新您的专栏,

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

as

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

希望这会对你有所帮助

答案 3 :(得分:0)

问题是加载图像和视频时的逻辑问题,并且在获取视图中为什么工作,,,在图像加载时检查图像是否为空,所以当它返回一个视图进行回收时它返回一个带有回收材料的视图,当它检查图像时,如果它是空的,它会失败,因为它得到一个回收的视图,所以它在没有图像的项目中插入一个图像。

解决方案是重置所有具有检查条件的图像和内容以设置它,以便在检查失败时您没有获得回收的东西

答案 4 :(得分:0)

试试这个

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;
}

}