2011-10-06 27 views
5

Horizo​​ntalScrollViewを使用して、次のレイアウトを使用してスクロール可能な水平メニューを作成しようとしています。メニューは、前/次の矢印ボタンまたは飛行中にスクロール可能です。 Horizo​​ntalScrollViewが一方の端に達すると、同じ端の矢印が非表示になります(下の画像では、左矢印を非表示にします)。矢印付きのHorizo​​ntalScrollView

Horizo​​ntalScrollViewが終了したことをどのように検出できますか?

おかげ

enter image description here

<RelativeLayout android:background="@drawable/bg_home_menu" 
    android:layout_width="fill_parent" android:layout_height="45dp"> 
    <ImageView android:id="@+id/previous" android:src="@drawable/arrow_l" 
     android:layout_height="14dp" android:layout_width="10dp" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" /> 

    <ImageView android:id="@+id/next" android:src="@drawable/arrow_r" 
     android:layout_height="14dp" android:layout_width="10dp" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" /> 

    <HorizontalScrollView android:id="@+id/horizontalScroll" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:scrollbars="none" android:fadingEdgeLength="30dp" android:layout_toLeftOf="@id/next" 
     android:layout_toRightOf="@id/previous" > 

     <LinearLayout android:id="@+id/home_menu" 
      android:orientation="horizontal" android:layout_width="wrap_content" 
      android:layout_height="fill_parent" android:gravity="center_vertical"> 

      <Button android:id="@+id/btn_home" android:background="@drawable/btn_home_menu_on" 
       android:layout_height="35dp" android:layout_width="wrap_content" android:focusable="true" 
       android_clickable="false" android:text="@string/menu_home" android:textColor="#FFFFFF" android:tag="-1"/> 

      <!-- More are added dynamically --> 

     </LinearLayout> 

    </HorizontalScrollView> 
</RelativeLayout> 

答えて

3

あなたのスクロールバーの内側のLinearLayoutの使用getScrollX()にこのコードを配置することになる他のaddOnGlobalLayoutListenerを使用することによって発見されました画面上の角。 0の場合は、左矢印を無効にする必要があります。

drawing rectangleの幅を取得し、getScrollX()を追加して、それをgetMeasuredWidth()と比較してください。同じ場合は右に、右矢印は必要ありません。

だからあなたのコードは次のようになります。もちろん

if (homeMenu.getScrollX()==0) { 
    hideLeftArrow(); 
} else { 
    showLeftArrow(); 
} 
if (homeMenu.getDrawingRect().right == homeMenu.getMeasuredWidth()) { 
    hideRightArrow(); 
} else { 
    showRightArrow(); 
} 

あなたはイベントリスナーでこれを配置する必要があります。私はあなた自身のHorizo​​ntalScrollViewクラスを使うことしか考えられません。上のチェックを行うために上書きされるonScroll()メソッドを使って、this postを参照してください。

4

Horizo​​ntalScrollViewにはスクロールリスナーがありません。できることはOnTouchListenerを追加することです。これは、ユーザーがスクロールしたときに連続的に発生し、intを返すgetScrollXメソッドを使用できるたびに発生します。

ここで、0は左端を意味し、右端(最大スクロール量)を見つけるには、horzontalスクロールビューの子ビューの幅を見つける必要があります。それは、幅が常にのonCreateメソッド内0

が左端であるかを決定するためのonCreateメソッド

final HorizontalScrollView hs = (HorizontalScrollView)findViewById(R.id.scroll); 

ViewTreeObserver vto = hs.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     hs.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     maxScrollX = hs.getChildAt(0) 
       .getMeasuredWidth()-getWindowManager().getDefaultDisplay().getWidth(); 

    } 
});   

hs.setOnTouchListener(new OnTouchListener() {   
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     Log.e("ScrollValue", Integer.toString(hs.getScrollX())); 
    if(hs.getScrollX() == maxScrollX){ 
     Log.e("MaxRight", "MaxRight");  
    } 
    return false; 
    } 
}); 
+0

申し訳ありませんが、私の問題を解決する方法を理解できません... – jul

+1

私は私の答えを更新しました。これがうまくいかない場合、これは助けになるかもしれません。http://stackoverflow.com/questions/3948934/synchronise-scrollview-scroll-positions-android – blessenm

+1

+1 Thanxはたくさんあり、Charmのように働いています。本当に大変参考になりました。 –

関連する問題