0

充氣意見獲取值我有一個ViewPager,並實現了PagerAdaper自定義適配器的Activity。我正在充氣PagerAdapter中的一個視圖,並向子視圖添加子視圖。這些子視圖RadioGroupCheckboxesEditText等等,這取決於的對象,其名單我傳遞給適配器的某個參數。從在PagerAdapter

我想一旦網頁被改爲從這些子視圖獲取值。但是由於我的充氣代碼在適配器中,所以我需要知道如何從我的適配器本身檢測頁面更改。

這裏是我的適配器代碼: -

public override Java.Lang.Object InstantiateItem(ViewGroup container, int position) 
     { 
      var view = voteFormActivity.LayoutInflater.Inflate(Resource.Layout.active_votes_layout, container, false); 
      txtvoteTitle = view.FindViewById<TextView>(Resource.Id.txtTitle); 
      //radioGroup = view.FindViewById<RadioGroup>(Resource.Id.radioGroupChoice); 
      btnSubmit = view.FindViewById<Button>(Resource.Id.btnSubmit); 
      layoutForcomponents = view.FindViewById<LinearLayout>(Resource.Id.layoutForComponents); 


      radioGroup = new RadioGroup(voteFormActivity); 

      var data = selectedVotes.ElementAt(position); 



      if (data != null) 
      { 
       if (!string.IsNullOrEmpty(data.Title)) 
       { 
        txtvoteTitle.Text = data.Title; 
       } 


       var choiceList = data.Choices; 

       if (choiceList != null && choiceList.Count > 0) 
       { 
        if (checkParam == "RadioChoice") 
        { 
         foreach (var choice in choiceList) 
         { 
          RadioButton rButton = new RadioButton(voteFormActivity); 
          rButton.Text = choice; 
          radioGroup.AddView(rButton); 
          layoutForcomponents.RemoveAllViews(); 
          layoutForcomponents.AddView(radioGroup); 
         } 
        } 
        else if (checkParam == "CheckBoxChoice") 
        { 
         foreach (var choice in choiceList) 
         { 
          CheckBox cButton = new CheckBox(voteFormActivity); 
          cButton.Text = choice; 
          radioGroup.AddView(cButton); 
          layoutForcomponents.RemoveAllViews(); 

          layoutForcomponents.AddView(radioGroup); 

         } 
        } 

       } 
       //if (checkParam == "MultiLineText") 
       else{ 
        EditText etMultilineText = new EditText(voteFormActivity); 
        etMultilineText.Hint = "Enter feedback"; 
        etMultilineText.SetLines(6); 
        layoutForcomponents.RemoveAllViews(); 

        layoutForcomponents.AddView(etMultilineText); 
       } 
      } 
      container.AddView(view); 
      return view; 
     } 

我通過適配器連接到我的ViewPager如下: -

mViewPager.Adapter = new PollViewPagerAdapter(this, this.FragmentManager, AppHelper.SelectedVotes); 

如何檢測頁面的變化,並從價值觀RadioGroupCheckboxEditText?任何幫助表示讚賞。

+0

有一個''ViewPager.onPageChangeListener您連接到viewpager。這是你以後的事嗎?我可以將它作爲一個答案,如果這樣 – RobVoisey

+0

@RobVoisey'ViewPager.onPageChangeListener'已被應用到活動中,但我需要在'PagerAdapter'充氣的值,我怎樣才能得到這些值的頁面更改時? – user3034944

+0

您不能簡單地將視圖作爲字段存儲在您的課堂上嗎? 'ViewPager.onPageChangeListener'可以讓你知道什麼時候頁面已經更新,你可以得到當前正在顯示的頁面,然後你就會知道使用哪個視圖字段。 – RobVoisey

回答

0

你不需要當一個頁面在適配器內滾動就知道了。你可以像這樣爲尋呼機分配一個頁面滾動監聽器:

viewPager.setOnPageChangeListener(object : OnPageChangeListener { 
     override fun onPageScrollStateChanged(state: Int) {} 
     override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {} 
     override fun onPageSelected(position: Int) {} 
    }) 

我會保持適配器外部的邏輯。你甚至可以在活動/片段和你的適配器之間建立一個接口來監聽諸如radiogroup狀態變化之類的事件。

編輯

讓我嘗試進一步闡述。你的問題沒有正確的答案。你可以用很多方式實現這種行爲。假設你的適配器中有一個radiogroup,並帶有一個checked change listener。在選中的變更方法中,您可以調用您在適配器內定義的某種接口方法。

interface RadioChangedListener{ 
    fun radioChanged(radioId: Int) 
} 

在適配器內部定義該接口,並使您的片段/活動實現它。然後聲明適配器通過它的活動/片段在構造函數中的radioChangedListener對象:

class YourAdapter(var listItems: MutableList<YourItem>, val yourListener: RadioChangedListener) 
: RecyclerView.Adapter<YourAdapter.YourHolder>() { 

//...adapter code 

} 

最後,在RadioGroup中監聽器,你可以打電話給你的接口方法,像這樣(讓你爲這個原因我的Java代碼米懶):

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);   
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{ 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
     yourListener.radioChanged(checkedId) 
    } 
}); 

這將觸發活動,您可以在其中存儲你的價值裏面你radioChanged方法。

+0

感謝您的答案。我正在尋找你答案的第二部分。您能否詳細說明我如何能夠監聽適配器和活動之間的radiogroup狀態更改等事件? – user3034944

+0

謝謝。我會試試 – user3034944

+0

沒問題,你也可以將值存儲在適配器中的字段中,並從onPageChanged方法中的Activity調用一個像getCurrentSelectedValues()這樣的方法,獲取該項目的值在位置1 –