Android - 自定义列表视图在滚动时更改?

时间:2013-12-23 04:59:11

标签: android listview scroll

我的列表视图在滚动时不断变化。我正在尝试在“messages”变量中显示消息。我第一次滚动时一切都很好。但是当我再次滚动时,文本视图会相互重叠。我不确定是什么问题。

public class DisplayMessageAdapter extends ArrayAdapter<Message> {

    Context context;
    int resource;
    ArrayList<Message> messages = null;

    public DisplayMessageAdapter(Context context, int resource, ArrayList<Message> messages) {
        super(context, resource, messages);
        this.context = context;
        this.resource = resource;
        this.messages = messages; //list of messages to display
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        final MessagesHolder holder;


        if(row == null)
        {
            //Log.i("row-null","row-null");
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(resource, parent, false);

            holder = new MessagesHolder();
            holder.sent  = (TextView) row.findViewById(R.id.sent_message);
            holder.received = (TextView) row.findViewById(R.id.received_message);
            row.setTag(holder);
        }

        else
        {
            //Log.i("holder-not-null","holder-not-null");
            holder = (MessagesHolder) row.getTag();
        }

        Message message = messages.get(position);


        if(message.sent != null)
        {
            holder.sent.setText(message.sent);
            holder.received.setBackground(null);
        }
        else
        {
            holder.received.setText(message.received);
            holder.sent.setBackground(null);
        }

        return row;
    }

    static class MessagesHolder
    {
        TextView sent;
        TextView received;

    }

}

布局xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
      <TextView android:id="@+id/sent_message"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:textColor="#000000"
        android:background="@drawable/sent"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"

        android:textSize="15sp"
         />

      <TextView
          android:id="@+id/received_message"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/rcvd"
          android:layout_marginRight="20dp"
          android:textColor="#000000"
          android:layout_marginTop="4dp"
          android:layout_marginBottom="4dp"
          android:textSize="15sp"
           />

</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

尝试更改您的条件,如下所示设置TextView中的值。 在您的下面的代码中,所有if条件都相同。这没有任何区别。

  if(message.sent != null && message.received != null)
    {
        Log.i("adapter","both");
        holder.sent.setText(message.sent);
        holder.received.setText(message.received);
    }
    else if(message.sent != null)
    {
        holder.sent.setText(message.sent);
        //holder.received.setBackground(null);
    }
    else if(message.received != null)
    {
        holder.received.setText(message.received);
        //holder.sent.setBackground(null);
    }

将其更改如下:

  if(message.sent != null && message.received != null)
    {
        holder.sent.setText(message.sent);
        holder.received.setText(message.received);
    }
    else
    {
        holder.sent.setText("NA");
         holder.received.setText("NA");
    }

答案 1 :(得分:0)

Temproary Solution:

这个问题似乎存在于持有者中并且正在使用中。如果我不使用持有者类来提升性能,它现在有效。虽然我不确定如何正确使用支架。

答案 2 :(得分:0)

我有类似的问题。我的行中有一个进度条。我的问题是每次调用视图时我都没有重置进度条。您必须重置资产。