What I did
Made a class CustomViewPager that makes swipe disable. However I can't use it correctly with using androidx.viewpager.widget.ViewPager. Display about ViewPager disappeared.
Code
AdapterViewPager.cs
using System.Collections.Generic;
using Android.Content;
using Android.Util;
using Android.Views;
using AndroidX.Fragment.App;
using AndroidX.ViewPager.Widget;
using Fragment = AndroidX.Fragment.App.Fragment;
namespace Android
{
//NuGet
//ViewPageAdapter
//lifecycle
public class VPAdapter : FragmentStatePagerAdapter
{
private bool m_enabled;
List<Fragment> fragments = new List<Fragment>();
List<string> fragmentsName = new List<string>();
public VPAdapter(AndroidX.Fragment.App.FragmentManager fragmentManager) : base(fragmentManager)
{
fragments = new List<Fragment>();
fragmentsName = new List<string>();
}
public void AddFragment(Fragment fragment, string name)
{
fragments.Add(fragment);
fragmentsName.Add(name);
}
public override int Count
{
get
{
return fragments.Count;
}
}
public override Fragment GetItem(int position)
{
return fragments[position];
}
public void SetPagingEnabled(bool enabled)
{
m_enabled = enabled;
}
}
public class CustomViewPager : ViewPager
{
public bool ScrollEnabled;
public CustomViewPager(Context context, IAttributeSet attrs) : base(context, attrs)
{
this.ScrollEnabled = false;
}
public override bool OnTouchEvent(MotionEvent e)
{
if (this.ScrollEnabled)
{
return base.OnTouchEvent(e);
}
return false;
}
public override bool OnInterceptTouchEvent(MotionEvent e)
{
if (this.ScrollEnabled)
{
return base.OnInterceptTouchEvent(e);
}
return false;
}
public void SetPagingEnabled(bool enabled)
{
ScrollEnabled = enabled;
}
}
}
ActivityMain.cs
//ViewPager
VPAdapter vpAdapter = new VPAdapter(SupportFragmentManager);
//omission
viewPager.Adapter = vpAdapter;
//CustomViewpager
private IAttributeSet attrs;
CustomViewPager customViewPager2 = new CustomViewPager(this, attrs);
customViewPager2.SetPagingEnabled(false);
activity_main.xml
<Android.CustomViewPager
android:id="@ id/customViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@ id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent">
</androidx.viewpager.widget.ViewPager>
</SMP_Android.CustomViewPager>
Reference destination
Is there a way to disable the swiping between Tabbed Page on Android in Xamarin Native android?
CodePudding user response:
You should just replace standard ViewPager with your custom one. No need to put the standard one inside the custom. That's all you need:
<SMP_Android.CustomViewPager
android:id="@ id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />