2015-05-01 41 views
0

我有1個頂部佈局和3個底部佈局,用戶從頂部佈局拖動項目到下面3個之一。如何重構android代碼以選擇佈局?

dd

下面的代碼:

  • 依次把每個圖像中的每個底部佈局成一個列表
  • 移除從每個底部佈局
  • 把它們放回頂部佈局

    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源代碼

這是可能的嗎?

回答

1

假設要重構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)); 
    } 
} 
+0

那麼簡單,THX這麼多! – ERJAN