ScrollView的使用详细

来源:互联网 发布:九品网络电视官方下载 编辑:程序博客网 时间:2024/06/12 01:10

滚动视图(ScrollView)是指当拥有很多内容,屏幕显示不完时,需要通过滚动来显示完整的视图。包括水平滚动视图(HorizontalScrollView)和垂直滚动视图(ScrollView)

隐藏滚动条
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视图的位置

<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>  
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;          }      }  }  

HorizontalScrollView的简单用法

HorizontalScrollView其实是FrameLayout的子类,所以内部只能有一个直接的子View。我们用来做Gallery效果,首选当然是LinearLayout,然后方向设置为水平。
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"       >      <HorizontalScrollView          android:layout_width="wrap_content"          android:layout_height="150dp"          android:layout_gravity="center_vertical"          android:background="#AA444444"          android:scrollbars="none" >          <LinearLayout              android:id="@+id/id_gallery"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_gravity="center_vertical"              android:orientation="horizontal" >          </LinearLayout>      </HorizontalScrollView>  </LinearLayout>  

很简单,就一个HorizontalScrollView内部有个水平方向的LinearLayout
MainActivity:

public class MainActivity extends Activity  {      private LinearLayout mGallery;      private int[] mImgIds;      private LayoutInflater mInflater;      @Override      protected void onCreate(Bundle savedInstanceState)      {          super.onCreate(savedInstanceState);          requestWindowFeature(Window.FEATURE_NO_TITLE);          setContentView(R.layout.activity_main);          mInflater = LayoutInflater.from(this);          initData();          initView();      }      private void initData()      {          mImgIds = new int[] { R.drawable.a, R.drawable.b, R.drawable.c,                  R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g,                  R.drawable.h, R.drawable.l };      }      private void initView()      {          mGallery = (LinearLayout) findViewById(R.id.id_gallery);          for (int i = 0; i < mImgIds.length; i++)          {              View view = mInflater.inflate(R.layout.activity_index_gallery_item,                      mGallery, false);              ImageView img = (ImageView) view                      .findViewById(R.id.id_index_gallery_item_image);              img.setImageResource(mImgIds[i]);              TextView txt = (TextView) view                      .findViewById(R.id.id_index_gallery_item_text);              txt.setText("some info ");              mGallery.addView(view);          }      }  }  

很简单,我预先准备了一些图片直接放在了Drawble下,然后循环加入HorizontalScrollView的LinearLayout中即可。

0 0