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();
}
完美的解決方案。保存了我的一天。謝謝。 –