Android: how to show and move ImageView in TableLayout
I have a grid of TextViews
shown inside a TableLayout
. When the user taps a TextView
I need to change the cell's background by adding an ImageView
that will go to another cell when other cells will be clicked.
TextView can have an image (drawable) as a background. So it is easy to do it with textView.setBackgroundDrawable(R.drawable.myimage);
You can move the image into any cell-textView with this method and set the previous to another image or nothing with textView.setBackgroundDrawable(null);
You cannot animate this movement in the table, because each row is one view an may limit the animation of its children views to its constrains.
Sounds like a puzzle you want do implemenent...
You should do the following to achieve what you want
- store all information you need from the first cell (which image is shown, which cell it was...)
- on the second cell store this information too
- now switch the image view settings, means you need to set the image of cell one to the image of cell 2 and vice versa
This is a simple algorithm to solve your problem.
- Maintain an array of image ids which are displayed in table as same order.
Get two taps positions and swap the corresponding image ids in array and reload all images as like as array in table.
If u follow this u will get.
精彩评论