2013-05-27 4 views
2

Je cherche à faire une « table des matières » qui est similaire à la sensation de wikipedias as shown here.Table des matières cliquable

J'ai un ScrollView qui contient beaucoup TextViews. J'ai fait une «table des matières» qui a des éléments cliquables, mais je n'arrive pas à comprendre comment faire du ScrollViewscrollTo() le bon endroit.

J'ai essayé de convertir le TextView en une chaîne, et d'utiliser s.IndexOf(), mais il me donne l'emplacement du texte dans la chaîne, pas dans le ScrollView. Ce comportement a du sens

J'ai ensuite essayé de regarder d'autres méthodes ScrollView pour trouver quelque chose d'utile, mais j'ai manqué d'idées.

Si je pouvais trouver le IndexOf le TextView dans le ScrollView je pouvais facilement ScrollTo cet endroit, mais je ne peux pas savoir comment faire cela.

Merci, EDIT2: Ceci est très similaire à hyperlinking à une ancre en ligne en HTML, en utilisant le # as described here.

EDIT: ajouté mon XML, cela semble être la seule partie importante du code pour c'est plus clair. L'objectif est de frapper le TextView avec l'ID KeyQuestionsTextView et de le faire défiler jusqu'au TextView avec l'ID KeyQuestions. J'ai fait le onclick écouteur pour KeyQuestionsTextView enregistrer l'événement OnClick, mais je ne sais pas comment faire le ScrollView défiler au bon endroit. J'ai vraiment besoin de juste obtenir le décalage Y de la TextView souhaitée dans le ScrollView, comme la méthode ScrollTo est simple, il semble. L'exemple ci-dessus sera répété pour PhysicalExamTextViewDiagnosticReasoning et Emergencies. Ces quatre éléments composent ma «table des matières».

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

Répondre

0

J'ai un solution.First vous obtenez l'axe Y de contact, lorsque vous cliquez sur un Textview comme celui-ci,

@Override 
public boolean onTouchEvent(MotionEvent event) { 

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

return false; 
} 

puis faites défiler votre point de vue de défilement pour que Y axe comme,

YourScrollView.scrollTo(0, y);//here y is the y axis coordinate got from touch event 
+0

Actuellement, le '' ScrollView' a une LinearLayout' à l'intérieur de. J'ai fait quelques recherches rapides pour en faire un «TableLayout» et trouvé des résultats positifs minimes. Est-ce que quelqu'un d'autre peut s'interroger si cela peut fonctionner avant que je ne me dirige vers cette route? http://stackoverflow.com/questions/6585019/tablelayout-in-scrollview-not-working http://stackoverflow.com/questions/8861851/scrollview-doesnt-display-whole-tablelayout – fooOnYou

+0

S'il vous plaît voir mon Editer la réponse –

+0

Avec la mise en page "Table des matières" de Wikipédia que j'essaie d'imiter, vous cliquez sur un élément dans la table des matières et il défile vers ce contenu.Je ne suis pas sûr comment le code ci-dessus m'obtiendrait de l'événement 'OnClick' pour faire défiler vers le nouvel emplacement. L'événement 'OnClick' est déclenché de nombreuses lignes au-dessus du' TextView' que je veux faire défiler. Je ne suis pas familier avec MotionEvents, et peut-être que cela pourrait peut-être m'aider à «indexer» les différents «TextViews» dans lesquels j'espère naviguer, mais je n'en suis pas certain. – fooOnYou

0

Je l'ai compris. J'ai découvert que je pouvais obtenir l'emplacement du Y TextView assez facilement et ScrollTo assez facilement:

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