DOWN, ACTION
I have a listview whose items are a custom views composed by 2 labels and a button in a relativelayout.
When doing this, the 'feedback' of the listview button clicking -the item changed background color while you touch it- dissapears so I decided to do it using ACTION_DOWN and ACTION_UP
I did this class to put in all listviews with the same issue:
// 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;
}
The problem is that the item gets the touch event ACTION_DOWN but not the ACTION_UP, thats: the background changes from white to cyan in 250ms, after of this it makes the onclick event, but it does not do the ACTION_UP...
The onClick does this:
@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);
}
Well: it goes to the next activity but it doesn't put its background back to white... More than this: sometimes it doesn't go to its destiny but the background changes to cyan and is stuck in cyan...
I've read in android documentation that returning 'false' in the ontouch function:
So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.
So if I return true the feedback works but the event is consumed and the onclick doesn't works...
So I don't know what to do to get the 'feedback' of touching a item and the onclick event working....
I could call the onClick inside the ACTION_UP but its very ugly -specially thinking in the 'onLongClick' event-.
Its possible to use the ACTION_DOWN, ACTION_UP to make animations and onClick event to make the logic of the app at once?
How can I restore -and costumize- the feedback of 'pushing a button'?
EDIT TO POST CODE:
Well, there's requested code.
The 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>
And the adapter's code. Note that when I do this:
cvh.updateCustomOnClickBases(titActual, context,posicion);
cvh.updateOnTouchListeners();
I do it because the listView only creates the views that are on screen and when you scroll down it recicles the one that goes out (the upper one) whit the info of the next item in your array of data (that will be shown down, as the next one).
So I recicle the eventlisteners too updating their references. The code is below.
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);
}
}
With this holder and clickbase I avoid object creations (I build a event listeners of the listview's items that are updated instead of create new listeners)
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);
}
}
}
}
And
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;
}
}
I think you need to change your onTouch() method
@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;
}
Hope it helps.
public boolean onTouch(MotionEvent ev) {
...
return super.onTouch(ev);
}
也许你不应该自己回来。
链接地址: http://www.djcxy.com/p/8932.html下一篇: 向下,行动