ListView控件的消息应用程序滚动显示错后的listItem布局 [英] ListView for messaging app shows wrong listItem layout after scrolling

查看:172
本文介绍了ListView控件的消息应用程序滚动显示错后的listItem布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多类似的问题已经被张贴在计算器,所以请不要以为我没有搜查高和低。我想我的问题,只是来源于现在已经完全理解和列表视图列表项的生命周期。我有一个可以包含两种类型的邮件,出站或入站的列表视图。本来,我的ListView会用这取决于消息的类型不同的背景颜色(出站入站VS),它完美地工作。现在我的应用程序不需要为列表项不同的背景,但它实际上需要不同的列表项不同的布局。

这是我的适配器的剪辑。

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){        视图V = convertView;        SoapBoxMessage thisMessage = messages.get(位置);        如果(V == NULL){
            LayoutInflater吹气=(LayoutInflater)的getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);            如果(thisMessage.isOutbound()){
                V = inflater.inflate(R.layout.outbound_row,NULL);            }其他{
                V = inflater.inflate(R.layout.inbound_row,NULL);            }
        }


解决方案

适配器可以支持将解决您的回收问题,不同的ViewItemTypes。

 静态公共枚举LAYOUT_TYPE {
    呼入,
    OUTBOUND
}@覆盖
公众诠释getViewTypeCount(){
    返回LAYOUT_TYPE.values​​()的长度。
}@覆盖
公众诠释getItemViewType(INT位置){
    如果(messages.get(位置).isOutbound())
        返回LAYOUT_TYPE.OUTBOUND.ordinal();
    其他
        返回LAYOUT_TYPE.INBOUND.ordinal();
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    LAYOUT_TYPE ITEMTYPE = LAYOUT_TYPE.values​​()[getItemViewType(位置);
    ...(code,直到气筒)
    开关(ITEMTYPE){
     案例入境:
          convertview = /膨胀和放大器;配置入站布局
          打破;
     案例出站:
          convertview = /膨胀和放大器;配置出站布局
          打破;
     }

您不必担心回收的观点,因为在ListView会尊重每个位置的ViewItemTypes,这只会在该位置提供正确viewtype的convertview

I know that many similar questions have been posted on stackoverflow, so please don't think I haven't searched high and low. I think my problems simply comes from now completely understanding listViews and the lifecycles of list items. I have a list view that can contain two types of messages, outbound or inbound. Originally, my listView would use a different background color depending on the type of message (outbound vs inbound), and it worked flawlessly. Now my application doesn't require a different background for list items, but it actually requires different layouts for different list items.

This is a clip of my Adapter.

public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        SoapBoxMessage thisMessage = messages.get(position);

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (thisMessage.isOutbound()) {
                v = inflater.inflate(R.layout.outbound_row, null);

            } else {
                v = inflater.inflate(R.layout.inbound_row, null);

            }
        }

解决方案

Adapters can support different ViewItemTypes that will solve your recycling problems.

static public enum LAYOUT_TYPE {
    INBOUND,
    OUTBOUND
}

@Override
public int getViewTypeCount () {
    return LAYOUT_TYPE.values().length;
}

@Override
public int getItemViewType (int position) {
    if ( messages.get(position).isOutbound())
        return LAYOUT_TYPE.OUTBOUND.ordinal();
    else
        return LAYOUT_TYPE.INBOUND.ordinal();
}

@Override
public View getView (int position, View convertView, ViewGroup parent) {
    LAYOUT_TYPE itemType = LAYOUT_TYPE.values()[getItemViewType(position)];
    ... (code until inflater )
    switch (itemType){
     case INBOUND:
          convertview = /inflate & configure inbound layout
          break;
     case OUTBOUND:
          convertview = /inflate & configure outbound layout
          break;
     }

you don't need to worry about recycling views because the listview will respect the ViewItemTypes for each position and it will only provide a convertview of the correct viewtype for that position

这篇关于ListView控件的消息应用程序滚动显示错后的listItem布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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