如何重构 android 代码以选择布局?
How do I refactor android code to choose layout?
我有 1 个顶部布局和 3 个底部布局,用户将项目从顶部布局拖到下面 3 个布局之一。
代码如下:
- 将每个底部布局中的每个图像按顺序放入列表中
- 从每个底部布局中删除项目
将它们放回顶部布局
public void onClick(View view) {
LinearLayout bottomLinearLayout1 = (LinearLayout) findViewById(R.id.bottom1);
LinearLayout bottomLinearLayout2 = (LinearLayout) findViewById(R.id.bottom2);
LinearLayout bottomLinearLayout3 = (LinearLayout) findViewById(R.id.bottom3);
LinearLayout topLinearLayout = (LinearLayout) findViewById(R.id.topLinear);
int total_num1 = bottomLinearLayout1.getChildCount();
int total_num2 = bottomLinearLayout2.getChildCount();
int total_num3 = bottomLinearLayout3.getChildCount();
View current_image = null;
List<View> listOfkids = new ArrayList<>() ;
//************ REPEATS **************
for(int i = 0 ; i < total_num1 ; i++){
current_image = bottomLinearLayout1.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout1.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
listOfkids.clear();
//************ REPEATS **************
for(int i = 0 ; i < total_num2 ; i++){
current_image = bottomLinearLayout2.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout2.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
listOfkids.clear();
//************ REPEATS **************
for(int i = 0 ; i < total_num3 ; i++){
current_image = bottomLinearLayout3.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout3.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
基本上,这些循环之间的唯一区别是 "bottomLinearLayout" 中的最后一位数字;其他代码只是自我复制!
我可以这样做吗:
if(id == R.id.bottom1){
String current_layout = "bottomLinearLayout" + 1 ;
}
else if( id == R.id.bottom2){
current_layout = "bottomLinearLayout" + 2 ;
}
else if( id == R.id.bottom3){
current_layout = "bottomLinearLayout" + 3 ;
}
然后将此字符串作为 命令添加到 java 源代码中?
这可能吗?
假设您想重构 onClick
方法,您可以创建一个布局 ID 数组并按如下方式遍历它们:
int bottomLayoutIds[] = {R.id.bottom1, R.id.bottom2, R.id.bottom3};
LinearLayout topLinearLayout = (LinearLayout) findViewById(R.id.topLinear);
for (int layoutId : bottomLayoutIds){
LinearLayout bottomLinearLayout = (LinearLayout) findViewById(layoutId);
int childCount = bottomLinearLayout.getChildCount();
List<View> listOfKids = new ArrayList<>() ;
for(int i = 0 ; i < childCount ; i++){
View currentImage = bottomLinearLayout.getChildAt(i);
listOfKids.add(currentImage);
}
bottomLinearLayout.removeAllViews();
for(int i = 0 ; i < listOfKids.size();i++){
topLinearLayout.addView( listOfKids.get(i));
}
}
我有 1 个顶部布局和 3 个底部布局,用户将项目从顶部布局拖到下面 3 个布局之一。
代码如下:
- 将每个底部布局中的每个图像按顺序放入列表中
- 从每个底部布局中删除项目
将它们放回顶部布局
public void onClick(View view) { LinearLayout bottomLinearLayout1 = (LinearLayout) findViewById(R.id.bottom1); LinearLayout bottomLinearLayout2 = (LinearLayout) findViewById(R.id.bottom2); LinearLayout bottomLinearLayout3 = (LinearLayout) findViewById(R.id.bottom3); LinearLayout topLinearLayout = (LinearLayout) findViewById(R.id.topLinear); int total_num1 = bottomLinearLayout1.getChildCount(); int total_num2 = bottomLinearLayout2.getChildCount(); int total_num3 = bottomLinearLayout3.getChildCount(); View current_image = null; List<View> listOfkids = new ArrayList<>() ; //************ REPEATS ************** for(int i = 0 ; i < total_num1 ; i++){ current_image = bottomLinearLayout1.getChildAt(i); listOfkids.add(current_image); } bottomLinearLayout1.removeAllViews(); for(int i = 0 ; i < listOfkids.size();i++){ topLinearLayout.addView( listOfkids.get(i)); } listOfkids.clear(); //************ REPEATS ************** for(int i = 0 ; i < total_num2 ; i++){ current_image = bottomLinearLayout2.getChildAt(i); listOfkids.add(current_image); } bottomLinearLayout2.removeAllViews(); for(int i = 0 ; i < listOfkids.size();i++){ topLinearLayout.addView( listOfkids.get(i)); } listOfkids.clear(); //************ REPEATS ************** for(int i = 0 ; i < total_num3 ; i++){ current_image = bottomLinearLayout3.getChildAt(i); listOfkids.add(current_image); } bottomLinearLayout3.removeAllViews(); for(int i = 0 ; i < listOfkids.size();i++){ topLinearLayout.addView( listOfkids.get(i)); }
基本上,这些循环之间的唯一区别是 "bottomLinearLayout" 中的最后一位数字;其他代码只是自我复制!
我可以这样做吗:
if(id == R.id.bottom1){
String current_layout = "bottomLinearLayout" + 1 ;
}
else if( id == R.id.bottom2){
current_layout = "bottomLinearLayout" + 2 ;
}
else if( id == R.id.bottom3){
current_layout = "bottomLinearLayout" + 3 ;
}
然后将此字符串作为 命令添加到 java 源代码中?
这可能吗?
假设您想重构 onClick
方法,您可以创建一个布局 ID 数组并按如下方式遍历它们:
int bottomLayoutIds[] = {R.id.bottom1, R.id.bottom2, R.id.bottom3};
LinearLayout topLinearLayout = (LinearLayout) findViewById(R.id.topLinear);
for (int layoutId : bottomLayoutIds){
LinearLayout bottomLinearLayout = (LinearLayout) findViewById(layoutId);
int childCount = bottomLinearLayout.getChildCount();
List<View> listOfKids = new ArrayList<>() ;
for(int i = 0 ; i < childCount ; i++){
View currentImage = bottomLinearLayout.getChildAt(i);
listOfKids.add(currentImage);
}
bottomLinearLayout.removeAllViews();
for(int i = 0 ; i < listOfKids.size();i++){
topLinearLayout.addView( listOfKids.get(i));
}
}