ListView内容仅在点击/点按后显示

时间:2017-02-06 12:07:54

标签: android listview android-fragments xamarin

我在片段上使用ListView并结合SwipeRefreshLayout

加载片段后,ListView保持为空,直到我点击(在模拟器中)或点击屏幕(真实设备)。在这种情况下不会触发刷新处理程序,但会显示内容。

如果我改变片段的加载顺序,当我切换到包含ListView的片段时,内容是可见的。

片段的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/DeviceOverviewRefresher"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:id="@+id/DeviceOverviewList"
            android:minWidth="25px"
            android:minHeight="25px"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/DeviceOverviewActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="15dip"
    app:backgroundTint="@color/DarkAccent"
    android:tint="@android:color/white"
    android:src="@android:drawable/ic_input_add" />
</android.support.design.widget.CoordinatorLayout>

C#片段代码

public class DeviceOverviewFragment : Fragment
{
    private SwipeRefreshLayout _refresher;
    private List<Device> _devices;
    private DeviceOverviewAdapter _adapter;
    private ListView _listView;
    private FloatingActionButton _fab;
    private AppPreferences _appPreferences;
    private View view;

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            _appPreferences = new AppPreferences(Context);
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            view = inflater.Inflate(Resource.Layout.DeviceOverviewFragment, container, false);

            SetupView();

            return view;
        }

        void SetupView()
        {
            _devices = new List<Device>();

            _adapter = new DeviceOverviewAdapter(this.Activity, _devices);

            _listView = view.FindViewById<ListView>(Resource.Id.DeviceOverviewList);
            _listView.Adapter = _adapter;
            _listView.ItemClick += _listView_ItemClick;

            #region map the swipe-to-refresh action

            _refresher = view.FindViewById<SwipeRefreshLayout>(Resource.Id.DeviceOverviewRefresher);

            _refresher.SetColorSchemeColors(
                    new Android.Graphics.Color(0xFF, 0x00, 0x00),
                    new Android.Graphics.Color(0xD0, 0xD0, 0xD0)
                );

            _refresher.Refresh += HandleRefresh;
            #endregion

            #region floating action button
            _fab = view.FindViewById<FloatingActionButton>    (Resource.Id.DeviceOverviewActionButton);

            _fab.Click += _fab_Click;
            #endregion

            var t = Task.Run(async () =>
            {
                await RefreshDeviceList();
            });


        }

        private List<Device> GetData()
        {
            var devices = new List<Device>();

            devices.Add(new Device
            {
                DeviceName = "Device 1",
                DeviceId = "123456789"
            });

            devices.Add(new Device
            {
                DeviceName = "Device 2",
                DeviceId = "123456788"
            });

            return devices;
        }

        private async void HandleRefresh(object sender, EventArgs e)
        {
            await RefreshDeviceList();

        }
        private async Task RefreshDeviceList()
        {
            List<Device> data;

            data = GetData();

            if (data == null)
            {
                return;
            }


            if (_devices == null)
            {
                _devices = data;
            }
            else
            {
                _devices.Clear();
                _devices.AddRange(data);
            }

            _adapter.NotifyDataSetChanged();
            _refresher.Refreshing = false;
        }
    }

有人可以帮我解决这个问题吗? 提前谢谢你 基督教

0 个答案:

没有答案