ListView第一项不可见/正在更新Android [英] ListView first item not visible/updating Android

查看:68
本文介绍了ListView第一项不可见/正在更新Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局ListView,EditText,Button中有3个组件. 当用户在edittext中输入文本并单击Button时,应用程序会进行Api调用,我刚刚添加的文本将添加到现有列表中,并获取项目列表.现在,我的问题是api调用成功,并且我正确获得了包含新项的列表.但是,当我在列表视图中显示它时,列表视图中的第一项从不可见.即使我有10个或更多项目的列表,我在第一个文本之后添加的文本也可以正确显示,但从不显示第一个项目.此外,Iam在滚动视图中显示了列表视图和其他组件,以显示完整长度.

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.

这是我的代码:-

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

推荐答案

尝试下面的代码可能是因为分隔线的高度.您是否在列表视图中定义了分隔线的自定义高度?

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

    }

这篇关于ListView第一项不可见/正在更新Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆