Xamarin C# - 创建可滑动的 LinearLayout

Xamarin C# - Create a swipeable LinearLayout

我创建了一个 LinearLayout 1,但我还需要显示 LinearLayout 2。问题是它会在屏幕上占用很多 space,所以我想我可以让它可滑动。

现在我需要创建一个可滑动视图,但我找不到任何关于在 LinearLayouts 之间滑动的信息。所以明确一点,我需要某种用于 LinearLayouts 的幻灯片放映...有人知道我如何创建类似的东西或者可以为我提供示例吗?

您可以通过 ViewPager 实现。 ViewPager 是一个布局管理器,可让您实现手势导航。手势导航允许用户左右滑动以浏览数据页面。

这是渲染。

请参考以下代码

activity_main.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
android:layout_height="match_parent"
    android:id="@+id/viewpager_page"
/>

view_one.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="first Page"
    android:textColor="#000000"
    android:textSize="18sp"
    android:textStyle="bold"
/>

view_Two.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="second Page"
    android:textColor="#000000"
    android:textSize="18sp"
    android:textStyle="bold"
/>

MainActivity.cs

namespace ViewPager
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        Android.Support.V4.View.ViewPager viewpager_page = FindViewById<Android.Support.V4.View.ViewPager>(Resource.Id.viewpager_page);
        List<View> viewlist =new List<View>();
        //LayoutInflater.Inflate;
        viewlist.Add(LayoutInflater.Inflate(Resource.Layout.view_one,null,false));
        viewlist.Add(LayoutInflater.Inflate(Resource.Layout.view_Two, null, false));
        MyPagerAdapter mAdapter = new MyPagerAdapter(viewlist);
        viewpager_page.Adapter = mAdapter;
    }
}
}

MyPagerAdapter.cs

class MyPagerAdapter : PagerAdapter
{
    List<View> viewlist;
    public MyPagerAdapter(List<View> viewlist)
    {

        this.viewlist = viewlist;
    }
    public override int Count => 2;

    public override bool IsViewFromObject(View view, Java.Lang.Object @object)
    {
        return view == @object;
    }

    public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
    {
        container.AddView(viewlist[position]);
        return viewlist[position];
    }
    public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object @object)
    {
        container.RemoveView(viewlist[position]);
    }
}