2016-01-12 45 views
0

我正在创建linearlayout与编程,然后我添加到它的意见。但addview函数只添加第一行的项目(第二个循环)。我该如何解决这个问题。我试图将LinearLayout.LayoutParams.WRAP_CONTENT更改为5000像素,但仍然只显示第一项。当我查看日志时,for循环运行良好。programmaticaly创建linearlayout不包装内容

getChildCount方法在for循环返回真值:

Crashlytics.log(Log.ASSERT, shoppingList.Title + "için: ", linearLayout.getChildCount() + ""); 

此外,我试图打电话for循环但是那仍然没有工作后invalidaterequestlayout方法。

for(ShoppingList shoppingList : shoppingLists){ 

     LinearLayout linearLayout = new LinearLayout(MainActivity.activity); 
     containerLL.addView(linearLayout); 

     LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     layoutParams.setMargins(14, 7, 14, 7); 
     linearLayout.setLayoutParams(layoutParams); 
     linearLayout.setBackgroundResource(R.drawable.rounded_gray); 



     for(int i = 0 ; i < shoppingList.shopLists.size() ; i ++){ 

      Crashlytics.log(Log.ASSERT, shoppingList.Title + "için: ", linearLayout.getChildCount() + ""); 
      ShopList shopList = shoppingList.shopLists.get(i); 

      View v = MainActivity.inflater.inflate(R.layout.row_list, linearLayout, false); 

      TextView  listTitle   = (TextView)  v.findViewById(R.id.listTitle); 
      TextView  brandTV   = (TextView)  v.findViewById(R.id.brandTV); 
      TextView  descriptionTV  = (TextView)  v.findViewById(R.id.descriptionTV); 
      TextView  sizeTV    = (TextView)  v.findViewById(R.id.sizeTV); 
      LinearLayout removeLL   = (LinearLayout) v.findViewById(R.id.removeLL); 
      FrameLayout  seperatorFL  = (FrameLayout)  v.findViewById(R.id.seperatorFL); 

      listTitle.setVisibility(i == 0 ? View.VISIBLE : View.GONE); 
      listTitle.setText(shoppingList.Title); 
      descriptionTV.setText(shopList.description); 
      brandTV.setText(shopList.brandName); 
      sizeTV.setText(shopList.description); 

      removeLL.setVisibility(i == shoppingList.shopLists.size() - 1 ? View.VISIBLE : View.GONE); 
      seperatorFL.setVisibility(i == shoppingList.shopLists.size() - 1 ? View.GONE : View.VISIBLE); 

      Crashlytics.log(Log.ASSERT, "width : " + v.getWidth() + " " + "height" + v.getHeight(), v.getX() + " X " + " " + v.getY() + " Y "); 
      linearLayout.addView(v); 

     } 
    } 

回答

1

尝试增加

linearLayout.setOrientation(LinearLayout.VERTICAL); 
+0

谢谢。这解决了这个问题。 –

0

尝试将containerLL.addView(linearLayout);行移至第二个for循环之后。

for(ShoppingList shoppingList : shoppingLists){ 

    LinearLayout linearLayout = new LinearLayout(MainActivity.activity); 
    . 
    . 
    . 
    for(.....){ 
     . 
     . 
     . 
    } 

    containerLL.addView(linearlayout); 
}