2016-04-02 8 views
1

この「問題」への回答については、このエントリの最後を参照してください。ScrollView内でビューのフォーカスを要求し、画面の上に配置します

私のアプリでは、いくつかのXMLビューを膨らませて、それらをScrollView内のLinearLayoutリストに追加しました。 XMLは次のようになります。

<ScrollView 
     android:layout_width="0dp" 
     android:layout_weight="19" 
     android:layout_height="wrap_content" 
     android:fillViewport="true"> 
     <LinearLayout 
      android:id="@+id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
     </LinearLayout> 
</ScrollView> 

私は「リスト」に入れたビューの1つに焦点を当てます。スクロールダウンで上記のコードの結果は、集中ビューは、画面の一番下に置かれます場合

view.getParent().requestChildFocus(view, view); 

、それはスクロールアップにつながる場合、集中ビューが置かれます。これは、このコードを使用して行われます画面の上部に表示されます。

リストの長さが許せば、フォーカスされたビューを常に画面上部に配置する方法はありますか?

編集:これが機能します!受け入れられた答えをご覧ください。

XML

<ScrollView 
     android:id="@+id/scroll_list" 
     android:layout_width="0dp" 
     android:layout_weight="19" 
     android:layout_height="wrap_content" 
     android:fillViewport="true"> 
     <LinearLayout 
      android:id="@+id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
     </LinearLayout> 
</ScrollView> 

サンプルあなたの活動のどこかに置くためのコードまたはフラグメント

view = findViewById(get_view_that_should_have_focus); 
ScrollView scrollView = findViewById(R.id.scroll_list); 
scrollView.smoothScrollTo(0, view.getTop()); 
+0

詳細を教えてください。多分、何が起きているのですか? – mhdjazmati

答えて

2

あなたはこれを使って、特定のビューに垂直スクロールすることができます:それはようになります

scrollView.smoothScrollTo(0,view.getTop()); 

をスクロールビューのトップビュー(十分な高さがある場合)スクロールした後、このビュー。

関連する問題