从在PagerAdapter
充气意见获取值我有一个ViewPager
,并实现了PagerAdaper
自定义适配器的Activity
。我正在充气PagerAdapter
中的一个视图,并向子视图添加子视图。这些子视图RadioGroup
,Checkboxes
,EditText
等等,这取决于的对象,其名单我传递给适配器的某个参数。从在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);
如何检测页面的变化,并从价值观RadioGroup
,Checkbox
和EditText
?任何帮助表示赞赏。
你不需要当一个页面在适配器内滚动就知道了。你可以像这样为寻呼机分配一个页面滚动监听器:
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
方法。
感谢您的答案。我正在寻找你答案的第二部分。您能否详细说明我如何能够监听适配器和活动之间的radiogroup状态更改等事件? – user3034944
谢谢。我会试试 – user3034944
没问题,你也可以将值存储在适配器中的字段中,并从onPageChanged方法中的Activity调用一个像getCurrentSelectedValues()这样的方法,获取该项目的值在位置1 –
有一个''ViewPager.onPageChangeListener您连接到viewpager。这是你以后的事吗?我可以将它作为一个答案,如果这样 – RobVoisey
@RobVoisey'ViewPager.onPageChangeListener'已被应用到活动中,但我需要在'PagerAdapter'充气的值,我怎样才能得到这些值的页面更改时? – user3034944
您不能简单地将视图作为字段存储在您的课堂上吗? 'ViewPager.onPageChangeListener'可以让你知道什么时候页面已经更新,你可以得到当前正在显示的页面,然后你就会知道使用哪个视图字段。 – RobVoisey