Android ListView布局滚动混乱 [英] Android ListView Layouts get mixed up on scroll

查看:181
本文介绍了Android ListView布局滚动混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义ArrayAdapter遇到一些问题.

I have some problems with my custom ArrayAdapter.

我有两种不同的布局,它们根据对象值应用于列表项.每次滚动某些项目时,应更改R.layout.layout1的应用位置的更改,请使用R.layout.layout2或其他方式.

I have two different layouts which I apply to the list item depending on an object value. Every time I scroll some of the items change where R.layout.layout1 should be applied, use R.layout.layout2 or the other way.

我想到了在XML文件中缓存布局的一些问题.

I thought of some problems on caching the layouts in the XML file.

我无法弄清楚为什么滚动更改布局的问题.

The Problem I can´t figure out why the layouts changes on scroll.

这是我的Java代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (this.items.get(position).getId() == this.id)
            convertView = layoutInflater.inflate(R.layout.layout1);
        else
            convertView = layoutInflater.inflate(R.layout.layout2);
        holder = new ViewHolder();
        holder.textView1 = (TextView) convertView.findViewById(R.id.textViewItem1);
        holder.textView2 = (TextView) convertView.findViewById(R.id.textViewItem2);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.textView1.setText(this.items.get(position).getInfo1());
    holder.textView2.setText(this.items.get(position).getInfo2());
    return convertView;
}

public static class ViewHolder {
    public TextView textView1;
    public TextView textView2;
}

这是ListView XML部分:

<ListView
        android:id="@+id/listViewMessages"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:stackFromBottom="true"
        android:transcriptMode="normal" />

推荐答案

您的convertView可能正在被重用,您必须检查null是否不同(如果它是正确的布局),然后重新填充正确的布局.

Your convertView is probably being reused, you have to check when it is different that null, if it is the correct layout and reinflate the correct one.

您可以将布局ID添加到ViewHolder中,并检查convertView是否不同于null,然后如果ID与所需的ID不同,则为正确的布局充气.

You can add the layout id to the ViewHolder and check if the convertView is different than null, then inflate the correct layout if the id is different than the one you want.

类似这样的东西:

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder = null;
    if(convertView == null || ((ViewHolder)convertView.getTag()).id != this.items.get(position).getId())
    {
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(this.items.get(position).getId() == this.id)
            convertView = layoutInflater.inflate(R.layout.layout1);
        else
            convertView = layoutInflater.inflate(R.layout.layout2);
        holder = new ViewHolder();
        holder.textView1 = (TextView) convertView.findViewById(R.id.textViewItem1);
        holder.textView2 = (TextView) convertView.findViewById(R.id.textViewItem2);
        holder.id = this.items.get(position).getId();
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.textView1.setText(this.items.get(position).getInfo1());
    holder.textView2.setText(this.items.get(position).getInfo2());
    return convertView;
}

public static class ViewHolder
{
    public int id; //Assuming your id is an int, if not, changed it to the correct type
    public TextView textView1;
    public TextView textView2;
}

这篇关于Android ListView布局滚动混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆