网上搜索侧滑关闭Activity,都是大同小异,就算有也没有完整的demo,说来集成也是简单,等会你就知道了。
个人习惯,写博客前喜欢先截图
1.首先以项目的形式导入到你的项目中
2.然后在你要侧滑的Activity集成SwipeBackActivity,注意只要集成就好的,其他的什么都不要你写,就能实现侧滑了
下面列出代码:
Mainactivity.java
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text);
}
public void click(View view) {
if (view.getId() == R.id.button) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
} else {
startActivityForResult(new Intent(MainActivity.this, ThirdActivity.class), 1);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String result = data.getStringExtra("result");
if (requestCode == 1 && resultCode == 0) {
textView.setText(result);
}
}
}
SecondActivity.java
public class SecondActivity extends SwipeBackActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
ThirdActivity.java
public class ThirdActivity extends SwipeBackActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
}
@Override
public void dothingBeforeFinish() {//这里是你要返回一些数据个启动的那个Activity,你可以在此做操作
super.dothingBeforeFinish();
Intent intent = new Intent();
intent.putExtra("result", "来自ThirdActivity返回的值");
setResult(0, intent);
}
}
代码是不是很简单,这样就轻易的实现了沉浸式侧滑了。至于侧滑的Activity状态栏为什么是灰色的,是因为我在SwipeBackActivity中写死了,你可以修改此颜色。
setupStatusBarView(activity, contentLayout, Color.parseColor("#cccccc"));
好了,在此附上github源码,喜欢的请start、fork。
https://github.com/DyncKathline/SwipeBackLayout
转载请注明出处,谢谢!
时间: 2024-11-02 13:39:24