任何人都可以建议在ViewPager的片段中绑定TextView。
请查看以下代码进行调查。
ViewPager 的片段资源文件 (about_fragment.axml(
在这里,我尝试将AboutDetail与ViewModel绑定。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/white">
<TextView
android:id="@+id/idAboutDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32.5dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="8dp"
android:textColor="@android:color/black"
android:textSize="17sp"
local:MvxBind="Text AboutDetail"/>
</LinearLayout>
ViewPager 的片段虚拟机
DetailViewModel 属于包含 ViewPager 的 DetailFragment。
[AddINotifyPropertyChangedInterface]
public class AboutDetailViewModel : MvxViewModel<DetailViewModel>
{
public override void Prepare(DetailViewModel parameter)
{
AboutDetail = "Yes loaded";
}
private string _aboutDetail;
public string AboutDetail
{
get
{
return _aboutDetail;
}
set
{
_aboutDetail = value;
RaisePropertyChanged(() => AboutDetail);
}
}
}
查看寻呼机的片段
public class UCAboutFragment :BaseFragment<AboutDetailViewModel>
{
protected override int FragmentId => Resource.Layout.fragment_details_about;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = base.OnCreateView(inflater, container, savedInstanceState);
return view;
}
}
包含 ViewPager 的 DetailFragment 代码。
DetailPagerAdapter pageAdapter = new DetailPagerAdapter(ChildFragmentManager);
ViewPager pager = thisView.FindViewById<ViewPager>(Resource.Id.viewpager);
pager.Adapter = pageAdapter;
TabLayout tabLayout = thisView.FindViewById<TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(pager);
详细信息寻呼机适配器
private class DetailPagerAdapter : FragmentPagerAdapter
{
private List<Android.Support.V4.App.Fragment> mFragmentList;
private List<string> mFragmentTitleList = new List<string>();
public SectionsPagerAdapter(Android.Support.V4.App.FragmentManager fm) : base(fm)
{
this.mFragmentList = new List<Android.Support.V4.App.Fragment>();
addFragment(new UCAboutFragment(), "About");
addFragment(new UCServicesFragment(), "Services");
addFragment(new UCInsuranceFragment(), "Insurance");
}
#region implemented abstract members of PagerAdapter
public override int Count
{
get
{
return mFragmentList.Count;
}
}
#endregion
#region implemented abstract members of FragmentPagerAdapter
public override Android.Support.V4.App.Fragment GetItem(int position)
{
return mFragmentList[position];
}
#endregion
public override ICharSequence GetPageTitleFormatted(int position)
{
return new Java.Lang.String(mFragmentTitleList[position].ToLower());
}
private void addFragment(Android.Support.V4.App.Fragment fragment, string title)
{
mFragmentList.Add(fragment);
mFragmentTitleList.Add(title);
}
}
碱基片段
{
public abstract class BaseFragment : MvxFragment
{
protected Toolbar Toolbar { get; private set; }
protected BaseFragment()
{
RetainInstance = true;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignore = base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(FragmentId, null);
return view;
}
protected abstract int FragmentId { get; }
}
public abstract class BaseFragment<TViewModel> : BaseFragment where TViewModel : class, IMvxViewModel
{
public new TViewModel ViewModel
{
get { return (TViewModel)base.ViewModel; }
set { base.ViewModel = value; }
}
}
}
在 ViewPager 中的片段中,您需要使用BindingInflate
来膨胀您的View
:
public class UCAboutFragment :BaseFragment<AboutDetailViewModel>
{
protected override int FragmentId => Resource.Layout.fragment_details_about;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var _ = base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(FragmentId, container, false);
return view;
}
}
BindingInflow 用于发现布局中的绑定表达式,并将 ViewModel 与 View iteself 连接起来。