滚动时有两个xml更改的Listview

时间:2014-08-20 10:30:33

标签: java android android-listview android-xml

目前,我正在处理我的应用程序的消息页面。问题是我在列表视图中使用了两个不同的xml。现在,我可以按照正确的顺序将数据显示在页面上,但是,当我滚动数据时,仍然以正确的顺序但以不同的样式显示。例如,在滚动之前,最后一条消息将使用messagesent.xml,如果我向上滚动,然后向下滚动,相同的消息现在具有message.xml样式。我目前使用的代码如下。

public class MessagingAdapter extends ArrayAdapter<MessagingObject> {


private static final String userID = "USERID";
private static final int TYPE_ITEM1 = 0;
private static final int TYPE_ITEM2 = 1;

public String newid;
public Context context;
public ArrayList<MessagingObject> object = new ArrayList<MessagingObject>();

int type;

public MessagingAdapter(Context context, int textViewResourceId, ArrayList<MessagingObject> objects) {
    super(context, textViewResourceId, objects);
    this.context = context;
    this.object = objects;
}

@Override
public int getItemViewType(int position) {

    if (userID.equals(newid)) {
        type = TYPE_ITEM1;
    } else {
        type = TYPE_ITEM2;
    }
    return type;
}

@Override
public int getViewTypeCount() {

    return 2;
}

@Override
public View getView(int position, View convertview, ViewGroup parent) {


    MessagingObject i = object.get(position);
    newid = i.getMessageID();

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertview = inflater.inflate(R.layout.messagesright_layout, null);

    if (convertview == null) {
        convertview = inflater.inflate(R.layout.messagesright_layout, null);
    }

    if (type == TYPE_ITEM1) {
       convertview = inflater.inflate(R.layout.messagesright_layout, null);
       TextView sentmessage = (TextView) convertview.findViewById(R.id.messagesent_text);
       sentmessage.setText(object.get(position).getMessage());

       TextView senttime = (TextView) convertview.findViewById(R.id.messagesentTime);
       String senttimestring = new String().valueOf(object.get(position).getTimeStamp());
       senttime.setText(senttimestring);

      } else {
       convertview = inflater.inflate(R.layout.messages_layout, null);
       TextView message = (TextView) convertview.findViewById(R.id.message_text);
       message.setText(object.get(position).getMessage());

       TextView time = (TextView) convertview.findViewById(R.id.messageTime);
       String timestring = new String().valueOf(object.get(position).getTimeStamp());
       time.setText(timestring);
     }
    return convertview;
}

现在我明白这与Listview Recycling有关,但是,我已经尝试了一些我在其他堆栈溢出帖子上找到的没有运气的解决方案,而且我还没能找到许多用2 xmls来解决这个问题。

谢谢你看看!

3 个答案:

答案 0 :(得分:2)

您的getView错了。只有当convertView为空时,您才应询问该位置的类型并使视图膨胀。

@Override
public View getView(int position, View convertview, ViewGroup parent) {


MessagingObject i = object.get(position);
newid = i.getMessageID();
int type = getItemViewType(position);

if (convertview == null) {
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (type == TYPE_ITEM1) {
       convertview = inflater.inflate(R.layout.messagesright_layout, null);
    } else if (type == TYPE_ITEM2) {
       convertview = inflater.inflate(R.layout.messages_layout, null);
    }
}

if (type == TYPE_ITEM1) {
   TextView sentmessage = (TextView) convertview.findViewById(R.id.messagesent_text);
   sentmessage.setText(object.get(position).getMessage());

   TextView senttime = (TextView) convertview.findViewById(R.id.messagesentTime);
   String senttimestring = new String().valueOf(object.get(position).getTimeStamp());
   senttime.setText(senttimestring);

  } else {

   TextView message = (TextView) convertview.findViewById(R.id.message_text);
   message.setText(object.get(position).getMessage());

   TextView time = (TextView) convertview.findViewById(R.id.messageTime);
   String timestring = new String().valueOf(object.get(position).getTimeStamp());
   time.setText(timestring);
 }
return convertview;
}

答案 1 :(得分:2)

我已经为列表中不同行的布局编制了一个包含2 xml的列表,我没有看到这个问题。

我没有将视图类型存储在局部变量中,而是总是调用&#34; getItemViewType&#34;在&#34; getView&#34;的开头所以我保证获得当前位置的正确视图类型,请参阅我对下面代码的调整(我没有检查过这个编译)


public class MessagingAdapter extends ArrayAdapter<MessagingObject> {


private static final String userID = "USERID";
private static final int TYPE_ITEM1 = 0;
private static final int TYPE_ITEM2 = 1;

public String newid;
public Context context;
public ArrayList<MessagingObject> object = new ArrayList<MessagingObject>();

int type;

public MessagingAdapter(Context context, int textViewResourceId, ArrayList<MessagingObject> objects) {
    super(context, textViewResourceId, objects);
    this.context = context;
    this.object = objects;
}

@Override
public int getItemViewType(int position) {

    if (userID.equals(newid)) {
        return = TYPE_ITEM1;
    } else {
        return = TYPE_ITEM2;
    }
}

@Override
public int getViewTypeCount() {

    return 2;
}

@Override
public View getView(int position, View convertview, ViewGroup parent) {


    MessagingObject i = object.get(position);
    newid = i.getMessageID();

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (getItemViewType(position) == TYPE_ITEM1) {
       convertview = inflater.inflate(R.layout.messagesright_layout, null);
       TextView sentmessage = (TextView) convertview.findViewById(R.id.messagesent_text);
       sentmessage.setText(object.get(position).getMessage());

       TextView senttime = (TextView) convertview.findViewById(R.id.messagesentTime);
       String senttimestring = new String().valueOf(object.get(position).getTimeStamp());
       senttime.setText(senttimestring);

      } else {
       convertview = inflater.inflate(R.layout.messages_layout, null);
       TextView message = (TextView) convertview.findViewById(R.id.message_text);
       message.setText(object.get(position).getMessage());

       TextView time = (TextView) convertview.findViewById(R.id.messageTime);
       String timestring = new String().valueOf(object.get(position).getTimeStamp());
       time.setText(timestring);
     }
    return convertview;

答案 2 :(得分:0)

try this:

if (convertview == null) {

if (type == TYPE_ITEM1) {
   convertview = inflater.inflate(R.layout.messagesright_layout, null);
   TextView sentmessage = (TextView) convertview.findViewById(R.id.messagesent_text);
   sentmessage.setText(object.get(position).getMessage());

   TextView senttime = (TextView) convertview.findViewById(R.id.messagesentTime);
   String senttimestring = new String().valueOf(object.get(position).getTimeStamp());
   senttime.setText(senttimestring);

  } else {
   convertview = inflater.inflate(R.layout.messages_layout, null);
   TextView message = (TextView) convertview.findViewById(R.id.message_text);
   message.setText(object.get(position).getMessage());

   TextView time = (TextView) convertview.findViewById(R.id.messageTime);
   String timestring = new String().valueOf(object.get(position).getTimeStamp());
   time.setText(timestring);
 }