2

我正在尝试制作一个与维基百科类似的“目录”,如此处所示。

我有一个ScrollView包含许多TextViews. 我制作了一个包含可点击元素的“目录”,但我不知道如何找到ScrollView scrollTo()正确的位置。

我试图将 转换TextView为字符串并使用s.IndexOf(),但它为我提供了字符串中文本的位置,而不是ScrollView. 这种行为是有道理的

然后,我尝试查看其他ScrollView方法以找到有用的方法,但没有想法。

如果我能在里面找到IndexOf那个地方,我可以很容易地找到那个地方,但我不知道该怎么做。TextViewScrollViewScrollTo

谢谢, EDIT2:这与超链接到 HTML 中的内联锚非常相似,使用 #如此处所述。

编辑:添加了我的 XML,这似乎是使代码更清晰的唯一重要部分。目标是点击TextViewwith idKeyQuestionsTextView并让它向下滚动到TextViewwith id KeyQuestions。我已经让onclick监听器KeyQuestionsTextView注册OnClick事件,但我不知道如何让ScrollView滚动到正确的位置。我真的需要在 中获得所需的 Y 偏移量TextViewScrollView因为该ScrollTo方法看起来很简单。

上面的例子将重复PhysicalExamTextView DiagnosticReasoningEmergencies。这四个元素构成了我的“目录”。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Platform2_option1" >



<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
        android:id="@+id/Intro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Plat2Option1Intro" />

        <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="#000000" />

        <TextView
         android:id="@+id/KeyQuestionsTextView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/KeyQuestionsTitle" />
        <TextView
         android:id="@+id/PhysicalExamTextView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/PhysicalExamTitle" />
        <TextView
         android:id="@+id/DiagnosticReasoningTextView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/DiagnosticReasoningTitle" />
        <TextView
         android:id="@+id/EmergenciesTextView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/EmergenciesTitle" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="#000000" />


        <TextView
            android:id="@+id/KeyQuestions"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Plat2Option1KeyQuestions" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="#000000" />

        <TextView
            android:id="@+id/PhysicalExam"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Plat2Option1PhysicalExam" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="#000000" />

        <TextView
            android:id="@+id/DiagnosticReasoning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Plat2Option1DiagReasoning" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="#000000" />

        <TextView
            android:id="@+id/Emergencies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Plat2Option1Emergencies" />

    </LinearLayout>
</ScrollView>

4

2 回答 2

0

我想到了。我发现我可以很容易地得到 Y 位置,TextView而且ScrollTo相当容易:

ScrollView sv = (ScrollView) findViewById(R.id.scrollView1);
TextView tv = (TextView) findViewById(R.id.KeyQuestionsTextView);
sv.scrollTo(0, (int) tv.getY());
于 2013-05-27T23:56:24.793 回答
0

我有一个解决方案。首先你得到触摸的 Y 轴,当你点击这样的 Textview 时,

    @Override
public boolean onTouchEvent(MotionEvent event) {

    int y = (int)event.getY();

return false;
}

然后将滚动视图滚动到 Y 轴,例如,

YourScrollView.scrollTo(0, y);//here y is the y axis coordinate got from touch event 
于 2013-05-27T04:16:33.070 回答