当前位置:网站首页>Complete activity switching according to sliding
Complete activity switching according to sliding
2022-07-05 11:57:00 【asahi_ xin】
quote
First step : take OnGestureListener Interface encapsulation
public class SlideListener implements android.view.GestureDetector.OnGestureListener {
private Context context;
private Class<? extends Activity> leftCls;
private Class<? extends Activity> rightCls;
public SlideListener(Context context, Class<? extends Activity> leftCls,Class<? extends Activity> rightCls) {
this.context = context;
this.leftCls = leftCls;
this.rightCls = rightCls;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int FLING_MIN_DISTANCE = 50;
float FLING_MIN_VELOCITY = 50;
if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
// Scroll left , The right view shows
ActivityManager.goActivity(context, rightCls);
} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
// Slide to the right , Left side view display
ActivityManager.goActivity(context, leftCls);
}
return false;
}
}
The second step : initialization
public class MainActivity extends AppCompatActivity {
GestureDetector gd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gd = new GestureDetector(this, new SlideListener(this, LeftActivity.class,RightActivity.class));
}
}
The third step : Realization OnTouchListener Interface , And set up monitoring
public class MainActivity extends AppCompatActivity implements View.OnTouchListener{
GestureDetector gd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gd = new GestureDetector(this, new SlideListener(this, LeftActivity.class,RightActivity.class));
ConstraintLayout layout = findViewById(R.id.layout);
layout.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
v.performClick();
return gd.onTouchEvent(event);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Optimize
The above three steps can realize sliding switching , But it seems stiff without animation when switching , You can refer to
边栏推荐
- 1.php的laravel创建项目
- [crawler] bugs encountered by wasm
- Vscode shortcut key
- Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in
- pytorch-线性回归
- 简单解决redis cluster中从节点读取不了数据(error) MOVED
- splunk配置163邮箱告警
- Yolov5 target detection neural network -- calculation principle of loss function
- 【 YOLOv3中Loss部分计算】
- leetcode:1200. Minimum absolute difference
猜你喜欢
![[deploy pytoch project through onnx using tensorrt]](/img/b6/ec95f028f749753666c7877134ad70.png)
[deploy pytoch project through onnx using tensorrt]

pytorch-多层感知机MLP

【 YOLOv3中Loss部分计算】

Redis集群的重定向

pytorch-softmax回归

COMSOL -- establishment of 3D graphics

中非 钻石副石怎么镶嵌,才能既安全又好看?

12.(地图数据篇)cesium城市建筑物贴图

Sentinel sentinel mechanism of master automatic election in redis master-slave

Pytorch weight decay and dropout
随机推荐
Solve readobjectstart: expect {or N, but found n, error found in 1 byte of
Principle of redis cluster mode
Network five whip
Redirection of redis cluster
【pytorch 修改预训练模型:实测加载预训练模型与模型随机初始化差别不大】
Redis集群的重定向
Redis cluster (master-slave) brain crack and solution
2022年国内云管平台厂商哪家好?为什么?
15 methods in "understand series after reading" teach you to play with strings
《看完就懂系列》15个方法教你玩转字符串
Hiengine: comparable to the local cloud native memory database engine
【TFLite, ONNX, CoreML, TensorRT Export】
ACID事务理论
13. (map data) conversion between Baidu coordinate (bd09), national survey of China coordinate (Mars coordinate, gcj02), and WGS84 coordinate system
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
Vscode shortcut key
Codeforces Round #804 (Div. 2)
[crawler] bugs encountered by wasm
C # implements WinForm DataGridView control to support overlay data binding
Check the debug port information in rancher and do idea remote JVM debug