2017-09-27 4 views
0

C'est un nouveau défi pour moi mais jamais résolu. J'espère que tout le monde peut m'aider à réussir ce douloureux jeu de rôles.L'application se bloque lors du chargement des éléments de la liste de l'API en décrochant SwipeRefreshLayout

J'ai utilisé Listview pour afficher les éléments de mon service Web et actualiser les données de la liste SwipeRefreshLayout est l'option qui me convient.

Lorsque je pulldown SwipeRefresh, liste éléments actualisés, mais l'application se fige pour charger complètement les données et l'indicateur de SwipeRefreshLayout ne bouge pas .Quelle est la solution?

Le code est ici:

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; 
} 

principal Code 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

Répondre

1

Vous avez un problème, parce que vous appelez élément d'interface utilisateur dans nonUI-fil. Déplacer

listview.Adapter = _podCastAdapter; 

à

Worker_RunWorkerCompleted

cette méthode synchronisée avec filetage de l'interface utilisateur

+0

Merci @helsq, je me suis déplacé listview.Adapter = _podCastAdapter; à Worker_RunWorkerCompleted méthode, mais n'a jamais résolu mon problème. –

+0

Et il me jeter dans une exception non gérée: Java.Lang.IllegalStateException: Observer [email protected] n'a pas été enregistré. –

+0

Ceci est un problème différent. Vous n'avez pas besoin de recréer un ** PodCatAdatper **, vous devez mettre à jour les données qu'il contient. Exemple avant: 'PodCastAdapter publique (contexte de contexte) { \t // initialisation \t // données de charge }' après: 'PodCastAdapter publique (contexte de contexte) { \t // initialisation } de LoadData public void() {// \t données de charge } ' et Worker_DoWork vous appellerez ** _ podCastAdapter.LoadData() ** et ** RunWorkerCompleted _ podCastAdapter.NotifyDataSetChanged() ** – helsq