ListViewのカスタムTextViewに問題があります。 私はonTouchEventをオーバーライドしますが、要素を取得する方法が見つかりませんでした。私の場合はオブジェクトTareaです。 onTouchEventを使って右にスライドすると、このTextViewのIDとテキストがキャプチャされます(リストビュー内)。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;
}
}
おかげで、 オリバー。