在 gridview 中滚动后,Android textview 不显示。

标签 android gridview adapter

我创建了一个 gridview 来显示带有图像和一些文本的类别:

 <GridView
            android:id="@+id/gridViewCategories"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#c7c9bf"
            android:fadingEdge="none"
            android:horizontalSpacing="1dp"
            android:listSelector="@null"
            android:numColumns="3"
            android:scrollbars="none"
            android:verticalSpacing="1dp" >

 </GridView>

这是我适配器中 getView 的代码

public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context)
                .getLayoutInflater();
        convertView = inflater.inflate(R.layout.poi_category_item, parent,
                false);
        holder = new ViewHolder();
        holder.imageView = (ImageView) convertView.findViewById(R.id.imageViewIcon);
        holder.textView = (TextView) convertView.findViewById(R.id.textViewName);
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    Category category = list.get(position);

    if (category != null) {

        if (Build.VERSION.SDK_INT < 11) {
            convertView.setBackgroundColor(Color.parseColor("#f8f8f3"));
        }

        holder.textView.setLines(2);
        holder.imageView.setImageResource(category.getResourceId());

        if (category.getId() > 0) {
            holder.imageView.setVisibility(View.VISIBLE);
        } else {
            holder.imageView.setVisibility(View.INVISIBLE);
            holder.textView.setVisibility(View.INVISIBLE);
        }
        holder.textView.setText(category.getName());
    }
    return convertView;
}

当我上下滚动时,一些属于 TextView 的文本随机消失。虽然没有错误。我不知道发生了什么。我的代码有问题吗?

最佳答案

你需要在 if(category.getId()>0) block 中添加这个

holder.textView.setVisibility(View.VISIBLE);

关于在 gridview 中滚动后,Android textview 不显示。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16959988/

相关文章:

android - 在android中制作像You-Tube一样的自定义抽屉导航

java - 如何实现在深色背景上运行的进度条?

android - findViewByID 突然返回 null

android - CheckBox inside a View inside 一个 ListView

android - 极其滞后的 RecyclerView 性能

android - 在 Android 中将位图图像存储到 SD 卡

android - GridView ImageView 在不同屏幕下的大小

asp.net - 如何防止在 gridview 中重复执行操作

c# - 将变量读入 GridView asp.net 4 c# 中 TemplateField 中的 NavigateUrl

android - 合并多个 RecyclerView.Adapter 以用于单个 RecyclerView (Android)