横向滚动条顶端或尾端事件

#功能:当横向滚动条滚到顶端时,左箭头灰掉;当滚到尾端时,右箭头灰掉;当滚到中间时,左右箭头水红色;

效果如下图


 

<!-- /res/layout/horizontal_view_demo.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <LinearLayout
	  xmlns:android="http://schemas.android.com/apk/res/android"
	  android:orientation="horizontal"
	  android:layout_width="fill_parent"
	  android:layout_height="40dp">
  <TextView android:id="@+id/h_left"
  	android:layout_width="13dp"
  	android:layout_height="fill_parent"
  	android:textSize="20dp"
  	android:gravity="center"
  	android:background="@color/grey"
  	android:text="<"/>
  <HorizontalScrollView android:id="@+id/h_horizontal"
  	android:layout_width="200dp"
  	android:layout_height="fill_parent"
  	android:layout_weight="1"
  	android:scrollbars="none">
  	<LinearLayout
	  xmlns:android="http://schemas.android.com/apk/res/android"
	  android:orientation="horizontal"
	  android:layout_width="fill_parent"
	  android:layout_height="fill_parent">
	  <TextView android:id="@+id/h_t1"
	  	android:layout_width="70dp"
	  	android:layout_height="fill_parent"
	  	android:textSize="20dp"
	  	android:gravity="center"
	  	android:layout_margin="1dp"
	  	android:background="#666666"
	  	android:text="人事部"/>
	  <TextView android:id="@+id/h_t2"
	  	android:layout_width="70dp"
	  	android:layout_height="fill_parent"
	  	android:textSize="20dp"
	  	android:gravity="center"
	  	android:layout_margin="1dp"
	  	android:background="#666666"
	  	android:text="账务部"/>
	  <TextView android:id="@+id/h_t3"
	  	android:layout_width="70dp"
	  	android:layout_height="fill_parent"
	  	android:textSize="20dp"
	  	android:gravity="center"
	  	android:layout_margin="1dp"
	  	android:background="#666666"
	  	android:text="技术部"/>
	  <TextView android:id="@+id/h_t4"
	  	android:layout_width="70dp"
	  	android:layout_height="fill_parent"
	  	android:textSize="20dp"
	  	android:gravity="center"
	  	android:layout_margin="1dp"
	  	android:background="#666666"
	  	android:text="销售部"/>
	  <TextView android:id="@+id/h_t5"
	  	android:layout_width="70dp"
	  	android:layout_height="fill_parent"
	  	android:textSize="20dp"
	  	android:gravity="center"
	  	android:layout_margin="1dp"
	  	android:background="#666666"
	  	android:text="制作部"/>
	  <TextView android:id="@+id/h_t6"
	  	android:layout_width="70dp"
	  	android:layout_height="fill_parent"
	  	android:textSize="20dp"
	  	android:gravity="center"
	  	android:layout_margin="1dp"
	  	android:background="#666666"
	  	android:text="设计部"/>
	  <TextView android:id="@+id/h_t7"
	  	android:layout_width="70dp"
	  	android:layout_height="fill_parent"
	  	android:textSize="20dp"
	  	android:gravity="center"
	  	android:layout_margin="1dp"
	  	android:background="#666666"
	  	android:text="行政部"/>
	</LinearLayout>
  </HorizontalScrollView>
  <TextView android:id="@+id/h_right"
  	android:layout_width="13dp"
  	android:layout_height="fill_parent"
  	android:textSize="20dp"
  	android:gravity="center"
  	android:background="@color/pink"
  	android:text=">"/>
  </LinearLayout>
</LinearLayout>

 

public class HorizontalViewDemo extends Activity {
	private HorizontalScrollView hsv;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.horizontal_view_demo);
		
		hsv = (HorizontalScrollView)findViewById(R.id.h_horizontal);
		hsv.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View view, MotionEvent me) {
				TextView hLeft = (TextView)findViewById(R.id.h_left);
				TextView hRight = (TextView)findViewById(R.id.h_right);
				if (hsv.getScrollX() == 0) {
					hLeft.setBackgroundResource(R.color.grey);
				} else if (hsv.getScrollX() > 0) {
					hLeft.setBackgroundResource(R.color.pink);
				}
				if (hsv.getScrollX() == 210) {//210这个值在不同像数需要改变
					hRight.setBackgroundResource(R.color.grey);
				} else if (hsv.getScrollX() < 210) {
					hRight.setBackgroundResource(R.color.pink);
				}
				return false;
			}
		});
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值