Create a gist now

Instantly share code, notes, and snippets.

Embed
SwipeRefreshLayout example
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/srl_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_main"/>
</android.support.v4.widget.SwipeRefreshLayout>
package com.andraskindler.sandbox.activity;
import retrofit.RestAdapter;
import retrofit.http.GET;
import retrofit.http.Query;
import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;
public class ApiManager {
private interface ApiManagerService {
@GET("/weather")
WeatherData getWeather(@Query("q") String place, @Query("units") String units);
}
private static final RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://api.openweathermap.org/data/2.5")
.build();
private static final ApiManagerService apiManager = restAdapter.create(ApiManagerService.class);
public static Observable<WeatherData> getWeatherData(final String city) {
return Observable.create(new Observable.OnSubscribe<WeatherData>() {
@Override
public void call(Subscriber<? super WeatherData> subscriber) {
if (!subscriber.isUnsubscribed())
try {
subscriber.onNext(apiManager.getWeather(city, "metric"));
subscriber.onCompleted();
} catch (Exception e) {
subscriber.onError(e);
}
}
}).subscribeOn(Schedulers.io());
}
}
package com.andraskindler.sandbox.activity;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;
import com.andraskindler.sandbox.R;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
public class MainActivity extends Activity {
private WeatherListAdapter adapter;
private SwipeRefreshLayout swipeRefreshLayout;
private Subscription subscription;
private final String[] cities = new String[]{"Budapest", "Santiago", "Barcelona", "Madrid", "London", "Moscow", "Berlin", "New York", "San Francisco", "Dublin"};
private Observable<WeatherData> observable = Observable.from(cities)
.flatMap(new Func1<String, Observable<WeatherData>>() {
@Override
public Observable<WeatherData> call(String s) {
return ApiManager.getWeatherData(s);
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
private final Observer<WeatherData> observer = new Observer<WeatherData>() {
@Override
public void onCompleted() {
if (swipeRefreshLayout.isRefreshing())
swipeRefreshLayout.setRefreshing(false);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(WeatherData weatherData) {
adapter.update(weatherData);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_main);
swipeRefreshLayout.setColorScheme(R.color.blue, R.color.green, R.color.red, R.color.yellow);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
if (!subscription.isUnsubscribed())
subscription.unsubscribe();
adapter.clear();
subscription = observable.subscribe(observer);
}
});
adapter = new WeatherListAdapter();
((ListView) findViewById(R.id.lv_main)).setAdapter(adapter);
swipeRefreshLayout.setRefreshing(true);
subscription = observable.subscribe(observer);
}
}
package com.andraskindler.sandbox.activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class WeatherListAdapter extends BaseAdapter {
private List<WeatherData> items;
public WeatherListAdapter() {
items = new ArrayList<>();
}
public void update(final WeatherData weatherData){
items.add(weatherData);
notifyDataSetChanged();
}
public void clear(){
items.clear();
notifyDataSetChanged();
}
@Override
public int getCount() {
return items.size();
}
@Override
public WeatherData getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final TextView textView = new TextView(parent.getContext());
textView.setText(items.get(position).name + "\n" + items.get(position).main.temp);
return textView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment