我有一个带有Activity的ViewPager和一个实现PagerAdaper的自定义适配器。我正在PagerAdapter中膨胀一个视图,并将子视图添加到父视图中。这些子视图是RadioGroup、Checkboxes、EditText等,这取决于对象列表中传递给适配器的某个参数。
我想得到这些子视图的值,一旦页面改变。但是,由于我的膨胀代码在适配器中,我需要知道如何从适配器本身检测页面更改。
这是我的适配器代码:-
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中获取值?任何帮助都是非常感谢的。
发布于 2017-08-02 10:01:15
您不需要知道页面何时在适配器内滚动。您可以向寻呼机分配一个页面滚动侦听器,如下所示:
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方法中,可以调用在适配器中定义的一些接口方法。
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方法,您可以在其中存储值。
https://stackoverflow.com/questions/45457457
复制相似问题