隱藏滾動條
1、標籤屬性:android:scrollbars="none"
2、代碼設置:
setHorizontalScrollBarEnabled(false);//隱藏橫向ScorollView
setVerticalScrollBarEnabled(false);//隱藏縱向ScorollView
setOnTouchListener的使用:判斷ScrollView何時滑動到底部
1、getScorollY()——滾動條滑動的距離
2、getMeasuredHeight()——內容的整體高度,包括隱藏部分
3、getHeight()——顯示高度。內容未佈滿屏幕,2=3;內容大於屏幕,3=屏幕高度,2>3。
4、getChildAt(int i)——獲取ScorollView的第i個子控件
scrollTo和scrollBy:控制ScrollView視圖的位置
使用實例
1、佈局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 橫向滾動條 -->
<!-- <HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbars="none" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="22sp" />
</HorizontalScrollView> -->
<!-- 縱向滾動條 -->
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="向上" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="向下" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp" />
</ScrollView>
</LinearLayout>
2、在Activity中實現滑動監聽、滑動加載、位置跳轉等功能
<pre name="code" class="java">package com.cx.scorollview;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button button1;
private Button button2;
private TextView textView;
private ScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
textView = (TextView) findViewById(R.id.textView1);
scrollView = (ScrollView) findViewById(R.id.scrollView1);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
//這裏是爲textView賦值,內容在R.string.text中,測試時最好內容長一些,這裏不再貼出。
textView.setText(getResources().getString(R.string.text));
scrollView.setOnTouchListener(new OnTouchListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
//手指擡起
case MotionEvent.ACTION_UP:
break;
//手指落下
case MotionEvent.ACTION_DOWN:
break;
//手指滑動
case MotionEvent.ACTION_MOVE:
/**
* 1、getScorollY()——滾動條滑動的距離
* 2、getMeasuredHeight()——內容的整體高度,包括隱藏部分
* 3、getHeight()——顯示高度。內容未佈滿屏幕,2=3;內容大於屏幕,3=屏幕高度,2>3。
*/
//頂部狀態
if(scrollView.getScrollY()<=0){
Log.e(">>>>>>>>>>>>>>", "頂部");
Toast.makeText(MainActivity.this, "頂部", Toast.LENGTH_SHORT).show();
}
//頂部狀態
//TextView的總高度<=一屏幕的高度+滾動條的滾動距離(getChildAt(0):第0個子控件)
if(scrollView.getChildAt(0).getMeasuredHeight()<= scrollView.getScrollY() + scrollView.getHeight()){
Log.e(">>>>>>>>>>>>>>", "底部");
Toast.makeText(MainActivity.this, "底部", Toast.LENGTH_SHORT).show();
//在文本中追加內容
textView.append("111111111111111111111");
}
break;
}
return false;
}
});
}
@Override
public void onClick(View v) {
//scrollTo:以滾動視圖起始位置開始計算的
//scrollBy:相對前一次的位置,滾動相應的距離
switch (v.getId()) {
case R.id.button1:
// scrollView.scrollTo(0, -30);
scrollView.scrollBy(0, -30);
break;
case R.id.button2:
// scrollView.scrollTo(0, -30);
scrollView.scrollBy(0, 30);
break;
}
}
}
源碼下載