ACTION_DOWN,ACTION_UP和onClick事件

时间:2013-06-29 20:25:51

标签: android

我有一个listview,其项目是由2个标签和relativelayout中的按钮组成的自定义视图。

执行此操作时,列表视图按钮的“反馈”点击 - 项目在您触摸时更改了背景颜色 - 消失,所以我决定使用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,即:背景在250ms内从白色变为青色,之后它会产生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只创建了屏幕上的视图,当你向下滚动它时,它会剔除那个熄灭的那个(上面的那个),这将是你数据数组中下一个项目的信息(将是显示下来,作为下一个)。

所以我修改了eventlisteners也更新了他们的引用。代码如下。

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

2 个答案:

答案 0 :(得分:1)

我认为您需要更改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;
}

希望它有所帮助。

答案 1 :(得分:0)

public boolean onTouch(MotionEvent ev) {
 ...
 return super.onTouch(ev);
}

也许你不应该自己回归假。