2017-09-27 36 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