从PagerAdapter中膨胀的视图中获取值

时间:2017-08-02 09:53:53

标签: android android-viewpager swipe-gesture android-pageradapter

我有一个with open(infile) as f: f = f.readlines() for line in f: if keep_phrases in line: print(line) important.append(line) Activity和一个实现ViewPager的自定义适配器。我正在PagerAdaper内部扩展视图并将子视图添加到父视图中。这些子视图是PagerAdapterRadioGroupCheckboxes等等,具体取决于我传递给适配器的列表对象的某个参数。

我想在页面更改后从这些子视图中获取值。但由于我的通胀代码在适配器中,我需要知道如何从适配器本身检测页面更改。

以下是我的适配器代码: -

EditText

我将适配器传递给我的ViewPager,如下所示: -

 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;
        }

如何检测网页更改并从mViewPager.Adapter = new PollViewPagerAdapter(this, this.FragmentManager, AppHelper.SelectedVotes); RadioGroupCheckbox获取值?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

您无需知道何时在适配器内滚动页面。您可以将页面滚动侦听器分配给寻呼机,如下所示:

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

我将逻辑保留在适配器之外。您甚至可以在Activity / Fragment和Adapter之间建立一个接口,以监听诸如radiogroup状态变化等事件。

修改

让我试着进一步阐述。你的问题没有正确的答案。您可以通过多种方式实现此类行为。假设您的适配器中有一个带有已检查更改侦听器的radiogroup。在选中的更改方法中,您可以调用您在适配器中定义的一些接口方法,例如。

interface RadioChangedListener{
    fun radioChanged(radioId: Int)
}

在适配器中定义此接口,并使您的片段/活动实现它。然后声明适配器将构造函数中的activity / fragment作为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方法,您可以在其中存储您的值。