从ViewHolder中删除行

时间:2017-05-12 21:40:31

标签: java android sidebar android-viewholder

这里的总计多维数据集。我正在尝试从Viewholder中删除一行,该行被用作Android应用中的侧边栏菜单。

其中一个菜单项返回“EMPTY_LABEL”文本。我可以将文本设置为不可见但我宁愿完全删除该行。关于我如何做到这一点的任何想法?

以下代码 - 非常感谢您的帮助!

        ViewHolder holder;
        int type = getItemViewType(position);

        APMenuItem currentItem = mData.get(position);
        Integer rId = getResources().getIdentifier("sidebar_label_" + currentItem.getKey(), "string", APBaseInnerActivity.this.getPackageName());

        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
                case 1:
                    convertView = mInflater.inflate(R.layout.drawer_list_item_profile, null);
                    holder.textView = (TextView) convertView.findViewById(R.id.text1);
                    holder.textView.setText(APApplication.getUserInfo().getDisplayName());
                    holder.textView.setHeight(APDimensions.convertDpToPixel(60, APBaseInnerActivity.this));
                    holder.avatar = (DrawerRoundedImageView) convertView.findViewById(R.id.avatar);
                    holder.pendingImage = (ImageView) convertView.findViewById(R.id.profile_pending_image);
                    holder.pendingBackground = (ImageView) convertView.findViewById(R.id.profile_pending_background);

                    holder.pendingImage.setVisibility(View.GONE);
                    holder.pendingBackground.setVisibility(View.GONE);

                    if( APApplication.getUserInfo().getAvatarUrl() != null ) {
                        holder.avatar.setImageUrl(APApplication.getUserInfo().getAvatarUrl());

                        if ( !APApplication.getUserInfo().getIsAvatarApproved() )
                        {
                            holder.pendingImage.setVisibility(View.VISIBLE);
                            holder.pendingBackground.setVisibility(View.VISIBLE);
                        }
                    }
                    else {
                        holder.avatar.setImageResource(R.drawable.rounded_rectangle_2_copy_2);
                    }

                    break;
                case 2:
                    convertView = mInflater.inflate(R.layout.drawer_list_item, null);
                    break;
                case 3:
                    convertView = mInflater.inflate(R.layout.drawer_list_item_bottom, null);
                    holder.textView = (TextView) convertView.findViewById(R.id.text1);
                    if( rId != null ){
                        holder.textView.setText(getResources().getString(rId));
                    }
                    else{
                        holder.textView.setText("EMPTY_LABEL");

                    }
                    holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                    holder.textView.setHeight(APDimensions.convertDpToPixel(40, APBaseInnerActivity.this));
                    break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if( type == 2 ){
            holder.textView = (TextView) convertView.findViewById(R.id.text1);

            if( rId != null && rId != 0){
                holder.textView.setText(getResources().getString(rId));
            }
            else{
                holder.textView.setText("EMPTY_LABEL");
            }

            holder.circleView = (TextView) convertView.findViewById(R.id.counter);
            holder.textView.setHeight(APDimensions.convertDpToPixel(50, APBaseInnerActivity.this));

            if (currentItem.getCount() > 0) {
                Integer cnt = currentItem.getCount();
                holder.circleView.setText(cnt.toString());
                holder.circleView.setVisibility(View.VISIBLE);
            } else {
                holder.circleView.setVisibility(View.INVISIBLE);
            }
        }

        return convertView;
    }
}

public static class ViewHolder {
    public TextView textView;
    public TextView circleView;
    public DrawerRoundedImageView avatar;
    public ImageView pendingImage;
    public ImageView pendingBackground;
}

}

1 个答案:

答案 0 :(得分:0)

那么您是否在询问如何删除显示空值的TextView?

我认为您误解了ViewHolder的概念。 ViewHolders用于存储视图引用,以避免每次出现新项时调用findViewById()

重用适配器视图中的视图。这也就是ViewHolders。如果从一个文本中删除TextView,则将在其他重用相同视图和视图的文档中删除它。

只需以getView()编程隐藏/显示它。这是一种常见的做法,没有任何问题。如果存在的差异多于此视图,请创建单独的布局和另一种视图类型。

编辑。

在您的情况下,隐藏和显示TextView将如下所示。

if( rId != null ) {
    holder.textView.setVisibility(View.VISIBLE);
    holder.textView.setText(getResources().getString(rId));
} else {
    holder.textView.setVisibility(View.GONE);
}