2013-11-23 46 views
0

我已填充的垂直滾動視圖與由...組成「配置文件」存在於裝置上的潛在的長的垂直線性佈局,像這樣:確定scrollView中單擊的對象?

for (Profile profile : Profiles) { 
    LayoutInflater inflater = getLayoutInflater(); 
    View profileCard = inflater.inflate(R.layout.selector, null); 

    ... 

    profileList.addView(profileCard); 
} 

selector佈局,我提供了一個onClick方法:因此,如果你點擊其中一個配置文件,onClick方法就會運行。但是,我想專門選擇特定的配置文件:如何返回scrollView中點擊哪個元素的值?

+0

哪裏是點擊事件和代碼的其餘部分?提供更多的代碼。 – TheFlash

回答

1

做這樣

for (final Profile profile : Profiles) { 
      LayoutInflater inflater = getLayoutInflater(); 
      View profileCard = inflater.inflate(R.layout.selector, null); 

      /* 
      *your code 
      */ 


      profileCard.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        Profile clickdProfile = profile; 

       } 
      }); 

      profileList.addView(profileCard); 
     } 

注:不要在佈局文件的onclick提供。

+0

剛剛意識到我可以這樣做,如果我使用增強for循環而不是for(int i = ...),我如何檢索我當前在配置文件中的配置文件的索引? – mike

+1

你爲什麼需要索引。你在onclick獲得的個人資料是點擊個人資料。 –

+0

對不起,接下來的是,當用戶滑動其中一個條目時,是否可以使用類似的方法? – mike