2011-02-16 60 views
2

我正在尝试创建一个单词混杂游戏,您将混乱的单词和字母交换中的字母向右或向左拖动。以编程方式在RelativeLayout中重新排序项目的最佳方式是什么?因此,当字母被拖动时,左侧的字母被放置在被拖动的字母的右侧。以编程方式重新排列RelativeLayout

我已经尝试过这样的基本测试。

public static void moveTile(Tile tile, int x, RelativeLayout parent) { 
    if (x < tile.getWidth()) { 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     params.addRule(RelativeLayout.LEFT_OF, tile.getId() - 1); 
     tile.setLayoutParams(params); 

     RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     p.addRule(RelativeLayout.RIGHT_OF, tile.getId()); 
     Tile t = (Tile) parent.findViewById(tile.getId() - 1); 
     t.setLayoutParams(p); 
    } 
    parent.invalidate(); 
} 

但是,这会导致应用程序有关,我理解“循环依赖关系不能在RelativeLayout的存在”,但我只是不知道这样做有什么其他方法错误崩溃。

任何帮助将不胜感激。

回答

0

我结束了使用LinearLayout,只是之前或之后删除瓷砖,并用当前选定的瓷砖替换它像这样。

public static void moveTile(Tile tile, int x, LinearLayout parent) { 
    if (x < 0) { 

     int t = Math.abs(x)/tile.getWidth() + 1; 

     if (tile.getId() - t >= 0) { 
      Tile new_tile = (Tile) parent.findViewById(tile.getId() - t); 

      parent.removeViewAt(new_tile.getId()); 
      parent.addView(new_tile, tile.getId()); 

      int id = tile.getId(); 
      tile.setId(new_tile.getId()); 
      new_tile.setId(id); 
     } 
    } else if (x > tile.getWidth()) { 

     int t = x/tile.getWidth(); 

     if (tile.getId() + t < word.length()) { 
      Tile new_tile = (Tile) parent.findViewById(tile.getId() + t); 

      parent.removeViewAt(new_tile.getId()); 
      parent.addView(new_tile, tile.getId()); 

      int id = tile.getId(); 
      tile.setId(new_tile.getId()); 
      new_tile.setId(id); 
     } 
    } 
} 
2

是的,这个错误意味着你不能有一个object1是toRightOf对象2和对象2 toLeftOf对象1

+0

是的,我明白这一点。在我左右移动时如何完成字母定位的任何建议? – 2011-02-16 13:08:37

+1

在添加新工作之前删除两条规则,这样就永远不会有什么时候什么是循环的? – Dre 2011-02-16 13:33:25

0

我觉得你的情况,你不应该使用toRightOf和toLeftOf来定位你的意见,但尝试使用刚刚保证金所有这些设置都是左边界和余下边界。通过这种方式,您的子视图将彼此独立,您可以通过更改边距来移动它们。

相关问题