操作ListView项时的奇怪行为

时间:2014-01-26 20:20:47

标签: android listview

当用户点击'about'mageView时,我想花费'layout_quota_info'的高度,所以我为imageview设置了一个onclicklistener。

但是,当用户单击第1行中的about-button时,它也会影响其他行。当我需要向下滚动时,只有当列表长度超过屏幕时才会发生。

我想这个问题与ViewHolder的回收有关,但我不知道如何修复它...... 请帮帮....

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    Section section = list.get(position);

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater
                .inflate(R.layout.list_section_item, null, false);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.list_section_name);
        holder.room = (TextView) convertView.findViewById(R.id.list_section_room);
        holder.instructor = (TextView) convertView.findViewById(R.id.list_section_instructor);
        holder.datetime = (TextView) convertView.findViewById(R.id.list_section_datetime);
        holder.about = (ImageView) convertView.findViewById(R.id.list_section_about);
        holder.quota = (TextView) convertView.findViewById(R.id.list_section_quota);
        holder.avail = (TextView) convertView.findViewById(R.id.list_section_avail);
        holder.enrol = (TextView) convertView.findViewById(R.id.list_section_enrol);
        holder.wait = (TextView) convertView.findViewById(R.id.list_section_wait);
        holder.quota_info = (LinearLayout) convertView.findViewById(R.id.layout_quota_info);
        holder.layout_card = (LinearLayout) convertView.findViewById(R.id.layout_card);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.name.setText(section.name);
    holder.room.setText(section.room);
    holder.quota.setText("Quota\n" + section.quota);
    holder.avail.setText("Avail\n" + section.avail);
    holder.enrol.setText("Enrol\n" + section.enrol);
    holder.wait.setText("Wait\n" + section.wait);
    holder.instructor.setText(section.instructor);
    holder.datetime.setText(section.datetime);
    holder.quota_info.getLayoutParams().height = 1;
    holder.quota_info.requestLayout();
    holder.about.setOnClickListener(new OnSectionMoreClickListener(holder, section){

        @Override
        public void onClick(View v) {
            ViewHolder holder = (ViewHolder) getViewHolder();
            Section section = getSection();

                LinearLayout ll = holder.quota_info;
                ResizeAnimation anim = new ResizeAnimation(ll, ll.getWidth(), ll.getHeight(), ll.getWidth(), ll.getHeight() + 165f);
                anim.setDuration(300);
                //anim.setFillAfter(true);
                ll.startAnimation(anim);
                ll.setVisibility(View.VISIBLE);


        }

    });
    return convertView;
}

class ViewHolder {
    TextView name;
    TextView room;
    TextView instructor;
    TextView datetime;
    TextView avail;
    TextView enrol;
    TextView wait;
    TextView quota;
    ImageView about;
    LinearLayout layout_card;
    LinearLayout quota_info;
}

1 个答案:

答案 0 :(得分:0)

就像你说的那样。因为您已经扩展了一行,并且当您开始滚动新行时该行被回收/重用将显示已经消耗。

你应该做的是在你的else语句中,检查布局高度的大小,并在需要时减少它。

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

        if (holder.quota_info.getHeight() > ORIGINAL_SIZE) {
            // Minimize it. no need for animation
        }
    }