2016-02-14 1297 views
1

我在我的布局ListView,EditText,Button中有3个组件。 当用户在edittext中输入文本并点击按钮时,应用程序会进行一次Api调用,我刚刚添加的文本将添加到现有列表中,并获取项目列表。现在,我的问题是,API调用是成功的,我正确地使用我的新项目获得列表。但是当我在列表视图中显示它时,ListView中的第一项是不可见的。我在第一个文本之后添加的文本可以正常显示,即使我有一个包含10个或更多项目的列表,第一个项目也不会显示。另外,Iam在scrollview中显示listview和其他组件以获取全长。ListView第一项不可见/更新Android

这里是我的代码: -

public class ItemListAdapter : BaseAdapter 
    { 
     public ItemListAdapter(Activities.Detail detail, List<Models.ListOfItems> itemList) 
     { 
      // TODO: Complete member initialization 
      this.detail = detail; 
      this.itemList = itemList; 
      inflater = detail.LayoutInflater; 
     } 
     public override int Count 
     { 
      get { return itemList.Count; } 
     } 

     public override Java.Lang.Object GetItem(int position) 
     { 
      return position; 
     } 

     public override long GetItemId(int position) 
     { 
      return position; 
     } 

     public override View GetView(int position, View convertView, ViewGroup parent) 
     { 
      if (convertView == null) 
      { 
       convertView = inflater.Inflate(Resource.Layout.item_row_layout, null); 

      } 
      txtAuthorName = convertView.FindViewById<TextView>(Resource.Id.txtCommentAuthor); 
      txtItemName = convertView.FindViewById<TextView>(Resource.Id.txtTime); 
      txtItemDate = convertView.FindViewById<TextView>(Resource.Id.txtItemDate); 

      var mData = itemList.ElementAt(position); 
      txtAuthorName.Text = mData.AuthorDisplayName; 
      txtItemName.Text = DateTime.Parse(mData.DateUpdated).ToString("dd MMMM yyyy", CultureInfo.InvariantCulture); 
      txtItemDate.Text = mData.Text; 
      return convertView; 
     } 

    } 

代码列表视图采取全高: -

public static void SetListViewHeightBasedOnChildren(ListView listView) 
     { 
      IListAdapter listAdapter = listView.Adapter; 
      if (listAdapter == null) 
       return; 

      int desiredWidth = Android.Views.View.MeasureSpec.MakeMeasureSpec(listView.Width, MeasureSpecMode.Unspecified); 
      int totalHeight = 0; 
      View view = null; 
      for (int i = 0; i < listAdapter.Count; i++) 
      { 
       view = listAdapter.GetView(i, view, listView); 
       if (i == 0) 
        view.LayoutParameters = new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WrapContent); 


       totalHeight += view.MeasuredHeight; 
       view.Measure(desiredWidth, totalHeight); 
      } 
      ViewGroup.LayoutParams param = listView.LayoutParameters; 
      param.Height = totalHeight + (listView.DividerHeight * (listAdapter.Count - 1)); 
      listView.LayoutParameters = param; 
      listView.RequestLayout(); 
     } 

回答

2

尝试下面的代码,可能是因为其分压器的高度。你有没有在你的列表视图中定义分隔符的自定义高度?

/** 
    * Sets ListView height dynamically based on the height of the items. 
    * 
    * @param listView to be resized 
    * 
    */ 

    public static void setListViewHeightBasedOnChildren(ListView listView) { 
     ListAdapter listAdapter = listView.getAdapter(); 
     if (listAdapter == null) { 
      // pre-condition 
      return; 
     } 

     int totalHeight = 0; 
     float singleViewHeight = 0; 
     for (int i = 0; i < listAdapter.getCount(); i++) { 
      View listItem = listAdapter.getView(i, null, listView); 
      listItem.measure(0, 0); 
      totalHeight += listItem.getMeasuredHeight(); 
      singleViewHeight = listItem.getMeasuredHeight(); 
     } 

     // Get total height of all item dividers. 
     int totalDividersHeight = listView.getDividerHeight() * 
       (listAdapter.getCount() - 1); 

     // Set list height 
     ViewGroup.LayoutParams params = listView.getLayoutParams(); 
     params.height = totalHeight + totalDividersHeight + (Math.round(singleViewHeight/2)); 
     listView.setLayoutParams(params); 
     listView.requestLayout(); 

    } 
+0

完美的解决方案。保存了我的一天。谢谢。 –