向下,行动
我有一个列表视图,其中的项目是由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,即:背景在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,则表示您没有使用该事件,并且对此事件的后续操作也不感兴趣。 因此, 您不会被要求在事件中执行任何其他操作,例如手指手势或最终上行动作事件。
所以,如果我回到真实的反馈作品,但事件消耗和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);
}
也许你不应该自己回来。
链接地址: http://www.djcxy.com/p/8931.html上一篇: DOWN, ACTION