如何使用下一个按钮使用 ViewPager 从一个片段调用并显示到另一个片段 (Xamarin.Android)

How to use next button to call and display from one fragment to another fragment using ViewPager (Xamarin.Android)

我现在的状态是可以滑动到 view/display 我的第二个片段,因为 ViewPager 就是这样做的。但是我接下来想要手册button.Sooner我要添加更多的片段。

SignUpActivity.cs 这是我的注册 Activity class

  public class SignUpActivity : AppCompatActivity
    {
    TextView logIn;
    private TextView[] _dots { get; set; }
    private LinearLayout _dotsLayout { get; set; }

    //button next to another fragment
    Button btnNext;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_signup);
        // Create your application here

        List<AndroidX.Fragment.App.Fragment> fragments = new 
        List<AndroidX.Fragment.App.Fragment>();
        fragments.Add(new SignUpFragment_1());
        fragments.Add(new SignUpFragment_2());

        var adapter = new S_UViewPagerAdapterFragment(SupportFragmentManager, fragments);
        ViewPager pager = (ViewPager)FindViewById(Resource.Id.pager);
        pager.Adapter = adapter;

        pager.PageSelected += Pager_PageSelected;

        _dotsLayout = FindViewById<LinearLayout>(Resource.Id.indicator);
        AddDotsIndicator(0);


        logIn = FindViewById<TextView>(Resource.Id.logIn);
        btnNext = FindViewById<Button>(Resource.Id.btnNext);

        
        //on click login button
        logIn.Click += LogIn_Click;
    }


    private void AddDotsIndicator(int pos)
    {
        _dots = new TextView[2];
        _dotsLayout.RemoveAllViews();
        for (int i = 0; i < _dots.Length; i++){
            _dots[i] = new TextView(this);
            _dots[i].Text = ".";
            _dots[i].TextSize = 35;
            _dotsLayout.AddView(_dots[i]);
        }
        if(_dots.Length > 0)
        {
            _dots[pos].SetTextColor(Android.Graphics.Color.Red); // change indicator color on 
            selected page
        }
    }

    private void Pager_PageSelected(object sender, ViewPager.PageSelectedEventArgs e)
    {
        AddDotsIndicator(e.Position);
    }

    //click to login
    private void LogIn_Click(object sender, EventArgs e)
    {
        //start login activity
        StartActivity(new Intent(this, typeof(LoginActivity)));
        this.Finish(); // finish this session
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, 
    [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, 
           grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }


 }

NeverMind about the Login intent

S_UViewPagerAdapterFragment.cs MyViewPagerAdapterFragment

  [Obsolete]
  public class S_UViewPagerAdapterFragment : AndroidX.Fragment.App.FragmentPagerAdapter
  {
    List<AndroidX.Fragment.App.Fragment> fragments;

    public S_UViewPagerAdapterFragment(AndroidX.Fragment.App.FragmentManager fm , 
     List<AndroidX.Fragment.App.Fragment> fragments) : base(fm)
    {
        this.fragments = fragments;
    }
    public override int Count
    {
        get
        {
            return fragments.Count;
        }

    }
    public override AndroidX.Fragment.App.Fragment GetItem(int position)
    {
        return fragments[position];
    }
 }

SignUpFragment_1 这是我的第一个片段

   public class SignUpFragment_1 : AndroidX.Fragment.App.Fragment
   {

    Button btnNext;
    public SignUpFragment_1()
    { }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
       savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);

        View view = inflater.Inflate(Resource.Layout.activity_signupFragment1, container, 
        false);
        btnNext = (Button)view.FindViewById(Resource.Id.btnNext);

        return view;

       
     }

  }

SignUpFragment_2我的第二个片段

  public class SignUpFragment_2 : AndroidX.Fragment.App.Fragment
  {
    public SignUpFragment_2()
    { }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
       savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);

        View view = inflater.Inflate(Resource.Layout.activity_signupFragment2, container, 
        false);
       return view;
     }
 }

您可以使用 SetCurrentItem 将片段从一个片段更改为另一个片段。

您 activity_signup 中的下一步按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
    android:id="@+id/button1"
    android:text="NEXT"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<androidx.viewpager.widget.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.viewpager.widget.PagerTabStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</androidx.viewpager.widget.ViewPager>
</LinearLayout>

代码:使用CurrentItem获取片段的当前索引。

var button = FindViewById<Button>(Resource.Id.button1);
        button.Click += delegate
        {
            pager.SetCurrentItem(pager.CurrentItem + 1, true);
        };