优化ListView适配器以在各个ListView项目中显示动态内容

时间:2015-01-23 17:56:44

标签: android android-layout listview android-listview

我尝试显示ListView,其中各个列表项具有动态数量的元素。每个项目列表都有一个日期的TextView。在该日期之后,可以有2-5个元素。

  • ListItemA
    • DateTextView
    • 2-5 TextView
  • ListItemB
    • DateTextView
    • 2-5 TextView
  • ListItemC
    • DateTextView
    • 2-5 TextView

...

使用此当前方法,它会在ListView项目的底部显示空白TextView。会有更好的方法吗?

我认为convertView会有方法为其添加布局元素,但事实并非如此。

适配器的getView方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   ViewHolder holder;


    if(convertView == null){
        holder = new ViewHolder();
        convertView = this.inflater.inflate(R.layout.layout_list_item, parent, false);

        holder.text_date_cashout = (TextView) convertView.findViewById(R.id.text_date_cashout);

        holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout1));
        holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout2));
        holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout3));
        holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout4));
        holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout5));

        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }

    //get and set date from current cashOutHistory
    CashOutHistory coh = cashOutHistory.get(position);
    holder.text_date_cashout.setText(coh.getDate());

    //loop individual cashOuts to display their values
    int i = 0;
    for (CashOut co : coh.getCashOutHistory() ){
        holder.dynamic_views.get(i).setText(co.toString());
        i++;
    }

    return convertView;
}

private class ViewHolder{
    TextView text_date_cashout;
    ArrayList<TextView> dynamic_views = new ArrayList<TextView>(5);
}

列出项目布局:

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


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text_date_cashout" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text_cashout1" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text_cashout2" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text_cashout3" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text_cashout4" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text_cashout5" />

</LinearLayout>

编辑#1

循环显示并隐藏列表项中的TextView:

//loop individual cashOuts to display their values
int i = 0;
for (CashOut co : coh.getCashOutHistory() ){
    holder.dynamic_views.get(i).setText(co.toString());
    i++;
}

while(i < 5){
    holder.dynamic_views.get(i).setVisibility(View.GONE);
    i++;
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下方法隐藏您不需要为该特定项目显示的每个视图:

Boolean someCondition = true;

if (someCondition)
    holder.dynamic_views.get(i).setVisibility(View.GONE);

希望这有帮助。