DEV Community

Sem
Sem

Posted on • Originally published at semihcelikol.com

Xamarin.Android - Using SwipeRefreshLayout

Layout:

    <android.support.v4.widget.SwipeRefreshLayout
                    android:id="@+id/swipeRefreshLayoutMain"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ListView
                        android:id="@+id/listMain"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"/>

                </android.support.v4.widget.SwipeRefreshLayout>
Enter fullscreen mode Exit fullscreen mode

Activity:


    using Android.App;
    using Android.OS;
    using Android.Support.V7.App;
    using Android.Runtime;
    using Android.Widget;
    using Android.Support.V4.Widget;

    namespace SwipeRefreshExample
    {
        [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
        public class MainActivity : AppCompatActivity
        {
            ListView listMain;
            SwipeRefreshLayout swipeRefreshLayoutMain;

            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                Xamarin.Essentials.Platform.Init(this, savedInstanceState);
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.activity_main);

                listMain = FindViewById<ListView>(Resource.Id.listMain);
                swipeRefreshLayoutMain = FindViewById<SwipeRefreshLayout>(Resource.Id.swipeRefreshLayoutMain);

                swipeRefreshLayoutMain.Refresh += SwipeRefreshLayoutMain_Refresh;
            }

            private void SwipeRefreshLayoutMain_Refresh(object sender, System.EventArgs e)
            {
                showData();
                swipeRefreshLayoutMain.Refreshing = false;
            }

            public void showData()
            {
                string[] data = new string[] { "Data 1", "Data 2", "Data 3", "Data 4", "Data 5" };

                ArrayAdapter arrayAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, data);

                listMain.Adapter = arrayAdapter;
            }

            public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
            {
                Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

                base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

Finish and Output:

Top comments (0)