Android 向上滚动后列表滚动项目在顶部不可见,但我不需要那个

Android list scroll item invisible on the top after scrolling on upside but I don't need that

我使用的列表视图在一行中有两个项目,还有一个通过 addHeaderView() 添加的 header 项目。

最初我使用的是包含 7 个项目的虚拟数据,因此列表视图的最后一项需要不可见。

第一次我的数据添加成功。但是在向上滚动后,我第一行的第二个项目变得不可见,但我不希望那样。

这是我的适配器代码:

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

    ViewHolder holder = null;

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.home_grid_item, parent,
                false);

        holder = new ViewHolder();
        holder.imgPerformer = (CircularImageView) convertView
                .findViewById(R.id.imgPerformer);
        holder.listItemTextView = (TextView) convertView
                .findViewById(R.id.listItemTextView);
        holder.imgPerformer1 = (CircularImageView) convertView
                .findViewById(R.id.imgPerformer1);
        holder.listItemTextView1 = (TextView) convertView
                .findViewById(R.id.listItemTextView1);
        holder.performerFirstLi = (LinearLayout) convertView
                .findViewById(R.id.performerFirstLi);
        holder.performerSecondLi = (LinearLayout) convertView
                .findViewById(R.id.performerSecondLi);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    index = position * 2;
    holder.imgPerformer.setImageResource(performerItems.get(index)
            .getIcon());
    holder.listItemTextView.setText(performerItems.get(index).getName());
    if (performerItems.size() > index + 1) {
        holder.imgPerformer1.setImageResource(performerItems.get(index + 1)
                .getIcon());
        holder.listItemTextView1.setText(performerItems.get(index + 1)
                .getName());
    } else {
        holder.performerSecondLi.setVisibility(View.INVISIBLE);
    }

    holder.imgPerformer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    holder.imgPerformer1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    return convertView;
}

private static class ViewHolder {
    public CircularImageView imgPerformer;
    public TextView listItemTextView;
    public CircularImageView imgPerformer1;
    public TextView listItemTextView1;
    public LinearLayout performerFirstLi;
    public LinearLayout performerSecondLi;
}

需要一些帮助。

看来你没有正确考虑视图被重用的事实,如果传递给 getView()convertView 的值不为 null,则意味着视图正在被重用,所以你必须重置它的状态。所以,正如你有时所说的

holder.performerSecondLi.setVisibility(View.INVISIBLE);

视图在重复使用时仍然不可见。

因此,要解决此问题,您需要通过调用

确保将其设置为可见
holder.performerSecondLi.setVisibility(View.VISIBLE);

在接下来的区块中

if (performerItems.size() > index + 1)