Android 在ListView中动态添加视图(本例中为线性布局)时出现图形错误

Android 在ListView中动态添加视图(本例中为线性布局)时出现图形错误,android,android-layout,listview,android-arrayadapter,Android,Android Layout,Listview,Android Arrayadapter,根据主题的建议,我的目标是直接在ListView加载中动态添加一组视图(在我的例子中是LinearLayout) 这意味着所有的工作都必须在ArrayAdapter getView()方法中完成 以下是我现在的情况: 我想要什么: ===================== Text... content of the child's linear layout 1 content of the child's linear layout 2 content of

根据主题的建议,我的目标是直接在ListView加载中动态添加一组视图(在我的例子中是LinearLayout)

这意味着所有的工作都必须在ArrayAdapter getView()方法中完成

以下是我现在的情况:

我想要什么:

=====================
Text...    
    content of the child's linear layout 1
    content of the child's linear layout 2
    content of the child's linear layout 3
Text...
Text...
=====================
Text...
    content of the child's linear layout 1
Text...
Text...    
=====================
Text...
    content of the child's linear layout 1
    content of the child's linear layout 2
Text...
Text...    
=====================
=====================
Text...    
    content of the child's linear layout 1
    BLANK SPACE
    BLANK SPACE
Text...
Text...
=====================
Text...
    content of the child's linear layout 1
    BLANK SPACE
    BLANK SPACE
Text...
Text...    
=====================
Text...
    content of the child's linear layout 1
    BLANK SPACE
Text...
Text...    
=====================
我现在拥有的:

=====================
Text...    
    content of the child's linear layout 1
    content of the child's linear layout 2
    content of the child's linear layout 3
Text...
Text...
=====================
Text...
    content of the child's linear layout 1
Text...
Text...    
=====================
Text...
    content of the child's linear layout 1
    content of the child's linear layout 2
Text...
Text...    
=====================
=====================
Text...    
    content of the child's linear layout 1
    BLANK SPACE
    BLANK SPACE
Text...
Text...
=====================
Text...
    content of the child's linear layout 1
    BLANK SPACE
    BLANK SPACE
Text...
Text...    
=====================
Text...
    content of the child's linear layout 1
    BLANK SPACE
Text...
Text...    
=====================

正如您将要捕获的那样,当只有一个线性布局作为子元素添加时,甚至会添加空白空间。听起来很奇怪

以下是我正在使用的代码:

static class ViewHolder {
    protected TextView ADI;
    protected TextView date;
    protected TextView vehicleType;
    protected TextView vehiclePlate;
    protected TextView vehicleCountry;
    protected TextView location;
    protected TextView agents;
    protected LinearLayout infractionRootLayout;
    protected List<LinearLayout> infractionLayoutList;
}

public ArchivioViolazioneAdapter(Activity context,
        List<ViolazioneSynchroViolazioneTO> listOfViol) {
    super(context, R.layout.adapter_archivio_riga, 0, listOfViol);

    this.mContext = context;
    this.listOfViol = listOfViol;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViolazioneSynchroViolazioneTO currentViol = getItem(position);

    View rowView = convertView;
    if (rowView == null)
    {
        // Inflates the layout that will be added to root LL
        LayoutInflater inflater = mContext.getLayoutInflater();
        rowView = inflater.inflate(R.layout.adapter_archivio_riga, null);

        // Creates current viewholder
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.ADI = (TextView) rowView.findViewById(R.id.tv_viol_archivio_number);
        viewHolder.date = (TextView) rowView.findViewById(R.id.tv_viol_archivio_data);
        viewHolder.vehicleType = (TextView) rowView.findViewById(R.id.tv_viol_archivio_vehicle_type);
        viewHolder.vehiclePlate = (TextView) rowView.findViewById(R.id.tv_viol_archivio_vehicle_plate);
        viewHolder.vehicleCountry = (TextView) rowView.findViewById(R.id.tv_viol_archivio_vehicle_country);
        viewHolder.location = (TextView) rowView.findViewById(R.id.tv_viol_archivio_location);
        viewHolder.agents = (TextView) rowView.findViewById(R.id.tv_viol_archivio_agent);

        // Dynamic infraction creation
        final LayoutInflater factory = mContext.getLayoutInflater();
        viewHolder.infractionLayoutList = new ArrayList<LinearLayout>();
        viewHolder.infractionRootLayout = (LinearLayout) rowView.findViewById(R.id.ll_viol_archivio_infraction);

        for (ViolazioneSynchroInfrazioniTO infraction : currentViol.getInfrazioni()) {
            LinearLayout infractionView = (LinearLayout) factory.inflate(R.layout.adapter_archivio_infrazione, null);
            TextView article = (TextView) infractionView.findViewById(R.id.tv_viol_archivio_infraction_article);
            TextView comma = (TextView) infractionView.findViewById(R.id.tv_viol_archivio_infraction_comma);
            TextView description = (TextView) infractionView.findViewById(R.id.tv_viol_archivio_infraction_description);

            article.setText(infraction.getArticolo());
            comma.setText(infraction.getComma());
            description.setText(infraction.getDescrizioneInfrazione());
            description.setMaxLines(2);

                            // IMPORTANT STEP
            // Add to list so with rowView.getTag() the populated infraction list of layout will be automatically loaded
            viewHolder.infractionLayoutList.add(infractionView);

            // Add to root view
            viewHolder.infractionRootLayout.addView(infractionView);
        }

        rowView.setTag(viewHolder);
    }

    // Sets UI object
    ViewHolder holder = (ViewHolder) rowView.getTag();
    holder.ADI.setText(currentViol.getIdentificativoADI().toString());      
    holder.date.setText(DateUtils.formatToHumanDate(currentViol.getDataInizio()));
    holder.vehicleType.setText(currentViol.getVeicolo().getTipo());
    holder.vehiclePlate.setText(currentViol.getVeicolo().getTarga());
    holder.vehicleCountry.setText(currentViol.getVeicolo().getNazione());
    holder.location.setText(currentViol.getToponimo());     
    holder.agents.setText(TextUtils.join(", ", currentViol.getListaMatricoleAgenti()));


    return rowView;
}
静态类视图保持器{
受保护的文本视图ADI;
受保护的文本查看日期;
受保护的文本视图车辆类型;
受保护的文本视图车辆板;
受保护的TextView车辆国家/地区;
受保护的文本视图位置;
受保护的文本视图代理;
受保护的线性布局违反根布局;
受保护列表违反LayoutList;
}
公共档案馆(活动背景,
列表(VIOL){
super(context,R.layout.adapter\u archivio\u riga,0,listOfViol);
this.mContext=上下文;
this.listOfViol=listOfViol;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
ViolazioneSynchronizolazioneToCurrentVIOL=getItem(位置);
视图行视图=转换视图;
if(rowView==null)
{
//膨胀将添加到根目录的布局
LayoutInflater充气器=mContext.getLayoutInflater();
rowView=充气机。充气(R.layout.adapter\u archivio\u riga,null);
//创建当前的视图持有者
ViewHolder ViewHolder=新ViewHolder();
viewHolder.ADI=(TextView)rowView.findViewById(R.id.tv\u viol\u archivio\u编号);
viewHolder.date=(TextView)rowView.findViewById(R.id.tv\u viol\u archivio\u数据);
viewHolder.vehicleType=(TextView)rowView.findViewById(R.id.tv\u viol\u archivio\u vehicle\u type);
viewHolder.vehiclePlate=(TextView)rowView.findViewById(R.id.tv\u viol\u archivio\u vehicle\u plate);
viewHolder.vehicleCountry=(TextView)rowView.findViewById(R.id.tv\u viol\u archivio\u vehicle\u country);
viewHolder.location=(TextView)rowView.findViewById(R.id.tv\u viol\u archivio\u位置);
viewHolder.agents=(TextView)rowView.findViewById(R.id.tv\u viol\u archivio\u agent);
//动态违规创建
最终LayoutFlater工厂=mContext.getLayoutFlater();
viewHolder.infractionLayoutList=新建ArrayList();
viewHolder.infractionRootLayout=(LinearLayout)rowView.findViewById(R.id.ll\u viol\u archivio\u infraction);
for(ViolazioneSynchroInfrazioniTO违规:currentViol.getInfrazioni()){
LinearLayout infractionView=(LinearLayout)工厂。充气(R.layout.adapter\u archivio\u infrazione,null);
TextView article=(TextView)infractionView.findViewById(R.id.tv\u viol\u archivio\u infraction\u article);
TextView逗号=(TextView)infractionView.findViewById(R.id.tv\u viol\u archivio\u infraction\u逗号);
TextView description=(TextView)infractionView.findViewById(R.id.tv\u viol\u archivio\u infraction\u description);
article.setText(infraction.getArticolo());
comma.setText(infraction.getComma());
description.setText(infraction.getDescriptioneInfrazione());
说明.设置最大行数(2);
//重要步骤
//使用rowView.getTag()将填充的布局违规列表自动加载到列表so中
viewHolder.infractionLayoutList.add(infractionView);
//添加到根视图
viewHolder.infractionRootLayout.addView(infractionView);
}
rowView.setTag(viewHolder);
}
//设置UI对象
ViewHolder=(ViewHolder)rowView.getTag();
holder.ADI.setText(currentViol.getIdentificationVoadi().toString());
holder.date.setText(DateUtils.formatToHumanDate(currentViol.GetDataInizationIO());
holder.vehicleType.setText(currentViol.getVeicolo().getTipo());
holder.vehiclePlate.setText(currentViol.getVeicolo().getTarga());
holder.vehicleCountry.setText(currentViol.getVeicolo().getNazione());
holder.location.setText(currentViol.getToponimo());
setText(TextUtils.join(“,”,currentViol.getListaMatricoleAgenti());
返回行视图;
}
这是适配器布局中的线性布局(ll_viol\u archivio\u infraction),我将其用作动态添加视图的锚点:

<LinearLayout
    android:id="@+id/ll_viol_archivio_infraction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:contentDescription="@string/dummy_string"
        android:scaleType="fitCenter"
        android:src="@drawable/iv_violation"
        android:padding="2dp" />

</LinearLayout>

编辑: 不清楚为什么这些观点会有这种行为。按照我正确的逻辑,视图是在第一次创建行时创建的,然后通过rowView.setTag(viewHolder)存储在rowView本身中。因此,当必须再次加载行时,它将使用ViewHolder=(ViewHolder)rowView.getTag()检索已填充的动态并返回它

编辑2: 添加了完整的适配器布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/iv_viol_archivo_number"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:contentDescription="@string/dummy_string"
            android:scaleType="fitCenter"
            android:padding="2dp" />

        <TextView
            android:id="@+id/tv_viol_archivio_number_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/iv_viol_archivo_number"
            android:text="@string/viol_bollettari_2"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tv_viol_archivio_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tv_viol_archivio_number_label"
            android:layout_toLeftOf="@+id/tv_viol_archivio_data"
            android:text="@string/dummy_number"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_viol_archivio_data"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:layout_alignParentRight="true"
            android:text="@string/dummy_date"
            android:textSize="14sp" />

    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:contentDescription="@string/dummy_string"
            android:scaleType="fitCenter"
            android:src="@drawable/iv_car"
            android:padding="2dp" />

        <TextView
            android:id="@+id/tv_viol_archivio_vehicle_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/dummy_vehicle_type"
            android:textSize="15sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:contentDescription="@string/dummy_string"
            android:scaleType="fitCenter"
            android:padding="2dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/vehicle_plate_label"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tv_viol_archivio_vehicle_plate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/dummy_vehicle_plate"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minus_separator"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tv_viol_archivio_vehicle_country"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/dummy_vehicle_country"
            android:textSize="15sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_viol_archivio_infraction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:contentDescription="@string/dummy_string"
            android:scaleType="fitCenter"
            android:src="@drawable/iv_violation"
            android:padding="2dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:contentDescription="@string/dummy_string"
            android:scaleType="fitCenter"
            android:src="@drawable/iv_marker"
            android:padding="2dp" />

        <TextView
            android:id="@+id/tv_viol_archivio_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:text="@string/dummy_location"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:contentDescription="@string/dummy_string"
            android:scaleType="fitCenter"
            android:src="@drawable/iv_user"
            android:padding="2dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/viol_agent_list"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tv_viol_archivio_agent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:text="@string/dummy_agent"
            android:textSize="15sp"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

我自己解决了这个问题

解决方法很简单,只是输入错误

需要做的是将用于创建动态视图的根布局的方向设置为垂直,如下所示:

<LinearLayout
    android:id="@+id/ll_viol_archivio_infraction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:contentDescription="@string/dummy_string"
        android:scaleType="fitCenter"
        android:src="@drawable/iv_violation"
        android:padding="2dp" />

</LinearLayout>


因此,我给出的动态创建逻辑是正确的。这是为了鼓励人们将我的作品用于他们自己的项目。

你能发布你的
适配器\u archivio\u riga
布局吗?在这里。抱歉,我不确定是否可以在此处发布整个适配器代码。因此,当文本为空时,是否要隐藏
TextView
?否,我想显示我动态添加到循环中的每个线性布局(ViolazioneSynchronfRazio)