首页 > 解决方案 > 如何在android中打开新活动并跳转到特定的子视图(类似于超链接)?)

问题描述

可能看起来是一个小问题,但我是初学者,所以需要你的帮助。

我有一个“主要活动”,有两个按钮,分别称为“愿景和使命”和“团队”。

我有“详细信息活动”,其中包含关于我们、愿景和使命、团队等详细信息。

现在,如果我单击“主要活动”中的愿景和任务按钮,它应该会自动打开“详细活动”并滚动到/跳转到活动的愿景和任务部分。团队按钮也应如此,它会自动打开详细信息活动并直接显示团队部分,而无需用户向下滚动。

这类似于指向网站特定部分的超链接。

标签: androidandroid-intenttextview

解决方案


您可以通过调用此函数来启动活动。这里每个部分都应该有一个数字。

 // 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
    }

推荐阅读