Android的 - 自定义列表视图的变化,而滚动? [英] Android - Custom listview changes while scrolling?

查看:104
本文介绍了Android的 - 自定义列表视图的变化,而滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的列表视图不断滚动时改变。我想在消息变量显示的消息。一切都很好,当我滚动第一次。但是,当我再次滚动时,textviews相互重叠。我不知道是什么问题。

 公共类DisplayMessageAdapter扩展ArrayAdapter<消息> {    上下文语境;
    INT资源;
    ArrayList的<消息>消息=无效;    公共DisplayMessageAdapter(上下文的背景下,INT资源的ArrayList<消息>消息){
        超级(上下文,资源,消息);
        this.context =背景;
        this.resource =资源;
        this.messages =消息; //消息列表中显示
    }    @燮pressLint(NewApi)
    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){        查看排= convertView;
        最后MessagesHolder持有人;
        如果(行== NULL)
        {
            //Log.i(\"row-null\",\"row-null);
            LayoutInflater吹气=(LayoutInflater)上下文
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            行= inflater.inflate(资源,家长,FALSE);            持有人=新MessagesHolder();
            holder.sent =(TextView中)row.findViewById(R.id.sent_message);
            holder.received =(TextView中)row.findViewById(R.id.received_message);
            row.setTag(保持器);
        }        其他
        {
            //Log.i(\"holder-not-null\",\"holder-not-null);
            支架=(MessagesHolder)row.getTag();
        }        消息消息= messages.get(位置);
        如果(message.sent!= NULL)
        {
            holder.sent.setText(message.sent);
            holder.received.setBackground(NU​​LL);
        }
        其他
        {
            holder.received.setText(message.received);
            holder.sent.setBackground(NU​​LL);
        }        返回行;
    }    静态类MessagesHolder
    {
        TextView的发送;
        TextView的好评;    }}

布局XML

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    >
      < TextView的机器人:ID =@ + ID / sent_message
        机器人:layout_alignParentRight =真
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =20dp
        机器人:文字颜色=#000000
        机器人:背景=@绘制/发送
        机器人:layout_marginTop =4DP
        机器人:layout_marginBottom =4DP        机器人:TEXTSIZE =15sp
         />      <的TextView
          机器人:ID =@ + ID / received_message
          机器人:layout_width =WRAP_CONTENT
          机器人:layout_height =WRAP_CONTENT
          机器人:背景=@绘制/ RCVD
          机器人:layout_marginRight =20dp
          机器人:文字颜色=#000000
          机器人:layout_marginTop =4DP
          机器人:layout_marginBottom =4DP
          机器人:TEXTSIZE =15sp
           />< / RelativeLayout的>


解决方案

我有一个类似的问题。我曾在我行的进度条。我的问题是,我没有重置进度条每次都获得视图被调用。必须重置以往的资产。

My listview keeps changing while scrolling. I'm trying to display the messages in "messages" variable. Everything is fine when i scroll first time. But when i scroll again, the textviews overlap each other. I'm not sure what the problem is.

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;

    }

}

Layout 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>

解决方案

I had a similar issue. I had a progress bar in my rows. My problem is that I wasn't resetting the progress bar every time get view was called. You must reset ever asset.

这篇关于Android的 - 自定义列表视图的变化,而滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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