【问题标题】:Clickable table of contents可点击的目录
【发布时间】:2013-05-28 07:56:24
【问题描述】:

我正在尝试制作一个与wikipedias as shown here. 感觉相似的“目录”

我有一个ScrollView,其中包含许多TextViews。我制作了一个包含可点击元素的“目录”,但我无法弄清楚如何将ScrollViewscrollTo() 放在正确的位置。

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

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

如果我能在ScrollView 中找到IndexOfTextView,我可以很容易地找到ScrollTo,但我不知道该怎么做。

谢谢, EDIT2:这与超链接到 HTML 中的内联锚非常相似,使用 #as described here.

编辑:添加了我的 XML,这似乎是使代码更清晰的唯一重要部分。目标是使用 id KeyQuestionsTextView 访问 TextView,然后向下滚动到 id 为 KeyQuestionsTextView。我已经让KeyQuestionsTextViewonclick 监听器注册了OnClick 事件,但我不知道如何让ScrollView 滚动到正确的位置。我真的需要在ScrollView 中获取所需TextView 的Y 偏移量,因为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>

【问题讨论】:

    标签: android


    【解决方案1】:

    我有一个解决方案。首先你得到触摸的 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 
    

    【讨论】:

    • 目前ScrollView 里面有一个LinearLayout。我做了一些快速搜索以将其设为TableLayout,并发现最少的积极结果。在我走上这条路之前,如果这可行,其他人可以插话吗? stackoverflow.com/questions/6585019/…stackoverflow.com/questions/8861851/…
    • 使用我试图模拟的 Wikipedia 中的“目录”布局,您单击目录中的一个项目,它会滚动到该内容。我不确定上面的代码如何让我从OnClick 事件中滚动到新位置。 OnClick 事件在我想要滚动到的实际 TextView 上方触发了很多行。我不熟悉 MotionEvents,也许这可以帮助我“索引”我希望导航到的不同 TextViews,但我不确定。
    • 能否贴出你的代码,让你的qstn更清晰。
    • 添加了我正在使用的 XML 文件,不确定是否有帮助。
    【解决方案2】:

    我想通了。我发现我可以很容易地获得TextView 的 Y 位置,并且很容易获得ScrollTo 的 Y 位置:

    ScrollView sv = (ScrollView) findViewById(R.id.scrollView1);
    TextView tv = (TextView) findViewById(R.id.KeyQuestionsTextView);
    sv.scrollTo(0, (int) tv.getY());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-12
      相关资源
      最近更新 更多