ListView第一项不可见/更新Android

时间:2016-02-14 02:54:13

标签: android listview android-adapter

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

这是我的代码: -

hand

列表视图的代码占据全高: -

 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;
        }

    }

1 个答案:

答案 0 :(得分: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();

    }