Ontouchlistener For Multiple Textviews Inside Tablerow
Solution 1:
Here's the code I tried myself:
The layout is just a TableLayout with 3 TableRows, and each TableRow contains 3 TextViews with ids like t1, t2, .., t9
And the activity:
publicclassImgActivityextendsActivity {
protected Map<View, Rect> cells = newHashMap<View, Rect>();
protectedboolean hasCoordinatesPopulated;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
finalTableLayouttable= (TableLayout) findViewById(R.id.table);
table.getViewTreeObserver().addOnGlobalLayoutListener(newOnGlobalLayoutListener() {
@OverridepublicvoidonGlobalLayout() {
if (!hasCoordinatesPopulated) {
Viewview= table.findViewById(R.id.t1);
Rectrect= getRawCoordinatesRect(view);
cells.put(view, rect);
view = table.findViewById(R.id.t2);
rect = getRawCoordinatesRect(view);
cells.put(view, rect);
view = table.findViewById(R.id.t3);
rect = getRawCoordinatesRect(view);
cells.put(view, rect);
view = table.findViewById(R.id.t4);
rect = getRawCoordinatesRect(view);
cells.put(view, rect);
view = table.findViewById(R.id.t5);
rect = getRawCoordinatesRect(view);
cells.put(view, rect);
view = table.findViewById(R.id.t6);
rect = getRawCoordinatesRect(view);
cells.put(view, rect);
view = table.findViewById(R.id.t7);
rect = getRawCoordinatesRect(view);
cells.put(view, rect);
view = table.findViewById(R.id.t8);
rect = getRawCoordinatesRect(view);
cells.put(view, rect);
view = table.findViewById(R.id.t9);
rect = getRawCoordinatesRect(view);
cells.put(view, rect);
hasCoordinatesPopulated = true;
}
}
private Rect getRawCoordinatesRect(final View view) {
int[] coords = newint[2];
view.getLocationOnScreen(coords);
Rectrect=newRect();
rect.left = coords[0];
rect.top = coords[1];
rect.right = rect.left + view.getWidth();
rect.bottom = rect.top + view.getHeight();
return rect;
}
});
table.setOnTouchListener(newOnTouchListener() {
@OverridepublicbooleanonTouch(final View v, final MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
finalintx= (int) event.getRawX();
finalinty= (int) event.getRawY();
for (final Entry<View, Rect> entry : cells.entrySet()) {
finalViewview= entry.getKey();
finalRectrect= entry.getValue();
if (rect.contains(x, y)) {
view.setBackgroundColor(Color.CYAN);
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
}
returntrue;
}
returnfalse;
}
});
}
}
Of course it's a quick sample variant. You can store your last selected view in a separate field (to deselect on move) and when element below the pointer is found, just break
the loop saving some time.
Solution 2:
Have you tried the following:
Get raw coordinates of all your textviews and storing them in a map with key being your TextView (or id) and value being Rect of it's coordinates and space it takes.
Set an ontouchlistener on your tableview, and then, when user moves, or touches something, you can get raw coordinates of the pointer, iterate through your map entries and find TextView currently below
?
P.S. This is first idea that popped in my mind. I'm pretty sure android can provide a more elegant solution.
Solution 3:
Works like a charm now. Thanks a bunch :>
EDIT: added handleCell()
// Initialization
grid.getViewTreeObserver().addOnGlobalLayoutListener(newOnGlobalLayoutListener() {
publicvoidonGlobalLayout() {
if (!populatedCells) {
handlePopulate();
populatedCells = true;
}
}
});
privatevoidhandlePopulate() {
intcurCellId=0;
for (inti=0; i < GRID_HEIGHT; i++) {
for (intj=0; j < GRID_WIDTH; j++) {
TextViewtv= (TextView) findViewById(curCellId);
Rectrect= getRawCoordinatesRect(tv);
Log.d("RECT", "id: "+tv.getId()+" "+rect.toString());
cells.put(tv, rect);
curCellId++;
}
}
}
// ListenerfinalOnTouchListenercellTouch=newOnTouchListener() {
publicbooleanonTouch(View v, MotionEvent e) {
finalintaction= e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE: {
handleCell(v,e);
break;
}
}
returntrue;
}
};
private TextView getCell(View v, MotionEvent e) {
intx= (int) e.getRawX();
inty= (int) e.getRawY();
for (final Entry<View, Rect> entry : cells.entrySet()) {
finalViewview= entry.getKey();
finalRectrect= entry.getValue();
if(rect.contains(x, y)) {
return (TextView) view;
}
}
returnnull;
}
privatevoidhandleCell(View v, MotionEvent e) {
TextViewcell= getCell(v, e);
if (cell != null) {
cell.setBackgroundResource(R.layout.border_cell_current);
curCell = (TextView) cell;
// do something
}
}
Post a Comment for "Ontouchlistener For Multiple Textviews Inside Tablerow"