代码拉取完成,页面将自动刷新
实现页面滑动的监听,其中包含普通实现和rxjava实现
滑动监听实现 |
---|
![]() |
通过DevEco studio,并下载openHarmonySDK 将项目中的build.gradle文件中dependencies→classpath版本改为对应的版本(即你的IDE新建项目中所用的版本)
方式一:
通过library生成har包,添加har包到libs文件夹内
在entry的gradle内添加如下代码
implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
方式二:
allprojects{
repositories{
mavenCentral()
}
}
implementation 'io.openharmony.tpc.thirdlib:swipe:1.0.5'
Step 1: Create Swipe
attribute in the Ability
:
swipe = new Swipe();
Step 2: Initialize Swipe
object and set listener:
swipe.setListener(new SwipeListener() {
@Override public void onSwipingLeft(final TouchEvent event) {
info.setText("SWIPING_LEFT");
}
@Override public boolean onSwipedLeft(final TouchEvent event) {
info.setText("SWIPED_LEFT");
return false;
}
@Override public void onSwipingRight(final TouchEvent event) {
info.setText("SWIPING_RIGHT");
}
@Override public boolean onSwipedRight(final TouchEvent event) {
info.setText("SWIPED_RIGHT");
return false;
}
@Override public void onSwipingUp(final TouchEvent event) {
info.setText("SWIPING_UP");
}
@Override public boolean onSwipedUp(final TouchEvent event) {
info.setText("SWIPED_UP");
return false;
}
@Override public void onSwipingDown(final TouchEvent event) {
info.setText("SWIPING_DOWN");
}
@Override public boolean onSwipedDown(final TouchEvent event) {
info.setText("SWIPED_DOWN");
return false;
}
});
Step 1: Create Swipe
attribute and Subscription
in the Ability
:
private Swipe swipe;
private Disposable disposable;
swipe = new Swipe();
Step 2: Initialize Swipe
object and subscribe Observable
:
disposable = swipe.observe()
.subscribeOn(Schedulers.computation())
.observeOn(HarmonySchedulers.mainThread())
.subscribe(swipeEvent -> info.setText(swipeEvent.toString()));
SwipeEvent
is an enum with the following values:
public enum SwipeEvent {
SWIPING_LEFT,
SWIPED_LEFT,
SWIPING_RIGHT,
SWIPED_RIGHT,
SWIPING_UP,
SWIPED_UP,
SWIPING_DOWN,
SWIPED_DOWN
}
Step 3: override onTouchEvent(Component component, TouchEvent touchEvent)
:
@Override
public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
swipe.dispatchTouchEvent(touchEvent);
return true;
}
Step 4: dispose previously created Disposable
when it's no longer needed:
@Override
protected void onInactive() {
super.onInactive();
safelyUnsubscribe(disposable);
}
private void safelyUnsubscribe(Disposable disposable) {
if (disposable != null && !disposable.isDisposed()) {
disposable.dispose();
}
}
Copyright 2016 Piotr Wittchen
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。