use of rx.functions.Action1 in project LiYuJapanese by 54wall.
the class MainActivity method setViewPager.
@Override
public void setViewPager(final List<BannerItem> data) {
if (bannerSubscription != null && bannerSubscription.isUnsubscribed()) {
bannerSubscription.unsubscribe();
}
mBannerViewPager.setAdapter(new BannerPagerAdapter(getSupportFragmentManager(), data));
mCircleIndicator.setViewPager(mBannerViewPager);
bannerSubscription = Observable.timer(10, 10, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
int next = (mBannerViewPager.getCurrentItem() + 1) % data.size();
mBannerViewPager.setCurrentItem(next);
}
});
}
use of rx.functions.Action1 in project AndroidStudy by tinggengyan.
the class RetrofitMainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrofit);
this.retrofitTextView = (TextView) findViewById(R.id.retrofitTextView);
// 1. 创建Retrofit2的实例,并设置BaseUrl和Gson转换。
Retrofit retrofit = new Retrofit.Builder().baseUrl(API).addConverterFactory(GsonConverterFactory.create()).client(new OkHttpClient()).build();
// 2. 创建请求服务,并为网络请求方法设置参数
GitHubService gitHubService = retrofit.create(GitHubService.class);
// Call是Retrofit中重要的一个概念,代表被封装成单个请求/响应的交互行为
final Call<List<Contributor>> call = gitHubService.contributors("square", "retrofit");
// 3. 请求网络,并获取响应
Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
try {
// execute(同步)或者enqueue(异步)方法,发送请求到网络服务器,并返回一个响应(Response)。
Response<List<Contributor>> response = call.execute();
subscriber.onNext(response.body().toString());
// 因为call只能被执行一次,可以clone一个,再重新执行
Call<List<Contributor>> cloneCall = call.clone();
cloneCall.enqueue(new Callback<List<Contributor>>() {
@Override
public void onResponse(Call<List<Contributor>> call, Response<List<Contributor>> response) {
Log.e(TAG, "onResponse:" + response.body().toString());
}
@Override
public void onFailure(Call<List<Contributor>> call, Throwable t) {
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
Action1<String> onNext = new Action1<String>() {
@Override
public void call(String o) {
retrofitTextView.setText(o);
}
};
observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(onNext);
}
use of rx.functions.Action1 in project knotx by Cognifide.
the class KnotxServerRoutingTest method createSimpleGatewayKnot.
private void createSimpleGatewayKnot(final String address, final String transition) {
Action1<KnotContext> simpleKnot = knotContext -> {
ClientResponse clientResponse = new ClientResponse();
clientResponse.setBody(Buffer.buffer());
clientResponse.setStatusCode(200);
knotContext.setClientResponse(clientResponse);
knotContext.setTransition(transition);
};
MockKnotProxy.register(vertx.vertx(), address, simpleKnot);
}
use of rx.functions.Action1 in project divide by HiddenStage.
the class MyActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DivideDrawer.attach(this, R.layout.main);
// setContentView(R.layout.main);
ButterKnife.inject(this);
getActionBar().setHomeButtonEnabled(true);
// au = AuthUtils.get(this, AuthManager.ACCOUNT_TYPE); TODO replace this
app = (MyApplication) this.getApplication();
BackendServices.addLoginListener(new LoginListener() {
@Override
public void onNext(BackendUser user) {
System.out.println("loginListener: setUser: " + user);
if (user != null) {
setUser(user);
}
}
});
adapter = new BackendObjectAdaper(this, objectList);
usersLV.setAdapter(adapter);
usersLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
BackendObject o = adapter.getItem(position);
Query q = new QueryBuilder().delete().from(BackendObject.class).where(TransientObject.OBJECT_KEY, OPERAND.EQ, o.getObjectKey()).build();
BackendServices.remote().query(BackendObject.class, q).subscribe(new Action1<Collection<BackendObject>>() {
@Override
public void call(Collection<BackendObject> backendObjects) {
getObjects();
}
});
BackendServices.local().delete(o);
}
});
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (BackendUser.isLoggedIn())
getObjects();
else
logger.debug("Not signed in, not querying data.");
}
}, 1000);
Storage s = new XmlStorage(new File(this.getFilesDir() + File.separator + "something.xml"), Storage.MODE_WORLD_WRITEABLE);
String id = "something";
System.out.println("Stored: " + s.getString(id, ""));
System.out.println("Stored: " + s.contains(id));
s.edit().putString(id, "something2").commit();
s.edit().putInt("int", 55).commit();
}
use of rx.functions.Action1 in project AnDevCon-RxPatterns by colintheshots.
the class Example5 method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example5);
mColdTextView = (TextView) findViewById(R.id.coldTextView);
mColdTextView2 = (TextView) findViewById(R.id.coldTextView2);
mHotTextView = (TextView) findViewById(R.id.hotTextView);
mHotTextView2 = (TextView) findViewById(R.id.hotTextView2);
mHotTextView.setText("Click Me!");
// Cold Observable
// Every time .subscribe() is called, it runs the function again.
// Using the .cache() operator is one of many options to make
// this hot.
final Observable<String> coldObservable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
for (int i = 0; i <= LOOP_MAX; i++) {
subscriber.onNext(Integer.toString(i));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
subscriber.onError(e);
}
}
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
coldObservable.subscribe(new Action1<String>() {
@Override
public void call(String s) {
mColdTextView.setText("Cold: " + s);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
}
});
// pause 5 seconds, then start second subscription from cold
// observable to show that the function executes again from scratch.
// CAUTION: Retrofit uses COLD observables by default, which means
// two separate subscriptions to the same observable will make two
// network calls. Turn observables HOT if you want the same values
// for all subscribers. Know which operators convert temperature.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
coldObservable.subscribe(new Action1<String>() {
@Override
public void call(String s) {
mColdTextView2.setText("Cold2: " + s);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
}
});
}
}, 4000);
// Hot Observable
// Hot observables emit events oblivious of subscribers so
// that every subscriber receives the same events.
Observable<Void> hotObservable = RxView.clicks(mHotTextView);
hotObservable.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
mNumClicks++;
mHotTextView.setText("Hot: " + Integer.toString(mNumClicks) + " (click again)");
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
}
});
hotObservable.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
mHotTextView2.setText("Hot2: " + Integer.toString(mNumClicks));
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
}
});
}
Aggregations