我是xamarin的新手,我尝试通过创建自定义Viewpager来禁用滑动,但我无法在Viewpager中找到触控侦听器界面。有没有办法做到这一点?
public class CustomViewPager : ViewPager
{
Context _context;
public CustomViewPager(Context context) : base(context)
{
_context = context;
}
public CustomViewPager(Context context, IAttributeSet arg1) : base(context, arg1)
{
_context = context;
}
public bool IsPagerDisabled { get; set; }
/*public override Boolean OnTouchEvent(MotionEvent event)
{
if (!IsPagerDisabled)
{
return base.OnTouchEvent(event);
}
else
{
return false;
}
}*/
}
答案 0 :(得分:1)
我最近做了一个类似的实现,这看起来像一个魅力:
using Android.Content;
using Android.Support.V4.View;
using Android.Views;
public class LockableViewPager : ViewPager
{
public bool SwipeLocked { get; set; }
Context mContext;
public LockableViewPager(Context context) : base(context)
{
Init(context, null);
}
public LockableViewPager(Context context, Android.Util.IAttributeSet attrs) : base(context, attrs)
{
Init(context, attrs);
}
private void Init(Context ctx, Android.Util.IAttributeSet attrs)
{
mContext = ctx;
}
public override bool OnTouchEvent(MotionEvent ev)
{
return !SwipeLocked && base.OnTouchEvent(ev);
}
public override bool OnInterceptTouchEvent(MotionEvent e)
{
return !SwipeLocked && base.OnInterceptTouchEvent(e);
}
public override bool CanScrollHorizontally(int direction)
{
return !SwipeLocked && base.CanScrollHorizontally(direction);
}
}
如何使用它?
在xml中:
<_yourNameSpace.LockableViewPager
auto:SwipeLocked="true"
其中auto是
xmlns:auto="http://schemas.android.com/apk/res-auto"
在代码中:
LockableViewPager viewpager= new LoackableViewpager();
Viewpager.SwipeLocked=true;
答案 1 :(得分:0)
你必须为此编写课程,如下所示。
public class OnTouchListener : Java.Lang.Object, View.IOnTouchListener
{
ViewPager pager;
public OnTouchListener(ViewPager _pager)
{
pager = _pager;
}
public bool OnTouch(View v, MotionEvent e)
{
var view = v as ViewPager;
pager.SetCurrentItem(0, false);
return false;
}
}
然后将此类设置为viewPager。
OnTouchListener onTouchListener = new OnTouchListener(viewPager);
viewPager.SetOnTouchListener(onTouchListener);
答案 2 :(得分:0)
我选择将属性命名为“ ScrollEnabled ”。因为iOS仅使用exat相同的命名。因此,您在两个平台上的名称都相同,从而使开发人员更容易。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V4.View;
using Android.Util;
namespace YourNameSpace.ViewPackage {
// Need to disable swiping for ViewPager, if user performs Pre DSA and the dsa is not completed yet
// http://stackoverflow.com/questions/9650265/how-do-disable-paging-by-swiping-with-finger-in-viewpager-but-still-be-able-to-s
public class CustomViewPager: ViewPager {
public bool ScrollEnabled;
public CustomViewPager(Context context, IAttributeSet attrs) : base(context, attrs) {
this.ScrollEnabled = true;
}
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;
}
// For ViewPager inside another ViewPager
public override bool CanScrollHorizontally(int direction) {
return this.ScrollEnabled && base.CanScrollHorizontally(direction);
}
// Some devices like the Galaxy Tab 4 10' show swipe buttons where most devices never show them
// So, you could still swipe through the ViewPager with your keyboard keys
public override bool ExecuteKeyEvent(KeyEvent evt) {
return this.ScrollEnabled ? base.ExecuteKeyEvent(evt) : false;
}
}
}
在.axml文件中:
<?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="match_parent">
<YourNameSpace.ViewPackage.CustomViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:layout_alignParentTop="true" />
</LinearLayout>