2011-10-25 18 views
4

HorizontalScrollViewを含むTabHostのサンプルアプリケーションを開発しています。ここに私の問題があります:水平スクロールビューを使用したTabホストの自動スクロール - プログラム

  • 私は10個以上のタブを持っていますが、いずれかのタブをクリックすると重力を中心に設定する必要がありますか?どのように私はこの問題に来ることができます。

ありがとうございます!!!

+0

これを達成するには、ギャラリービューが最適です。 – milind

+0

私はgalleryviewと知っていますが、私はtabhost ..とそれをしたいです –

答えて

5

これには計算が含まれます。

要素を中心にするには、スクロールしてその位置に移動する必要があります。

int scrollX = (button.getLeft() - (screenWidth/2))+(button.getWidth()/2); 
hScrollView.scrollTo(scrollX,0); 

どこ

button -> It is the button you are trying to center. 

screenWidth -> Width of the screen. 
int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); 

hScrollView -> It is the horizontal scrollview. 

しかし、この方法でも、あなたはその境界を越えてスクロールしませんscrollviewsとしてエンド要素をセンタリングすることができません。

+0

私はもう一つのことを聞いてほしいですか? –

+0

私は10のタブを持っていると仮定します.1,2,3,4タブのいずれかをクリックすると2の位置に置き換えます。 –

0
public void onTabChanged(String tabId) { 
    for (int i = 0; i < tablist.length; i++) { 
     if (tabId.contentEquals(tablist[i])) { 
      HorizontalScrollView scroll = (HorizontalScrollView)findViewById(R.id.horScrollView); 
      scroll.smoothScrollTo(((int)(getResources().getDimensionPixelSize(R.dimen.tab_width) * (i))), 0); 
     } 
    } 

私は使用しました:OnTabChangeListenerを実装します。

関連する問題