android - 如何在android中打开新活动并跳转到特定的子视图(类似于超链接)?)
问题描述
可能看起来是一个小问题,但我是初学者,所以需要你的帮助。
我有一个“主要活动”,有两个按钮,分别称为“愿景和使命”和“团队”。
我有“详细信息活动”,其中包含关于我们、愿景和使命、团队等详细信息。
现在,如果我单击“主要活动”中的愿景和任务按钮,它应该会自动打开“详细活动”并滚动到/跳转到活动的愿景和任务部分。团队按钮也应如此,它会自动打开详细信息活动并直接显示团队部分,而无需用户向下滚动。
这类似于指向网站特定部分的超链接。
解决方案
您可以通过调用此函数来启动活动。这里每个部分都应该有一个数字。
// 1 is about us, 2 is vision and mission, etc.
public void startDetailActivity(int type){
Intent startIntent = new Intent(this, DetailsActivity.class);
startIntent.putExtra("Type", type);
startActivity(startIntent);
}
在您的 DetailsActivity 中,您将Type
参数滚动到相应的位置。
ScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
// this is the important part
scrollView = findViewById(R.id.scrollView);
int type = getIntent().getIntExtra("Type", 0);
scrollTo(type);
}
public void scrollTo(int type){
if(type == 0)
return;
if(type == 1){
//First argument is x, second is y. Test around a bit with the x value
scrollView.scrollTo(10, 0); //Use this if you want no animation
scrollView.smoothScrollTo(10, 0);//Use this if you want a scroll animation
} //add more if you want
}
推荐阅读
- r - x$name 不等于 x[["name", exact = FALSE]],为什么不呢?
- cordova - 在我的 android 设备中使用 media-capture cordova 插件的 captureImage 方法时应用程序崩溃
- python-3.x - 在 Putty/Plink 中使用 Python 代码在多个设备中执行命令
- kubernetes - 具有 2 个或更多指标的 kubernetes HPA 的行为如何——尤其是副本数计算?
- java - 如何重新训练 inceptionV3?
- java - 在具有特定位置android的另一个位图图像内绘制位图图像
- c# - C# 有条件地循环遍历 JSON 值
- python - 来自谷歌结果的 Python 抓取链接
- c# - 在同一代码块中多次访问列表 - 有更好的方法吗?
- wordpress - 如何使用用户的表单输入注册短代码?