2012-03-02 3 views
0

ListView에서 내 사용자 지정 TextView에 문제가 있습니다. onTouchEvent를 오버라이드하지만 요소를 가져 오는 방법을 찾지 못했습니다. 내 경우에는 Tarea 개체입니다. onTouchEvent가이 TextView의 ID와 텍스트를 캡처하여 사용자가 오른쪽으로 슬라이드 할 때 필요합니다 (Listview에서). 사전에onTouchEvent가있는 TextView

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


} 

감사합니다, 올리버 :

내 코드입니다.

답변

1

당신이 당신의 터치 이벤트 내에서

int id = TextViewFlinger.this.getId(); 

을 시도 했습니까?

0

문제를 해결하는 데는 몇 가지 방법이 있습니다. 한 가지 방법은 onRightSwipe() 및 onLeftSwipe()와 같은 두 가지 메서드를 정의하는 인터페이스를 만드는 것입니다. 이 리스너를 어댑터에 제공하고 (TextView 구현에 setter 메서드 추가) 오른쪽/왼쪽 스 와이프를 감지 할 때이 리스너를 호출합니다. 그러나 최상의 구현은 특정 사용 사례에도 달려 있습니다. 안드로이드 중 하나 다음에이 청취자를 모델링 할 수 있습니다 (예 : OnScrollListener).