带有onTouchEvent的TextView

时间:2012-03-02 00:26:03

标签: android get textview element ontouchevent

我在ListView中使用自定义TextView时遇到问题。 我重写了onTouchEvent,但是我没有找到获取元素的方法,我的情况就是对象Tarea。 我需要当用户向右滑动时onTouchEvent捕获此TextView的ID和文本(在Listview中)。

这是我的代码:

public class TextViewFinger extends TextView {

float historicX = Float.NaN;
float historicY = Float.NaN;
static final float TRIGGER_DELTA = 150;

public TextViewFinger(Context context){
    super(context);
}
public TextViewFinger(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}
public TextViewFinger(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}


@Override public boolean onTouchEvent(MotionEvent e){

     switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            historicX = e.getX();
            historicY = e.getY();
            break;
        case MotionEvent.ACTION_UP:
             if ((e.getX() - historicX) > TRIGGER_DELTA)  {
                 onSlideComplete(Direction.RIGHT);

                return false;
            } 
             else if ((e.getX() - historicX) < -TRIGGER_DELTA) {

                onSlideComplete(Direction.LEFT);
               return false;
            }
            break;
        default:
            return super.onTouchEvent(e);
        }
     return super.onTouchEvent(e);
}

enum Direction {
    LEFT, RIGHT;

    }

 public void onSlideComplete(Direction dir){
     if(dir==Direction.RIGHT){
         this.setBackgroundColor(Color.GREEN);
         this.setText("Hecho" + ": " +  this.getText());

        // this.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_left));
         //this.setTextColor(color.white);
        //Toast.makeText(getContext(), "Derecha", 1).show();
     }
     if(dir==Direction.LEFT){
         this.setBackgroundColor(Color.RED);
         this.setText("Borrar" + ": " +  this.getText());


         //Toast.makeText(getContext(), "Izquierda", 1).show();
     }

    // Toast.makeText(getContext(), "Derecha", 5).show();
 }

}



class TareasAdapter extends ArrayAdapter<Tarea>{
private ArrayList<Tarea> tareas;

public TareasAdapter(Context context, int textViewResourceId,
        ArrayList<Tarea> objects) {

    super(context, textViewResourceId, objects);
    this.tareas=objects;
    // TODO Auto-generated constructor stub
}

public View getView(int position, View convertView, ViewGroup parent ){
    View v = convertView;


    if (v == null){
        LayoutInflater vi=(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v= vi.inflate(R.layout.list_item, null);
    }
    Tarea t = tareas.get(position);
    if(t != null){
        TextView txtTarea= (TextView) v.findViewById(R.id.txtNotaCustomID);

        txtTarea.setText(t.getTarea_description());
        txtTarea.setBackgroundColor(Color.BLACK);
        txtTarea.setClickable(true);
        //txtTarea.setId((int) t.getId());
        txtTarea.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            //int t= (Integer) v.getTag();

               Toast.makeText(getContext(),"f",4).show();


            }
        });
    }
    //LayoutInflater li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //li.inflate(layout.list_item, null);

    return v;

}
public int getCount(){
    return tareas.size();
}
public Tarea getItem(int position){
    return this.tareas.get(position);

}
public long getItemId(int position){
    return position;
}


}

 public class Tarea {

private int id;
private String tarea_description;
private Integer urgencia;

public double getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getTarea_description() {
    return tarea_description;
}
public void setTarea_description(String tarea_description) {
    this.tarea_description = tarea_description;
}
public Integer getUrgencia() {
    return urgencia;
}
public void setUrgencia(Integer urgencia) {
    this.urgencia = urgencia;
}


}

提前致谢, 奥利弗。

2 个答案:

答案 0 :(得分:1)

你试过吗

int id = TextViewFlinger.this.getId();

来自您的触摸事件?

答案 1 :(得分:0)

有几种方法可以解决您的问题。一种方法是创建一个定义两个方法的接口,比如onRightSwipe()和onLeftSwipe();然后,在适配器中提供此侦听器(向TextView实现添加setter方法),并在检测到向右/向左滑动时调用它。但最好的实现也取决于具体的用例。您可以在安装Android之后对此类侦听器进行建模,例如OnScrollListener