![](/img/trans.png)
[英]Android ACTION_DOWN event automatically executes ACTION_UP event after few seconds
[英]ACTION_DOWN, ACTION_UP and onClick event
我有一個listview,其項目是由2個標簽和一個相對布局中的按鈕組成的自定義視圖。
執行此操作時,單擊列表視圖按鈕的“反饋”-該項目在您觸摸時更改了背景色-消失了,所以我決定使用ACTION_DOWN和ACTION_UP
我做了這個課程來放入所有具有相同問題的列表視圖:
// The same instance of this class is setted as onTouchListener to the labels and the layout
public class OnTouchChangeColor implements OnTouchListener {
TransitionDrawable transition;
private final int duration = 250;
public static final int INITCOLOR = Color.WHITE;
public static final int FINALCOLOR = Color.CYAN;
// this will be the layout container of the labels and the button
ViewGroup layout = null;
public OnTouchChangeColor(ViewGroup layout){
update(layout);
}
public void update(ViewGroup layout){
this.layout = layout;
TransitionDrawable t = new TransitionDrawable(new Drawable[]{new ColorDrawable(INITCOLOR), new ColorDrawable(FINALCOLOR)});
layout.setBackgroundDrawable(t);
transition = (TransitionDrawable) layout.getBackground();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
transition.startTransition(duration);
break;
case MotionEvent.ACTION_UP:
transition.reverseTransition(duration);
break;
}
// tell the system that we handled the event but a further processing is required
return false;
}
問題在於,該項目獲得了觸摸事件ACTION_DOWN但沒有獲得ACTION_UP,那就是:背景在250毫秒內從白色變為青色,此后將產生onclick事件,但不會執行ACTION_UP ...
onClick會執行以下操作:
@Override
public void onClick(View v) {
try {
loadData();
} catch (IOException e) {
e.printStackTrace();
}
Intent intent = new Intent(context, ActDestiny.class);
intent.putExtra("stop", true);
context.startActivity(intent);
}
好吧:它進入下一個活動,但它不會使背景變回白色...不僅如此:有時它並沒有達到預定的目的,但是背景變成了青色,並停留在青色中...
我已經閱讀了Android文檔,該文檔在ontouch函數中返回“ false”:
因此,如果在收到按下動作事件時返回false,則表明您尚未消耗該事件,並且也對該事件的后續動作不感興趣。 因此, 不會在事件內要求您進行任何其他操作,例如手指手勢或最終的上動作事件。
因此,如果我返回true,則反饋有效,但事件已消耗且onclick不起作用...
所以我不知道該怎么做才能使觸摸項目的“反饋”和onclick事件正常工作。
我可以在ACTION_UP內調用onClick,但是它非常難看-特別是在“ onLongClick”事件中的思考-。
是否可以使用ACTION_DOWN,ACTION_UP制作動畫以及使用onClick事件立即制作應用邏輯?
如何恢復並匯總“按下按鈕”的反饋?
編輯郵編:
好吧,這里有要求的代碼。
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_titular_mp3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/LblTitulo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/btnAccion"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/LblSubTitulo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/LblTitulo"
android:layout_toLeftOf="@id/btnAccion"
android:text=" "
android:textSize="12dp"
android:textStyle="normal" />
<Button
android:id="@+id/btnAccion"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/playbutton_style"
android:gravity="center_vertical|center_horizontal|right"
android:visibility="gone" />
</RelativeLayout>
以及適配器的代碼。 請注意,當我這樣做時:
cvh.updateCustomOnClickBases(titActual, context,posicion);
cvh.updateOnTouchListeners();
我這樣做是因為listView僅創建屏幕上的視圖,當您向下滾動它時,記錄下來的(上面的)記錄了數據數組中下一項的信息(將顯示在屏幕上,作為下一個)。
因此,我記錄了事件偵聽器也更新了它們的引用。 代碼如下。
public class AdaptPlayList extends BaseAdapter {
private final Context context;
ArrayList<PlayList> datos;
long id;
public AdaptPlayList(Context context, ArrayList<PlayList> datos, int typ) {
this.datos = datos;
this.context = context;
}
public void updatePlaylist(ArrayList<PlayList> pl){
ThreadPreconditions.checkOnMainThread();
this.datos = pl;
notifyDataSetChanged();
}
@Override
public int getCount(){
return datos.size();
}
@Override
public PlayList getItem(int index) {
return datos.get(index);
}
@Override
public long getItemId(int index) {
return index;
}
public View getView(int posicion, View convertView, ViewGroup parent) {
final PlayList titActual = getItem(posicion);
CancionViewHolder cvh;
if (convertView == null) {
cvh = new CancionViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.titularmp3, parent, false);
OnPlaylistItemClick itemClick = new OnPlaylistItemClick(titActual, context,posicion);
cvh.titulo = (TextView) convertView.findViewById(R.id.LblTitulo);
cvh.btnAction = (Button) convertView.findViewById(R.id.btnAccion);
cvh.layout = (ViewGroup)convertView.findViewById(R.id.layout_titular_mp3);
cvh.click = itemClick;
cvh.longClick = itemClick;
cvh.btnClick = new OnPlaylistButtonClick(titActual, context,posicion);
convertView.setTag(cvh);
}else{
cvh = (CancionViewHolder)convertView.getTag();
}
cvh.updateCustomOnClickBases(titActual, context,posicion);
cvh.updateOnTouchListeners();
TextView titulo = cvh.titulo;
Button btnAction = cvh.btnAction;
titulo.setText(titActual.getDesc());
btnAction.setVisibility(View.VISIBLE);
btnAction.setOnClickListener(cvh.btnClick);
titulo.setOnClickListener(cvh.click);
titulo.setOnLongClickListener(cvh.longClick);
return convertView;
}
}
class OnPlaylistItemClick extends CustomOnClickBase implements OnClickListener, OnLongClickListener{
public OnPlaylistItemClick(PlayList pl, Context ctx, int position) {
super(pl, ctx, position);
}
@Override
public boolean onLongClick(View v) {
// do things....
//
Intent intent = new Intent(context, ActListadoCancionesAsync.class);
intent.putExtra("stopMusic", true);
context.startActivity(intent);
return true;
}
@Override
public void onClick(View v) {
// do more things!
}
}
}
class OnPlaylistButtonClick extends CustomOnClickBase implements OnClickListener{
PlayList titActual;
public OnPlaylistButtonClick(PlayList pl, Context ctx, int position) {
super(pl, ctx, position);
titActual = pl;
}
@Override
public void onClick(View v) {
// do things
//....
Intent intent = new Intent(context, ActListadoCancionesAsync.class);
intent.putExtra("stopMusic", true);
context.startActivity(intent);
}
}
有了這個持有人和clickbase,我避免了對象的創建(我創建了一個被更新的listview項的事件監聽器,而不是創建新的監聽器)
public class CancionViewHolder{
public TextView titulo;
public TextView subtitulo;
public ToggleButton button;
public Button btnAction;
public OnClickListener btnClick;
public OnClickListener click;
public OnLongClickListener longClick;
public ViewGroup layout = null;
/**Actualiza los eventos que estan cacheados para que apunten a sus nuevos contenidos del adapter. De otro modo, como los
* datos del adapter se moveran mientras que los views seran reutilizados los eventos apuntarian a la anterior posicion
* @param datosItem
* @param ctx
* @param pos
*/
public void updateCustomOnClickBases(Object datosItem, Context ctx, int pos){
((CustomOnClickBase)click).updateObject(datosItem, ctx,pos, layout);
((CustomOnClickBase)longClick).updateObject(datosItem, ctx,pos, layout);
((CustomOnClickBase)btnClick).updateObject(datosItem, ctx,pos, layout);
}
/**
* Establece los listeners que hacen efectos cuando se pulsa algo
*/
public void updateOnTouchListeners() {
if (layout != null) {
OnTouchChangeColor cc = new OnTouchChangeColor(layout);
layout.setOnTouchListener(cc);
if (subtitulo != null){
subtitulo.setOnTouchListener(cc);
}
if (titulo != null){
titulo.setOnTouchListener(cc);
}
}
}
}
和
public abstract class CustomOnClickBase {
protected Object datosItem;
protected Context context;
protected int position;
protected ViewGroup layout;
public CustomOnClickBase(Object datosItem, Context ctx, int position){
updateObject(datosItem, ctx, position, layout);
}
public void updateObject(Object datosItem, Context ctx, int position, ViewGroup layout){
this.datosItem = datosItem;
context =ctx;
this.position = position;
this.layout = layout;
}
}
我認為您需要更改onTouch()方法
@Override
public boolean onTouch(View v, MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
transition.startTransition(duration);
//Tell Android that you can handle this MotionEvent, and you
//want to keep informed of further events of this touch
return true;
break;
case MotionEvent.ACTION_UP:
transition.reverseTransition(duration);
break;
}
// tell the system that we handled the event but a further processing is required
return false;
}
希望能幫助到你。
public boolean onTouch(MotionEvent ev) {
...
return super.onTouch(ev);
}
也許您不應該自己返回false。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.