HorizontalScrollView with arrows

I am trying to make a scrollable horizontal menu using HorizontalScrollView using the layout shown below. The menu is scrolled using the left / right or left arrow buttons. When the HorizontalScrollView reaches one end, I would like the arrow on the same end to be hidden (in the image shown below, I would like the left arrow to be hidden).

How can I detect that HorizontalScrollView has reached the end?

thank

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>
+5
source share
2 answers

getScrollX() LinearLayout , , . 0, .

, getScrollX() getMeasuredWidth(), , , .

, :

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

, . , HorizontalScrollView, onScroll() , . .

+3

HorizontalScrollView. , OnTouchListener. , , getScrollX, int.

0 , ( ), horzontal. addOnGlobalLayoutListener 0 onCreate

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;
    }
});
+4

All Articles