Android ListView Layouts在滚动时混淆

ora*_*e01 2 java android android-layout android-listview

我的习惯有些问题ArrayAdapter.

我有两种不同的布局,我根据对象值应用于列表项.每次我滚动一些项目改变R.layout.layout1应该应用的地方,使用R.layout.layout2或其他方式.

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

问题我无法弄清楚为什么布局会在滚动时发生变化.

这是我的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;
}
Run Code Online (Sandbox Code Playgroud)

这是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" />
Run Code Online (Sandbox Code Playgroud)

Mar*_*sta 5

您的convertView可能正在被重用,您必须检查它是否为null,如果它是正确的布局并重新填充正确的布局.

您可以将布局ID添加到ViewHolder并检查convertView是否不同于null,如果id与您想要的id不同,则膨胀正确的布局.

像这样的东西:

@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;
}
Run Code Online (Sandbox Code Playgroud)