ListView first item not visible/updating Android

冷暖自知 提交于 2019-12-23 02:40:10

问题


I have 3 components in my layout ListView, EditText, Button. When the user enters text in the edittext and hits the Button, the app makes an Api call, my just added text is added to an existing list,and fetches the list of items. Now, my issue is that that the api call is successful and I get the list with my new item properly. But when I show it in the listview, the first item in the ListView is never visible. The text I add after the first text are visible properly, even if I have a list of 10 or more items, the first item never shows. Also, Iam showing the listview and other components inside a scrollview to take full length.

Here is my code:-

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

    }

Code for listview to take full height :-

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

回答1:


Try the below code may be its because of divider height. Have you defined custom height for divider in your listview?.

    /**
     * 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();

    }


来源:https://stackoverflow.com/questions/35387851/listview-first-item-not-visible-updating-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!