开发者

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.

Wh开发者_Python百科ich is the best way to do this in Android?


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

  1. store all information you need from the first cell (which image is shown, which cell it was...)
  2. on the second cell store this information too
  3. 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.

  1. Maintain an array of image ids which are displayed in table as same order.
  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜