DEV Community

Sem
Sem

Posted on • Originally published at semihcelikol.com

2 1

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:

Sentry mobile image

Improving mobile performance, from slow screens to app start time

Based on our experience working with thousands of mobile developer teams, we developed a mobile monitoring maturity curve.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay