android - View 中的 Xamarin Android EggsToGo(滑动手势)监听器不适用于 ListView

标签 android listview xamarin xamarin.android listener

我在 ListView 控件中遇到 EggsToGo(向左/向右滑动效果)监听器的问题。我有问题 HERE并发现 EggsToGo 的问题与 ListView 监听器完全相同......当我的 ListView 上有一个监听器时,ScrollView 在那里不起作用......是否有一些解决方案可以使 EggsToGo 监听器在 ListView 上运行?

我的测试项目是HERE - 问题出在 FirstFragment(第一个 View )中。当我在 ListView 上有一个监听器时, ScrollView 不起作用。但是当我没有监听器时,滑动不起作用....

这是我认为的代码:

<?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:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include
        layout="@layout/toolbar_back_layout" />
<!-- Test Top Layout -->
    <include
        layout="@layout/view_shopname_lastupdate_ribbon" />
<!-- Layout Date and Picker-->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:background="@android:color/white"
        android:layout_height="50dp"
        android:paddingRight="20dp"
        android:paddingLeft="20dp"
        android:gravity="center_vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Období:"
            local:MvxLang="Text Period"
            android:textSize="16dp"
            android:textStyle="normal"
            android:textColor="@color/main_dark_gray"
            android:ellipsize="marquee"
            android:maxLines="1"
            android:gravity="center"
            android:layout_marginRight="10dp" />
        <MvxSpinner
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/periodSpinner"
            local:MvxBind="ItemsSource PeriodList; SelectedItem SelectedPeriod"
            android:spinnerMode="dropdown" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1">
             <oxyplot.xamarin.android.PlotView
               android:id="@+id/dailySalesModel"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_weight="1"
               android:background="@android:color/white" />
             <MvxListView
              android:id="@+id/dailySalesListView"
              android:layout_weight="1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              local:MvxBind="ItemsSource DailySales"
              local:MvxItemTemplate="@layout/view_dailysalesitem"
              android:divider="@color/main_gray"
              android:dividerHeight="1dp"
              android:choiceMode="none"
              android:layout_gravity="start"
              android:background="@android:color/white"
              android:listSelector="@android:color/transparent"
              android:paddingTop="15dp" />
        </LinearLayout>
        <include
            layout="@layout/view_bottomribbon" />
    </LinearLayout>
</LinearLayout>

这是我的代码 fragment :

   [MvxFragment(typeof(MainViewModel), Resource.Id.content_frame, true)]
    public class DailySalesFragment : BaseBackFragment<DailySalesViewModel>, View.IOnTouchListener
    {
        private Easter _easter;
        private View _view;
        protected override int FragmentId => Resource.Layout.fragment_dailysales;

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var ignore = base.OnCreateView(inflater, container, savedInstanceState);

            _view = this.BindingInflate(FragmentId, null);

            var orientation = Resources.Configuration.Orientation;

            if (orientation == Orientation.Landscape)
            {
                var metrics = Resources.DisplayMetrics;

                var height = CoreHelper.ConvertPixelsToDp(metrics.HeightPixels, Resources);

                if (height <= 360)
                {
                    var topLayout = _view.FindViewById<LinearLayout>(Resource.Id.topLayout);
                    if (topLayout != null)
                    {
                        topLayout.Visibility = ViewStates.Gone;
                    }
                }
            }

            var plotView = _view.FindViewById<PlotView>(Resource.Id.dailySalesModel);
            var bindset = this.CreateBindingSet<DailySalesFragment, DailySalesViewModel>();
            bindset.Bind(plotView).For(q => q.Model).To(vm => vm.Model);
            bindset.Apply();

            _easter = new Easter(new KonamiCode());

            var easyEgg = new CustomEgg("Easy")
                .WatchForSequence(Command.SwipeLeft(), Command.SwipeRight());

            _easter = new Easter(easyEgg);
            _easter.CommandDetected += cmd => DoSwipe(cmd.Value);

            var model = _view.FindViewById<PlotView>(Resource.Id.dailySalesModel);
            var listView = _view.FindViewById<MvxListView>(Resource.Id.dailySalesListView);
            model?.SetOnTouchListener(this);
            listView?.SetOnTouchListener(this);

            InitializeSwipeButtons();
            InitializeShopPicker();

            return _view;
        }

        public override void OnConfigurationChanged(Configuration newConfig)
        {
            base.OnConfigurationChanged(newConfig);
            ViewModel.RefreshView();
        }

        private void InitializeShopPicker()
        {
            var shopLayout = _view.FindViewById<LinearLayout>(Resource.Id.shopLayout);
            if (shopLayout != null)
            {
                shopLayout.Click += delegate
                {
                    ShowShopPickerDialog();
                };
            }
        }

        private void ShowShopPickerDialog()
        {
            var dialog = new ShopPickerDialogFragment(ViewModel.ShopId);
            dialog.DialogClosed += OnDialogClosed;
            dialog.Show(this.Activity.FragmentManager, null);
        }

        private async void OnDialogClosed(object sender, Helpers.ShopPickerDialogEventArgs e)
        {
            if (e.IsChanged)
            {
                var shop = e.Shop;
                await ViewModel.ChangeShop(shop);
            }
        }

        private void InitializeSwipeButtons()
        {
            var leftButton = _view.FindViewById<ImageButton>(Resource.Id.leftButton);
            if (leftButton != null)
            {
                leftButton.Click += delegate
                {
                    DoSwipe("RIGHT");
                };
            }

            var rightButton = _view.FindViewById<ImageButton>(Resource.Id.rightButton);
            if (rightButton != null)
            {
                rightButton.Click += delegate
                {
                    DoSwipe("LEFT");
                };
            }
        }

        private void DoSwipe(string swipeText)
        {
            if (swipeText.Equals("LEFT"))
            {
                FragmentManager.BeginTransaction()
                    .SetCustomAnimations(Resource.Animation.slide_from_right, Resource.Animation.slide_to_left)
                    .Replace(Resource.Id.content_frame, GroupSalesFragment.NewInstance(null))
                    .Commit();
            }

            if (swipeText.Equals("RIGHT"))
            {
                FragmentManager.BeginTransaction()
                    .SetCustomAnimations(Resource.Animation.slide_from_left, Resource.Animation.slide_to_right)
                    .Replace(Resource.Id.content_frame, ReportsFragment.NewInstance(null))
                    .Commit();
            }

            ViewModel.SwipeView(swipeText);
        }

        private int ConvertDpToPixels(int dp)
        {
            var pixels = dp * Resources.DisplayMetrics.Density;
            return (int)pixels;
        }

        private void RefreshLayout(object sender, EventArgs e)
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += WorkerDoWork;
            worker.RunWorkerCompleted += WorkerCompleted;
            worker.RunWorkerAsync();
        }

        private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            _dailySalesSwipeRefreshLayout.Refreshing = false;
        }

        private void WorkerDoWork(object sender, DoWorkEventArgs e)
        {
            Task.Run(ViewModel.ExecuteRefreshAsync);
        }

        public bool OnTouch(View v, MotionEvent e)
        {
            _easter.OnTouchEvent(e);
            return true;
        }
    }

最佳答案

When I have a listener on listview, the scrollview doesnt work. But when I don't have a listener, the swipe doesn't work

EasterOnTouch方法有问题,我不知道它的逻辑,但你可以在OnTouch中自己做实现此功能。

您可以对整个 FirstFragment 布局使用 SetOnTouchListener 方法,并在您的 OnTouch 方法中实现您的逻辑,这是我的代码:

var view = base.OnCreateView(inflater, container, savedInstanceState);
......
view.SetOnTouchListener(this);
//model?.SetOnTouchListener(this);
//listView?.SetOnTouchListener(this);

float mPosX = 0;
float mCurPosX = 0;
float mPosY = 0;
float mCurPosY = 0;
public bool OnTouch(View v, MotionEvent e)
{
    switch (e.Action)
    {
       case MotionEventActions.Down:
            mPosX = e.GetX();
            mCurPosX = mPosX;
            mPosY = e.GetY();
            mCurPosY = mPosY;
            break;
       case MotionEventActions.Move:
            mCurPosX = e.GetX();
            mCurPosY = e.GetY();

            float xDistance = Math.Abs(mCurPosX - mPosX);
            float yDistance = Math.Abs(mCurPosY - mPosY);
            if (xDistance > yDistance && mCurPosX - mPosX > 0)//Swip Right
            {
                DoSwipe("RIGHT");
            }
            else if (xDistance > yDistance && mCurPosX - mPosX < 0)//Swip Left
            {
                DoSwipe("LEFT");
            }
            break;
        default:
            break;
    }
    return true;
}

效果类似this .

关于android - View 中的 Xamarin Android EggsToGo(滑动手势)监听器不适用于 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46073827/

相关文章:

java - 从 .class (Class<?>) 调用 Object 的 toString() 方法

android - 当前 Joda Time 在 Android 上的表现?

android - 如何获得固定宽度的文本高度并获得适合框架的文本长度?

android - Fragments 内 ListView 上的上下文菜单

android - 我有一个包含多个 ListView 的 ViewPager,我在哪里监听被单击的项目?

ios - iPhone 4S - 未经授权的访问异常

android - 不同硬件的不同文本大小

java - OnItemClickListener 消费 onClickListener 事件

xamarin - 如何在 xamarin forms 应用程序中将我的 nuget 包从旧项目安装到新项目?

c# - 如何使用从远程通知创建的本地通知激活 Activity