这对我来说是一个新的挑战,但从未解决过。我希望任何人都可以帮助我度过这个痛苦的时光。
我使用Listview显示来自我的网络服务的项目并刷新列表数据SwipeRefreshLayout是我的唯一选择。
当我下拉SwipeRefresh时,列表项目刷新但App冻结以完全加载数据并且SwipeRefreshLayout指示器不移动。解决方案是什么?
代码在这里:
ApiDataManager.ApiDAl apiDAl=new ApiDataManager.ApiDAl();
private DrawerLayout _drawer;
private ActionBarDrawerToggle _barDrawer;
private ListView listview;
private PodCastAdapter _podCastAdapter;
private SwipeRefreshLayout _swipeRefresh;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
var _toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
_drawer = FindViewById<DrawerLayout>(Resource.Id.leftDrawer);
SetSupportActionBar(_toolbar);
SupportActionBar.Title = "Some";
//Swipe Refresh
_swipeRefresh = FindViewById<SwipeRefreshLayout>(Resource.Id.swipeRefresh);
_swipeRefresh.Refresh += _swipeRefresh_Refresh;
_barDrawer = new ActionBarDrawerToggle(this, _drawer, _toolbar, 0, 1);
SupportActionBar.SetHomeButtonEnabled(true);
listview = FindViewById<ListView>(Resource.Id.EslTitles);
//Get list data by webservice backend
_podCastAdapter = new PodCastAdapter(this);
listview.Adapter = _podCastAdapter;
listview.ItemClick += Listview_ItemClick;
}
private void _swipeRefresh_Refresh(object sender, System.EventArgs e)
{
//Get list data by last received
listview.Adapter = _podCastAdapter;
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += Worker_DoWork;
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
worker.RunWorkerAsync();
}
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_swipeRefresh.Refreshing = false;
Toast.MakeText(this, "refreshing false", ToastLength.Long).Show();
}
private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
//Get list data by webservice backend
_podCastAdapter = new PodCastAdapter(this);
listview.Adapter = _podCastAdapter;
}
主要AXML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/leftDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Main Content-->
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefresh"
android:layout_height="match_parent"
android:layout_width="wrap_content">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/EslTitles" />
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
<!--Left Navigation Drawer-->
<LinearLayout
android:id="@+id/llLeftDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="?attr/colorAccent"
android:layout_gravity="left">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="test"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="test"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="test"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
答案 0 :(得分:1)
您遇到问题,因为您在非UI线程中调用UI元素。移动
listview.Adapter = _podCastAdapter;
到
Worker_RunWorkerCompleted
此方法与UI-thread
同步