2015-12-09 72 views
0

我在這裏創建一個動態佈局。我有一個父級佈局,我在其中添加所有動態創建的視圖。現在點擊特定視圖我需要位置。如何獲得在主線性佈局中動態創建的視圖位置?

根據那個位置我必須發送包含這個視圖的JsonObject在Android中。在這裏,我無法獲得確切的觀點。

這是我的代碼:

 int len = result.length(); 
     View addView; 

     if (!(len == 0)) { 

      finalResult.clear(); 
      for (int i = 0; i <= len; i++) { 
       try { 
        finalResult.add(result.getJSONObject(i)); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 

      for (int i = 0; i < result.length(); i++) { 

       JSONObject obj = result.getJSONObject(i); 

       if (i == 0) { 

        addView = mInflater.inflate(R.layout.sightseeing_firstview, null); 
        image = (ImageView) addView.findViewById(R.id.imagesightseeing_first); 
        textview = (TextView) addView.findViewById(R.id.textView1); 
        llfirst = (LinearLayout) addView.findViewById(R.id.llfirstview); 
        llayout.addView(addView); 

        String category1 = finalResult.get(i).getString("CategoryName"); 
        imageurl1 = finalResult.get(i).getString("ImageUrl"); 

        ((BaseActivity) SightseeingToursActivity.this).imageLoader.displayImage(imageurl1, image, 
          Common.optionsSmall); 

        textview.setText(category1); 


       } else if (i % 5 == 0) { 
        addView = mInflater.inflate(R.layout.sightseeing_firstview, null); 
        images = (ImageView) addView.findViewById(R.id.imagesightseeing_first); 
        textview = (TextView) addView.findViewById(R.id.textView1); 
        llfirst = (LinearLayout) addView.findViewById(R.id.llfirstview); 
        llayout.addView(addView); 
        String category1 = finalResult.get(i).getString("CategoryName"); 
        imageurlss = finalResult.get(i).getString("ImageUrl"); 

        ((BaseActivity) SightseeingToursActivity.this).imageLoader.displayImage(imageurlss, images, 
          Common.optionsSmall); 

        textview.setText(category1); 

       } else { 
        addView = mInflater.inflate(R.layout.sightseeing_secondview, null); 
        iv = (ImageView) addView.findViewById(R.id.secondimagesightseeing_second); 
        tv = (TextView) addView.findViewById(R.id.textView2); 
        llsecond = (LinearLayout) addView.findViewById(R.id.llsecondview); 
        String category2 = finalResult.get(i).getString("CategoryName"); 
        imageurl2 = finalResult.get(i).getString("ImageUrl"); 

        ((BaseActivity) SightseeingToursActivity.this).imageLoader.displayImage(imageurl2, iv, 
          Common.optionsSmall); 
        tv.setText(category2); 


        if (result.length() > i + 1) { 
         i++; 
         ivview = (ImageView) addView.findViewById(R.id.secondimagesightseeing_third); 
         text = (TextView) addView.findViewById(R.id.textView3); 
         llthird = (LinearLayout) addView.findViewById(R.id.llthirdview); 
         String category3 = finalResult.get(i).getString("CategoryName"); 
         imageurl3 = finalResult.get(i).getString("ImageUrl"); 
         ((BaseActivity) SightseeingToursActivity.this).imageLoader.displayImage(imageurl3, ivview, 
           Common.optionsSmall); 

         text.setText(category3); 


        } 
        llayout.addView(addView); 

       } 

       position = llayout.indexOfChild(addView); 
       addView.setTag(position); 
       addView.setOnClickListener(this); 

      } 

     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

@Override 
public void onClick(View v) { 

    position = (Integer) v.getTag(); 
    Toast.makeText(SightseeingToursActivity.this, "Position" + position, Toast.LENGTH_SHORT).show(); 


    try { 
     Intent in = new Intent(SightseeingToursActivity.this, sightseeing_list.class); 
     in.putExtra("TourList", result.getJSONObject(position).toString()); 
     startActivity(in); 
    } catch (Exception e) { 

    } 

} 
+0

看起來像'llayout.addView(addView);'應該是最後'else'塊之外。也就是說,將它移到'else'的右括號後面的'position = llayout.indexOfChild(addView);'之前。 –

+0

嗨邁克...我還沒有得到確切的位置。有兩種佈局。第一種,我加入單個圖像,在第二種佈局中,我有兩個圖像horizo​​ntal.Sequence是一個圖像填充父,然後第二個佈局,兩個圖像,再次兩個圖像,然後下一個圖像,這是重複的。現在我想點擊每個圖像的位置。 –

+0

你已經在添加視圖後編寫了設置標記語句,所以在添加視圖之前將其寫入 – Vickyexpert

回答

0
ViewGroup parent; 
int position; 

for(int i = 0; i < parent.getChildCount(); ++i) { 
    int currentViewId = parent.getChildAt(i).getId(); 

    if(currentViewId == wantendViewId) { 
     position = i; 
    } 
} 

試試這個

+0

可能重複https://stackoverflow.com/a/7737218 –

相關問題