2017-09-27 52 views
0

这对我来说是一个新的挑战,但从未解决。我希望任何人都可以帮助我通过这个痛苦的时代。当通过下拉菜单从API加载列表项时,App冻结SwipeRefreshLayout

我用Listview来显示我的Web服务中的项目和刷新列表数据SwipeRefreshLayout是我的一个选择。

当我pulldown SwipeRefresh,清单项刷新,但应用程序冻结完全加载数据和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> 

enter image description here

回答

1

你有问题,因为你调用nonUI线程的UI元素。移动

listview.Adapter = _podCastAdapter; 

Worker_RunWorkerCompleted

此方法与UI线程同步

+0

由于@helsq,我移动listview.Adapter = _podCastAdapter;到Worker_RunWorkerCompleted方法,但从未解决我的问题。 –

+0

它把我扔进一个未处理的异常:Java.Lang.IllegalStateException:Observer [email protected]未注册。 –

+0

这是不同的问题。您不必重新创建** PodCatAdatper **,您必须更新其中的数据。实施例之前: '公共PodCastAdapter(上下文上下文){ \t //初始化 \t //负载数据 }' 后: '公共PodCastAdapter(上下文上下文){ \t //初始化 } 公共无效LoadData (){ \t //加载数据 } ' 和Worker_DoWork你会叫** _ podCastAdapter.LoadData()**和** RunWorkerCompleted _ podCastAdapter.NotifyDataSetChanged()** – helsq

相关问题