首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从PagerAdapter中膨胀的视图中获取值

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

Stack Overflow用户
提问于 2017-08-02 09:53:52
回答 1查看 261关注 0票数 0

我有一个带有ActivityViewPager和一个实现PagerAdaper的自定义适配器。我正在PagerAdapter中膨胀一个视图,并将子视图添加到父视图中。这些子视图是RadioGroupCheckboxesEditText等,这取决于对象列表中传递给适配器的某个参数。

我想得到这些子视图的值,一旦页面改变。但是,由于我的膨胀代码在适配器中,我需要知道如何从适配器本身检测页面更改。

这是我的适配器代码:-

代码语言:javascript
复制
 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如下:

代码语言:javascript
复制
mViewPager.Adapter = new PollViewPagerAdapter(this, this.FragmentManager, AppHelper.SelectedVotes);

如何检测页面更改并从RadioGroupCheckboxEditText中获取值?任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-02 10:01:15

您不需要知道页面何时在适配器内滚动。您可以向寻呼机分配一个页面滚动侦听器,如下所示:

代码语言:javascript
复制
viewPager.setOnPageChangeListener(object : OnPageChangeListener {
        override fun onPageScrollStateChanged(state: Int) {}
        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
        override fun onPageSelected(position: Int) {}
    })

我会把逻辑放在适配器外面。您甚至可以在活动/片段和Adapter之间创建一个接口,以侦听诸如无线电组状态更改之类的事件。

编辑

让我再详细阐述一下。你的问题没有正确的答案。您可以通过多种方式实现这种行为。让我们假设您的适配器中有一个无线电组,并有一个检查过的更改侦听器。例如,在选中的change方法中,可以调用在适配器中定义的一些接口方法。

代码语言:javascript
复制
interface RadioChangedListener{
    fun radioChanged(radioId: Int)
}

在适配器中定义这个接口,并使您的片段/活动实现它。然后将适配器在构造函数中传递给它的活动/片段声明为radioChangedListener对象:

代码语言:javascript
复制
class YourAdapter(var listItems: MutableList<YourItem>, val yourListener: RadioChangedListener)
: RecyclerView.Adapter<YourAdapter.YourHolder>() {

//...adapter code

}

最后,在您的radiogroup监听器中,您可以像这样调用您的接口方法(为此向您提供Java代码,我很懒):

代码语言:javascript
复制
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        yourListener.radioChanged(checkedId)
    }
});

这将触发活动中的radioChanged方法,您可以在其中存储值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45457457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档