運用手勢操作ListView中的Item

2021-06-17 19:18:51 字數 1960 閱讀 8207

文章出處:

需要實現的功能是:用手指在某條記錄上從左往右劃一下,出現一條橫線,用於表示這個菜已經上過了;倒過來劃,取消這條橫線,表示這個菜沒上過。

如何實現呢?

看我的吧!

首先需要乙個item的布局,在布局的最上方有乙個imageview用於顯示這條橫線,如果沒有就把這個imageview背景設定成透明,這個imageview同這條記錄等寬等高!

布局如下:

然後就是寫listview了:

listview = (listview) findviewbyid(r.id.listview);

listview.setonitemclicklistener(onitemclicklistener);

listview.setontouchlistener(new view.ontouchlistener()

});

實現手勢:

mdetector=new gesturedetector(new gesturedetector.ongesturelistener() 

@override

public void onshowpress(motionevent e)

@override

public boolean onscroll(motionevent e1, motionevent e2, float distancex,

float distancey)

@override

public void onlongpress(motionevent e)

@override

public boolean onfling(motionevent e1, motionevent e2, float velocityx,

float velocityy) }}

}else if(e1.getx() - e2.getx() > fling_min_distance && math.abs(velocityx) > fling_min_velocity)}}

}return false;

}@override

public boolean ondown(motionevent e)

});};

再下來就要實現adapter,提供一段偽**:

class detailadapter extends baseadapter 

@override

public object getitem(int position)

@override

public long getitemid(int position)

@override

public view getview(int position, view convertview, viewgroup parent) else

iv_line.settag(instance);

iv_line.setontouchlistener(new view.ontouchlistener()

});return convertview;

} }

這裡對iv_line也要實現ontouchlistener,並且view是個全域性變數,需要在手勢當中判斷一下if(view!=null),確保程式沒問題,並且獲得物件view.gettag()。最後一定要返回false,確保listview的ontouchlistener能夠繼續下去!

整個觸控流程是:

先imageview中的ontouch起作用-->在listview中的ontouch起作用-->手勢

iphone開發中的手勢操作 Swipes

void touchesbegan nsset touches withevent uievent event void touchesmoved nsset touches withevent uievent event else if deltay kminimumgesturelength d...

在安卓開發中需要運用手機振動的功能

第一步 宣告程式許可權 必須在androidmanifest.xml檔案中指定許可權。注 在檔案末尾的標識上單加一行,寫下如上 即可 第二步 獲取vibrator物件 vibrator vibrator vibrator getsystemservice context.vibrator servi...

C 中關於ListView控制項的一些操作實現

幾個關於c 中對listview控制項操作的具體實現方法,主要包括了列表顯示 大圖顯示 專案選中 右鍵浮動選單等,下面是例項的執行圖 以下是具體實現 新增列 set to details view.listview1.view view.details add a column with width...