2011-07-08 115 views
3

我需要两个滚动视图中的listview,这显然是不可能与Android,但这是客户需要的。所以我试图以编程方式夸大我的列表。我的表格单元格没有显示出我目前的设置。如果我调整xml文件中的设置,我可以在顶部显示一行,但不显示其他行。我在这里做错了什么?Android创建列表以编程方式

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.alertsview); 

    inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    topRows = new ArrayList<View>(); 
    topLayout = (LinearLayout)findViewById(R.id.topListLayout); 
    bottomRows = new ArrayList<View>(); 
    bottomLayout = (LinearLayout)findViewById(R.id.bottomListLayout); 

    addRecip = (Button) findViewById(R.id.addRecipient); 
    addRecip.setOnClickListener(this); 

    if (SugarLoafContext.currentSite.alertRecipients.size() >= 5) 
     addRecip.setVisibility(View.GONE); 
    else 
     addRecip.setVisibility(View.VISIBLE); 


    EventBus.subscribe(ConfigureAlertsNotification.class, this); 
    EventBus.publish(new ViewAlertsNotification()); 
} 


public void setTopList() 
{ 
    topLayout.removeAllViews(); 
    topRows.clear(); 
    List<Camera> camList = new ArrayList<Camera>(SitesOrchestrator.getInstance().currentSite.cameraList); 

    for (int i = 0; i < camList.size(); i++) 
    { 
     String index = Integer.toString(i); 
     Camera cam = camList.get(i); 
     View row = inflater.inflate(R.layout.cameraalertrow, null); 
     TextView name = (TextView)row.findViewById(R.id.cameraNameAlerts); 
     CheckBox chk = (CheckBox)row.findViewById(R.id.camAlertChk); 
     name.setText(cam.name); 
     name.setTextColor(Color.GRAY); 
     chk.setOnCheckedChangeListener(this); 
     chk.setTag(index); 

     if (cam.isOnline) 
     { 
      chk.setEnabled(true); 

      if (cam.alertsEnabled && !chk.isChecked()) 
       chk.setChecked(true); 
      else if (cam.alertsEnabled == false && chk.isChecked()) 
       chk.setChecked(false); 
     } 
     else 
     { 
      chk.setChecked(cam.alertsEnabled); 
      chk.setEnabled(false); 
      name.setTextColor(Color.DKGRAY); 
     } 

     topRows.add(row); 
     topLayout.addView(row); 
    } 

} 

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:orientation="vertical" 
android:fillViewport="true" 
android:id="@+id/alertsTopScroll"> 
<LinearLayout 
android:layout_height="fill_parent" 
android:layout_width="fill_parent"> 
<TextView 
android:id="@+id/alertsTopText" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="#CCCCCC" 
android:text="@string/alerts_top_title" 
android:gravity="center" 
android:textColor="#000000" 
android:layout_centerVertical="true" 
android:layout_centerHorizontal="true"> 
</TextView> 
<LinearLayout 
android:id="@+id/topListLayout" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent"> 
</LinearLayout> 
<TextView 
android:id="@+id/alertsBottomText" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="#CCCCCC" 
android:text="@string/alerts_bottom_title" 
android:gravity="center" 
android:textColor="#000000" 
android:layout_centerVertical="true" 
android:layout_centerHorizontal="true"> 
</TextView> 
<LinearLayout 
android:id="@+id/bottomListLayout" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content"> 
</LinearLayout> 

<Button 
android:id="@+id/addRecipient" 
android:layout_height="50dp" 
android:layout_width="fill_parent" 
android:layout_centerHorizontal="true" 
android:text="@string/addRecipient"/> 
</LinearLayout> 
</ScrollView> 

(是的方法setTopList()被调用)

回答

1

“我在做什么错在这里?” 你把ListViews放在ScrollView中 - 只是不这样做。您应该在列表视图world of listview上观看Romain Guys google io会话。 (解释滚动视图中列表视图的42:40)

我不知道客户想要什么。但我怀疑他告诉你把ListViews放在ScrollView中。很可能有另一种解决方案来归档客户想要的内容。

列表视图包含多少项目?如果没有太多,可以在ScrollView中使用两个LinearLayouts。

请更具体了解布局应该如何。

+0

你能解释客户想要什么吗?他们想看什么? – Estel

+0

它会显示一个文本视图,然后是项目列表,然后是另一个文本视图,然后是另一个项目列表,然后是一个按钮。项目列表需要完全展开,整个屏幕可以滚动。无论如何,我绕过Android的限制并计算每个列表视图中行的高度,将每个列表视图扩展为该高度,并且它们在滚动视图中工作良好。 – spentak

+0

您是否设置了绝对高度,即px或dip?你确定textviews在所有设备上都会有高低不平的高度吗?在不同的屏幕分辨率下,会有不同数量的换行符,不同的设备上可能会使用不同高度的不同字体等等。 – thaussma

相关问题