2015-08-22 2 views
6

J'ai essayé d'implémenter Pull to Refresh dans mon application.Puis-je utiliser Tirer pour actualiser sans utiliser ScrollView/ListView?

J'ai enveloppé mon code XML avec android.support.v4.widget.SwipeRefreshLayout et ça marche, mais l'interface graphique ne semble pas bonne. Quand je tire vers le bas, je devrais voir l'icône du cercle animé régulier avec mes couleurs définies, non ?, mais je le vois comme un cercle vide et pas un tournant avec des couleurs comme il se doit.

En outre, je ne peux pas "jouer" avec et même le maintenir pendant un moment après l'avoir tiré, il descend pendant une seconde (faire ce qu'il fait) et disparaît ensuite.

Je pense que c'est parce que je n'utilise pas ListView mais LinearLayout à la place.

Qu'en pensez-vous?

code:

XML:

<android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/swipe"> 

    <LinearLayout 
     android:layout_height="match_parent" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:background="#7B68EE" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     tools:context=".HomeActivity"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/loin" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="@string/welcome" 
      android:textColor="#B0C4DE" 
      android:id="@+id/welcomeBackID" 
      android:layout_alignParentTop="true" 
      android:layout_alignRight="@+id/webViewCurren" 
      android:layout_alignEnd="@+id/webViewCurren" /> 

     .... 

</android.support.v4.widget.SwipeRefreshLayout> 

Java:

public class HomeActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener { 

    SwipeRefreshLayout mSwipeRefreshLayout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 

     mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe); 
     mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_green_light, 
       android.R.color.holo_blue_bright, 
       android.R.color.holo_orange_light, 
       android.R.color.holo_red_light); 
     mSwipeRefreshLayout.setOnRefreshListener(this); 

    @Override 
    public void onRefresh() { 
     Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show(); 
     sharedPrefEditor.putString("coordinateX", "34.7"); 
     sharedPrefEditor.putString("coordinateY", "32.5"); 
     sharedPrefEditor.putString("saved", "false"); 
     sharedPrefEditor.commit(); 

     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       mSwipeRefreshLayout.setRefreshing(false); 
      } 
     }, 3000); 
    } 
} 

Répondre

0

Essayez de changer le schéma de couleurs SwipeRefreshLayout.

swipeRefreshLayout.setColorSchemeColors(
    Color.RED, Color.GREEN, Color.BLUE, Color.CYAN); 

En outre, le SwipeRefreshLayout sera visible jusqu'à son exécution onRefresh() termine.