當前位置:首頁 » 安卓系統 » android滾動監聽

android滾動監聽

發布時間: 2023-08-22 22:12:54

A. 在android系統中,那些被監聽對象有哪些

在android系統中,那些被監聽對象有哪些:1、單擊事件(View.OnClickListener):當用戶觸碰到某個組件或者方向鍵被按下時產生該事件,該事件的處理方法是onClick()。

2、焦點事件(View.OnFocusChangeListener):組件得到或者失去焦點時產生該事件,事件處理方法是onFocusChange()。

3、按鍵事件(View.OnKey Listener):用戶按下或者釋放設備上的某個按鍵時產生,事件處理方法是 onKey()。

4、觸碰事件(View.OnTouchListener):設備具有觸摸屏功能時,觸碰屏幕產生該事件。事件處理方法是onTouch()。

5、創建上下文菜單事件(View.OnCreateContextMenu Listener):創建上下文菜單時產生該事件,事件處理方法是 onCreateContextMenu()。

B. android fragment怎麼監聽上下滑動

在Android應用中,經常需要手勢滑動操作,比如上下滑動,或左右方向滑動,處理手勢滑動 。通常有兩種方法:一種是單獨實現setOnTouchListener監聽器來,另一種是構建手勢探測器

第一種方法,就是在要實現滑動的View中,實現OnTouchListener監聽事件,然後判斷KeyDonw和KeyUp 直接的位置距離來判斷滑動方向,核心實現代碼如下:

/**
* 設置上下滑動作監聽器
* @author jczmdeveloper
*/
private void setGestureListener(){
myView.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:
mPosX = event.getX();
mPosY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
mCurPosX = event.getX();
mCurPosY = event.getY();

break;
case MotionEvent.ACTION_UP:
if (mCurPosY - mPosY > 0
&& (Math.abs(mCurPosY - mPosY) > 25)) {
//向下滑動

} else if (mCurPosY - mPosY < 0
&& (Math.abs(mCurPosY - mPosY) > 25)) {
//向上滑動
collapse();
}

break;
}
return true;
}

});
}

第二種方法:就是構建手勢探測器,如GestureDetector mygesture = new GestureDetector(this);,然後在onFling方法中根據MotionEvent的兩個參數的 按下和滑動以及放開的位置和距離來判斷滑動方向以及滑動速度等的。要構建GestureDetector,必須要和OnTouchListener一起使用,因為必須設置Touch監聽,核心實現實例如下:

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class TagScrollView extends FrameLayout implements OnTouchListener, OnGestureListener{
private float mPosX, mPosY, mCurPosX, mCurPosY;
private static final int FLING_MIN_DISTANCE = 20;// 移動最小距離
private static final int FLING_MIN_VELOCITY = 200;// 移動最大速度
//構建手勢探測器
GestureDetector mygesture = new GestureDetector(this);

public TagScrollView(Context context) {
this(context, null);
}

public TagScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public TagScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

//setGestureListener();
//設置Touch監聽
this.setOnTouchListener(this);
//允許長按
this.setLongClickable(true);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return mygesture.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}

@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
// e1:第1個ACTION_DOWN MotionEvent
// e2:最後一個ACTION_MOVE MotionEvent
// velocityX:X軸上的移動速度(像素/秒)
// velocityY:Y軸上的移動速度(像素/秒)

// X軸的坐標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個像素/秒
//向
if (e1.getY() - e2.getY() > FLING_MIN_DISTANCE){
// && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
collapse();
}
//向上
if (e2.getY() - e1.getY() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {

}
return false;
}

}


C. 怎麼判斷android中ScrollView滑動到了最底部

滾動到頂部判斷:
getScrollY() == 0
滾動到底部判斷:
View childView = getChildAt(0);
childView.getMeasuredHeight() <= getScrollY() + getHeight();
其中getChildAt表示得到ScrollView的child View
childView.getMeasuredHeight()表示得到子View的高度,
getScrollY()表示得到y軸的滾動距離,
getHeight()為scrollView的高度
getScrollY()達到最大時加上scrollView的高度就的就等於它內容的高度了.
判斷滑動位置的地方,可以有兩種方式:
1、實現OnTouchListener來監聽是否滑動到最底部
OnTouchListener onTouchListener=new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (childView != null && childView .getMeasuredHeight()< = getScrollY() + getHeight()) {
} else if (getScrollY() == 0) {
}
break;
}
return false;
}
}
2、重寫ScrollView的onScrollChanged的方法,在onScrollChanged函數中判斷
public class myScrollView extends ScrollView
{
public myScrollView(Context context)
{
super(context);
}
public myScrollView(Context context, AttributeSet attributeSet)
{
super(context,attributeSet);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt)
{
View view = (View)getChildAt(getChildCount()-1);
int d = view.getBottom();
d -= (getHeight()+getScrollY());
if(d==0)
{
//you are at the end of the list in scrollview
//do what you wanna do here
}
else
super.onScrollChanged(l,t,oldl,oldt);
}
}

D. Android的對話框怎麼監聽觸屏事件

用我這個, 我己經給你寫好了,你可以稍做修改就能用,
title是對話框的標題
icon是resID,是一張圖片的ID,放在你res目錄下的drawable
okcmd和cancelcmd分別是確認和取消按鈕的字元串

//確認對話框的呼出
public void showDialog(String title,int icon,String msg,String okcmd,String cancelCmd){
if(okcmd==null){return;}
Log.i("setDialog", "dialogTitle="+title
+" dialogContent"+msg+
" dialogCmdOk"+okcmd
+" dialogCmdCancel"+cancelCmd
+" dialogIcon"+icon);

Builder builder=new AlertDialog.Builder(this);
builder.setTitle(title);
if(icon>0){
builder.setIcon(icon);
}else{
builder.setIcon(R.drawable.icon);
}
builder.setMessage(msg);
builder.setPositiveButton(okcmd,
new
android.content.DialogInterface.OnClickListener(){
public void onClick(DialogInterface d, int i) {
Log.i("showDialog", "onClick");
}
});
if(cancelCmd!=null && cancelCmd.length()>0){
builder.setNeutralButton(cancelCmd,
new android.content.DialogInterface.OnClickListener(){
public void onClick(DialogInterface d, int i) { }
});
}

AlertDialog dialog =builder.create();
dialog.show();
}

E. 安卓scrollview滑動監聽高度防止抖動

安卓scrollview滑動監聽高度防止抖動步驟如攔簡下:
1、創建一個介面,創建一個類繼承ScrollView。
2、設置變化李激時哪衡襪的顏色,去掉Scrollview滾動條:加上這條屬性,去掉陰影即可防止抖動。

F. android怎麼動態監聽arraylist集合的變化

先把 datagridview.datasource = null;設置數據源為空,然後再重新綁一次數據 datagridview.datasource = ArrayList對象。

G. 如何監聽android的屏幕滑動停止事件

android的屏幕滑動停止事件監聽方法為:
1、自定義一個ScrollView,內容只要將標準的ScrollView 拷貝出來,然後就可以利用Scroller的兩個屬性去判斷是否滾動完畢:
public final boolean isFinished() {
return mFinished;
}
public final int getDuration() {
return mDuration;
}
2、啟動線程來獲得當前的scrollview的高度,並實時更新就可以了,一旦高度不再發生變化則判斷為停止。

H. Android 手機軟鍵盤的彈起和關閉的監聽

       在很多Android App 開發的過程中,需要對Activity 中 軟鍵盤的彈起和關閉進項監聽,但是Andoid中並沒有提供相對應的api進行監聽, 我有一個簡單的方法。

首先需要知道一些基礎知識

在manifest文件中可以設置Activity的android:windowSoftInputMode屬性,這個屬性值常見的設置如下:android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

那麼這里值的含義列表如下:

1、stateUnspecified:軟鍵盤的狀態並沒有指定,系統將選擇一個合適的狀態或依賴於主題的設置

2、stateUnchanged:當這個activity出現時,軟鍵盤將一直保持在上一個activity里的狀態,無論是隱藏還是顯示

3、stateHidden:用戶選擇activity時,軟鍵盤總是被隱藏

4、stateAlwaysHidden:當該Activity主窗口獲取焦點時,軟鍵盤也總是被隱藏的

5、stateVisible:軟鍵盤通常是可見的

6、stateAlwaysVisible:用戶選擇activity時,軟鍵盤總是顯示的狀態

7、adjustUnspecified:默認設置,通常由系統自行決定是隱藏還是顯示

8、adjustResize:該Activity總是調整屏幕的大小以便留出軟鍵盤的空間

9、adjustPan:當前窗口的內容將自動移動以便當前焦點從不被鍵盤覆蓋和用戶能總是看到輸入內容的部分

案例:

1.我們需要將監聽所在的Activity在Manifest文件中的設置為如下形式:

<activity 

android:name="com.zy.project.MainActivity"

android:label="@string/app_name"

android:windowSoftInputMode="stateAlwaysHidden|adjustResize" >

<intent-filter>

         <action android:name="android.intent.action.MAIN/>

        <category android:name="android.intent.category.LAUNCHER/>

</intent-filter>

</activity>

當有軟鍵盤彈起時,Activity的布局大小會被滾動上去,但是你仍然可以通過滑動瀏覽所有。

2 需要在外層布局文件設置一個id,並在activity 中設置監聽

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnLayoutChangeListener;

import android.widget.Toast;

public class MainActivity extends Activity implements OnLayoutChangeListener{

//Activity最外層的Layout視圖

private View rootView;

//屏幕高度

private int screenHeight = 0;

//軟體盤彈起後所佔高度閥值 一般是佔用屏幕的1/3

private int keyHeight = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

rootView = findViewById(R.id.root_layout);

//獲取屏幕高度

screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();

//閥值設置為屏幕高度的1/3

keyHeight = screenHeight/3;

}

@Override

protected void onResume() {

super.onResume();

//添加layout大小發生改變監聽器

rootView.addOnLayoutChangeListener(this);

}

@Override

public void onLayoutChange(View v, int left, int top, int right,int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

//old是改變前的左上右下坐標點值,沒有old的是改變後的左上右下坐標點值

// System.out.println(oldLeft + " " + oldTop +" " + oldRight + " " + oldBottom);

// System.out.println(left + " " + top +" " + right + " " + bottom);

//現在認為只要控制項將Activity向上推的高度超過了1/3屏幕高,就認為軟鍵盤彈起

if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){

Toast.makeText(MainActivity.this, "監聽到軟鍵盤彈起...", Toast.LENGTH_SHORT).show();

}else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){

Toast.makeText(MainActivity.this, "監聽到軟體盤關閉...", Toast.LENGTH_SHORT).show();

}

}

本文來自:http://m.blog.csdn.net/bear_huangzhen/article/details/45896333

I. android recyclerview滑動監聽滑過多少條

1.利用OnScrollListener[java]viewplainmRecyclerView.addOnScrollListener(newRecyclerView.OnScrollListener(){privateinttotalDy=0;@OverridepublicvoidonScrolled(RecyclerViewrecyclerView,intdx,intdy){totalDy-=dy;}});如代碼所述,totalDy的確保存了RecyclerView的滑動距離,但是當我向下滑動RecyclerView,之後插入/刪除/移動Item的時候,totalDy就變得不精確了;比如刪除或者插入新的Item,那麼totalDy就不能再回歸到0了。解決法:監聽RecyclerView的addOnScrollListener後自己記錄onScrolled的dy,同時給adapter加個registerAdapterDataObserver,監聽插入/刪除/移動,自己加減前面記錄的dy滾動值。自己沒有試過,不知道是否可行!2.有人說可以嘗試computeVerticalScrollOffset()[java]viewplaintotalDy=recyclerView.computeVerticalScrollOffset();然而compute方法計算出的並不是滑動的精確距離,stackOverflow上有答案解釋其為item的平均高度*可見item數目,不是我們需要的精確距離。3.還有人說可以嘗試getChildAt(0)[java]viewplaintotalDy=recyclerView.getChildAt(0).getTop();依靠第一個item的滑動距離來進行動畫的設置,但是根據該方法得出的totalDy在滑動到一定程度後清零。這是因為recyclerViewl.getChildAt(0)返回的永遠是第一個可見的child,不是所有viewlist的第一個child,因此這種用法是得不到滑動距離的。另外下面這三種用法都是等價的,都是獲取第一個可見的child:[java]=(LinearLayoutManager)this.getLayoutManager();ViewfirstVisiableChildView=this.getChildAt(0);ViewfirstVisiableChildView=layoutManager.getChildAt(0)intposition=layoutManager.findFirstVisibleItemPosition();ViewfirstVisiableChildView=layoutManager.getChildAt(position)但是下面這種就不是獲取第一個可見的child,而是獲得所有viewlist的第一個child。但是滑動一段距離後它總是返回null,即第一個child被recycle後,總是返回null。[java]viewplain//Don',.=(LinearLayoutManager)this.getLayoutManager();Viewchild2=layoutManager.findViewByPosition(0);4.如果LayoutManager用的是LinearLayoutManager,強烈推薦下面的方法獲取滑動距離:[java](){=(LinearLayoutManager)this.getLayoutManager();intposition=layoutManager.findFirstVisibleItemPosition();ViewfirstVisiableChildView=layoutManager.findViewByPosition(position);intitemHeight=firstVisiableChildView.getHeight();return(position)*itemHeight-firstVisiableChildView.getTop();}

熱點內容
滑板鞋腳本視頻 發布:2025-02-02 09:48:54 瀏覽:432
群暉怎麼玩安卓模擬器 發布:2025-02-02 09:45:23 瀏覽:557
三星安卓12彩蛋怎麼玩 發布:2025-02-02 09:44:39 瀏覽:743
電腦顯示連接伺服器錯誤 發布:2025-02-02 09:24:10 瀏覽:537
瑞芯微開發板編譯 發布:2025-02-02 09:22:54 瀏覽:146
linux虛擬機用gcc編譯時顯示錯誤 發布:2025-02-02 09:14:01 瀏覽:233
java駝峰 發布:2025-02-02 09:13:26 瀏覽:651
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:532
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:212
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:726