滚动时显示随机项目的getView

Posted

技术标签:

【中文标题】滚动时显示随机项目的getView【英文标题】:getView showing random items when scrolling 【发布时间】:2015-11-20 19:16:05 【问题描述】:

我的类 PlannerListAdapter 扩展了 BaseAdapter,我的 getView() 方法有 2 个问题。

1) 当我将适配器设置为列表视图时,一切正常,列表视图显示我列表中的所有元素,我向下滚动,我可以看到列表中的所有元素。但是当我向上滚动时,元素开始相互切换并且正在重复,而且它正在改变我的元素顺序,为什么会发生这种情况?

2) 在我的 BrandPlannersList 中,我有一个 String[] socialNetworks 对象。如果社交网络存在于 socialNetworks 中,我想更改社交网络(twitter、facebook、instagram)的默认图像。我认为我做得对,但它在不必要的时候改变了我在某些行中的一些图像。这是我的 getView 方法。

public View getView(int position, View convertView, ViewGroup parent) 
    // TODO Auto-generated method stub
    ViewHolder viewHolder;
    if (convertView == null) 
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.planificacion_item, null);
        viewHolder = new ViewHolder();
        viewHolder.txtDate = (TextView) convertView.findViewById(R.id.date);
        viewHolder.txtTime = (TextView) convertView.findViewById(R.id.time);
        viewHolder.txtContent = (TextView) convertView.findViewById(R.id.content);
        viewHolder.txtStatus = (TextView) convertView.findViewById(R.id.status);
        viewHolder.txtBrandName = (TextView) convertView.findViewById(R.id.brandNameTextView);
        viewHolder.imgInstagram = (ImageView) convertView.findViewById(R.id.instagramPlanImg);
        viewHolder.imgTwitter = (ImageView) convertView.findViewById(R.id.twitterPlanImg);
        viewHolder.imgFacebook = (ImageView) convertView.findViewById(R.id.facebookPlanImg);
        viewHolder.contentImg = (ImageView) convertView.findViewById(R.id.image);
        convertView.setTag(viewHolder);
     else 
        viewHolder = (ViewHolder)item.getTag();
    
    viewHolder = (ViewHolder) convertView.getTag();
    viewHolder.txtDate.setText(brandPlannersList.get(position).publicationDate);
    viewHolder.txtTime.setText(brandPlannersList.get(position).publicationTime);
    viewHolder.txtBrandName.setText(brandPlannersList.get(position).brandName);

        if (brandPlannersList.get(position).status.equalsIgnoreCase("0")) 
            viewHolder.txtStatus.setText(R.string.to_publish);
         else 
            viewHolder.txtStatus.setText(R.string.published);
        
        viewHolder.txtContent.setText(brandPlannersList.get(position).content);
        if (brandPlannersList.get(position).image.trim().equalsIgnoreCase("")) 
            viewHolder.contentImg.setVisibility(View.GONE);
         else 
            Log.d("Imagen", brandPlannersList.get(position).image + "");
            new ImageLoadTask(context.getResources().getString(R.string.short_endpoint) + brandPlannersList.get(position).image, viewHolder.contentImg).execute();
        
//This is where i'm trying to change the social networks image
        if (brandPlannersList.get(position).socialNetworks.length > 0) 
            for (int i = 0; i < brandPlannersList.get(position).socialNetworks.length; i++) 
                Log.d("PlannerListAdapter", "Array de redes sociales en planner en posicion: "+position +" "+ Arrays.toString(brandPlannersList.get(position).getSocialNetworks()));
                if (brandPlannersList.get(position).socialNetworks[i].equalsIgnoreCase("twitter")) 
                    viewHolder.imgTwitter.setImageDrawable(twDrawable);
                
                if (brandPlannersList.get(position).socialNetworks[i].equalsIgnoreCase("instagram")) 
                    viewHolder.imgInstagram.setImageDrawable(inDrawable);
                
                if (brandPlannersList.get(position).socialNetworks[i].equalsIgnoreCase("facebook")) 
                    viewHolder.imgFacebook.setImageDrawable(fbDrawable);
                
            
        

    return convertView;

这是我的 viewHolder 类

static class ViewHolder
    TextView txtDate;
    TextView txtTime;
    TextView txtContent;
    TextView txtStatus;
    TextView txtBrandName;
    ImageView imgInstagram;
    ImageView imgTwitter;
    ImageView imgFacebook;
    ImageView contentImg;

如果您想查看其余方法:

public PlannerListAdapter(List<BrandPlanners> brandPlannersList, Drawable twDrawable, Drawable inDrawable, Drawable fbDrawable, Context context) 
    super();
    this.brandPlannersList = brandPlannersList;
    this.twDrawable = twDrawable;
    this.inDrawable = inDrawable;
    this.fbDrawable = fbDrawable;
    this.context = context;


@Override
public int getCount() 
    // TODO Auto-generated method stub
    return brandPlannersList.size();


@Override
public Object getItem(int arg0) 
    // TODO Auto-generated method stub
    return null;


@Override
public long getItemId(int arg0) 
    // TODO Auto-generated method stub
    return 0;

P.D.抱歉,我的英语不是最好的。

【问题讨论】:

为什么让 getItem 为空? 感谢您的评论,我已经将其更改为返回 brandPlannersList.get(i);但这不是问题 【参考方案1】:

问题已解决,我在适配器中添加了这两种方法:

@Override
public int getViewTypeCount()                  
return getCount();


@Override
public int getItemViewType(int position) 
return position;

【讨论】:

以上是关于滚动时显示随机项目的getView的主要内容,如果未能解决你的问题,请参考以下文章

Android sqlite数据库-如何在滚动结束时显示列表视图添加 "加载更多项目"。

Android ListView 在向下滚动时显示不正确的内容,然后在向上滚动时显示正确的内容

Xamarin form-UWP - 在快速滚动列表时显示数据之前的黑色单元格

事件监听 & 页面滚动(页面滚动到某一位置时显示/隐藏某元素,Vue环境)

向下滚动Angular时显示导航栏

向下滚动时隐藏标题,向上滚动时显示