0,y=0時,向右移動x像素,當x<0,y=0時,向左移動x像素,而View的大小和位置不發(fā)生改變" />

黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

View.scrollBy()與View.scrollTo()的使用

系統(tǒng) 1951 0

原帖: http://ipjmc.iteye.com/blog/1307565


scrollTo()和scrollBy()都是View的public成員函數(shù),使用這兩個函數(shù)可以達到同樣的目的,只是使用方式不同。
public void scrollBy (int x, int y),將View的Content偏移(x,y)。x控制左右方向的偏移,y控制上下方向的偏移。例如當x>0,y=0時,向右移動x像素,當x<0,y=0時,向左移動x像素,而View的大小和位置不發(fā)生改變。如果Content超出了View的范圍,則超出的部分會被擋住。
public void scrollTo (int x, int y),將View的Content的位置移動到(x,y),而View的大小和位置不發(fā)生改變。如果Content超出了View的范圍,則超出的部分會被擋住。

舉個例子說明情況,這個例子有兩個控件可以Scroll,一個是TextView,一個是ListView,另外用兩個Button控制scroll的方向(向上或向下)

View.scrollBy()與View.scrollTo()的使用

main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView android:id="@+id/text"
        android:text="@string/hello" android:layout_marginBottom="100dip"
        android:layout_width="fill_parent" android:layout_height="60dp"
        android:gravity="center" />
    
    <Button android:id="@+id/up" android:text="UP"
	    android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    
    <Button android:id="@+id/down" android:text="DOWN"
	    android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    
    <ListView android:id="@+id/list"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:cacheColorHint="#00000000" android:entries="@array/names"/>
</LinearLayout>
  

    package com.ipjmc.demo.scroll;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ScrollActivity extends Activity implements View.OnClickListener{
	
	private static final String TAG = "Hello";
	private Button mUp, mDown;
	private TextView mTextView;
	private ListView mListView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mTextView = (TextView) findViewById(R.id.text);
		mUp = (Button) findViewById(R.id.up);
		mDown = (Button) findViewById(R.id.down);
		mListView = (ListView) findViewById(R.id.list);

                //預(yù)先向下滾動到50,在android2.2上,可以用手指拖動一下ListView感受一下效果
		mListView.scrollTo(0, 50);
		
		mUp.setOnClickListener(this);
		mDown.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.up: //向上滾動
			mTextView.scrollBy(0, 10);
			mListView.scrollBy(0, 10);
			Log.i(TAG, "mTextView.getScrollY() = " + mTextView.getScrollY()
				   + ", mListView.getBaseline() = " + mListView.getScrollY());

			break;
		case R.id.down://向下滾動
			mTextView.scrollBy(0, -10);
			mListView.scrollBy(0, -10);
			
			Log.i(TAG, "mTextView.getScrollY() = " + mTextView.getScrollY()
					   + ", mListView.getBaseline() = " + mListView.getScrollY());
			break;
		default:
			break;
		}
	}
}
  

實驗的結(jié)果是:調(diào)用scrollTo()和scrollBy()可以到達預(yù)想的效果,但是因為用手指在ListView上拖動時,ListView也會滾動,貌似和scrollTo()/scrollBy()沖突,在android2.2和4.0上都有問題。 所以最好不要在ListView上調(diào)用這兩個函數(shù),以免出現(xiàn)莫名奇妙的問題。同樣也不能通過getScrollY()獲取ListView滾動的位置。

View.scrollBy()與View.scrollTo()的使用


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論