滚动视图时,Imageview会消失

时间:2014-09-22 20:04:38

标签: android listview android-imageview

我真的很疯狂 - 好了它的列表视图 - 两个图像视图 - 左边所有listview-items必须每个都有一个图像 - 左边全部都是填充的。但是 - 在右边我只想用imageview填充3个listitems。而且我已经了解到,由于回收利用,其他的(我不想填充)必须设置为隐形。发生了什么 - 启动应用程序时 - 首先看到右侧的3个图像视图 - 但是,当它们滚出视图然后返回时 - 它们都消失了。

非常感谢帮助

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

    ViewHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.main, parent, false);
        holder = new ViewHolder();
        holder.textView = (TextView) convertView.findViewById(R.id.label);
        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
        holder.infoView = (ImageView) convertView.findViewById(R.id.image_icon);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.textView.setText(values[position]);

    switch (position) {
        case 0:
            holder.imageView.setImageResource(R.drawable.conv);
            holder.infoView.setVisibility(View.GONE);
            break;
        case 1:
            holder.imageView.setImageResource(R.drawable.counting);
            holder.infoView.setVisibility(View.GONE);
            break;
        case 2:
            holder.imageView.setImageResource(R.drawable.travelling);
            holder.infoView.setVisibility(View.GONE);
            break;
        case 3:
            holder.imageView.setImageResource(R.drawable.dating);
            holder.infoView.setVisibility(View.GONE);
            break;
        case 4:
            holder.imageView.setImageResource(R.drawable.restaurant);
            holder.infoView.setVisibility(View.GONE);
            break;
        case 5:
            holder.imageView.setImageResource(R.drawable.thaidishes);
            holder.infoView.setVisibility(View.GONE);
            break;  
        case 6:
            holder.imageView.setImageResource(R.drawable.time_);
            holder.infoView.setImageResource(R.drawable.imageicon);
            break;
        case 7:
            holder.imageView.setImageResource(R.drawable.time2);
            holder.infoView.setVisibility(View.GONE);
            break;  
        case 8:
            holder.imageView.setImageResource(R.drawable.colours);
            holder.infoView.setVisibility(View.GONE);
            break;
        case 9:
            holder.imageView.setImageResource(R.drawable.weather);
            holder.infoView.setVisibility(View.GONE);
            break;
        case 10:
            holder.imageView.setImageResource(R.drawable.directions);
            holder.infoView.setVisibility(View.GONE);
            break;
        case 11:
            holder.imageView.setImageResource(R.drawable.emergency);
            holder.infoView.setVisibility(View.GONE);
            break;
        case 12:
            holder.imageView.setImageResource(R.drawable.zoo);
            holder.infoView.setVisibility(View.GONE);
            break;
        case 13:
            holder.imageView.setImageResource(R.drawable.shopping);
            holder.infoView.setVisibility(View.GONE);
            break;  
        case 14:
            holder.imageView.setImageResource(R.drawable.bank);
            holder.infoView.setVisibility(View.GONE);
            break;
        case 15:
            holder.imageView.setImageResource(R.drawable.hotel);
            holder.infoView.setVisibility(View.GONE);
            break;
        case 16:
            holder.imageView.setImageResource(R.drawable.countries);
            holder.infoView.setVisibility(View.GONE);
            break;
        case 17:
            holder.imageView.setImageResource(R.drawable.cities);
            holder.infoView.setImageResource(R.drawable.imageicon);
            break;
        case 18:
            holder.imageView.setImageResource(R.drawable.features);
            holder.infoView.setImageResource(R.drawable.imageicon);
            break;
    }

    return convertView;
}

2 个答案:

答案 0 :(得分:2)

我不能强调18个案例的开关风格很差,很难处理,而且这个方法太长了。您应该重新考虑代码的构造方式 - 将其分解为更多的方法或类。

至于你的问题,你的观点正在被回收,所以一旦你设置它们就消失了 - 它们就会消失。

你只是错过了第6,17和18例中的holder.infoView.setVisibility(View.VISIBLE);行。

答案 1 :(得分:1)

您在回收时将ImageView的可见性设置为GONE,但是对于具有第二张图像的元素,不会将其重置为VISIBLE。将此添加到第二个图像应显示的情况:

holder.infoView.setVisibility(View.VISIBLE);