无法使用mvxlistview获取适配器视图项

时间:2015-03-05 01:23:18

标签: xamarin.android mvvmcross

我有一个填充的mvxlistview。我也实现了一个滑动手势,这个想法是扫描一个项目执行操作1,扫描右边执行其他操作。

我正在使用移动事件中的PointToPosition来获取滑动下的项目。虽然它适用于第一个项目(PointToPosition方法给出的值为1,从我可以看到的是,当你从真实位置的值中减去1时很好),第二个项目给出的值为-1(当

项下没有任何内容

我正在使用的代码就是这个

// Activity - the IOnTouchListener and IOnItemSelectedListener and IOnLongClickListeners can be ignored

public class WishList : MvxActivity,View.IOnTouchListener, AdapterView.IOnItemSelectedListener, View.IOnLongClickListener
{
    private GestureDetector gestureDetector;

    private int REL_SWIPE_MIN_DISTANCE;
    private int REL_SWIPE_MAX_OFF_PATH;
    private int REL_SWIPE_THRESHOLD_VELOCITY;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.WishListView);

        var d = new ColorDrawable(Color.White);
        ActionBar.SetBackgroundDrawable(d);

        var dm = Resources.DisplayMetrics;
        REL_SWIPE_MIN_DISTANCE = (int)(120f * (float)dm.DensityDpi / (160f + .5));
        REL_SWIPE_MAX_OFF_PATH = (int)(250f * (float)dm.DensityDpi / (160f + .5));
        REL_SWIPE_THRESHOLD_VELOCITY = (int)(200f * (float)dm.DensityDpi / (160f + .5));

        var lv = FindViewById<MvxListView>(Resource.Id.listView);
        gestureDetector = new GestureDetector(this, new MyGestureDetector(lv, REL_SWIPE_MIN_DISTANCE, REL_SWIPE_MAX_OFF_PATH, REL_SWIPE_THRESHOLD_VELOCITY));
        lv.Focusable = true;
        /*lv.SetOnTouchListener(this);
        lv.ItemLongClick += HandleLongClick;*/
    }

    void HandleDrag(object sender, View.DragEventArgs e)
    {
        var item = sender as MvxListItemView;
        Console.WriteLine(item);
    }

    void HandleLongClick(object sender, AdapterView.ItemLongClickEventArgs e)
    {
        var item = sender as ListView;
        Console.WriteLine(item);
    }

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

    public void OnItemSelected(AdapterView parent, View view, int position, long id)
    {
        Console.WriteLine("Pos = {0}", position);
        //return true;
    }

    public void OnNothingSelected(AdapterView parent)
    {
        //return true;
    }

    public bool OnLongClick(View v)
    {
        Console.WriteLine(v);
        return true;
    }



    public override bool OnTouchEvent(MotionEvent e)
    {
        if (gestureDetector.OnTouchEvent(e))
        {
            if (MyGestureDetector.currentGestureDetected == "LTR")
                Toast.MakeText(this, "Share wishlist item", ToastLength.Long).Show();
            if (MyGestureDetector.currentGestureDetected == "RTL")
            {
                var _vm = ((WishListViewModel)ViewModel);
                var items = _vm.WishListItems;
                Console.WriteLine("hi");
            }
        }
        return true;
    }
}

// MyGestureDetector.cs

public class MyGestureDetector : GestureDetector.SimpleOnGestureListener
{
    MvxListView listView;
    int REL_SWIPE_MIN_DISTANCE;
    int REL_SWIPE_MAX_OFF_PATH;
    int REL_SWIPE_THRESHOLD_VELOCITY;
    Context context;
    public static string currentGestureDetected;

    public MyGestureDetector(MvxListView lv, int minDist, int maxPath, int velo)
    {
        listView = lv;
        context = lv.Context;
        REL_SWIPE_MAX_OFF_PATH = maxPath;
        REL_SWIPE_MIN_DISTANCE = minDist;
        REL_SWIPE_THRESHOLD_VELOCITY = velo;
    }

    public override bool OnSingleTapUp(MotionEvent e)
    {
        var pos = listView.PointToPosition((int)e.GetX(), (int)e.GetY());
        currentGestureDetected = string.Format("Item clicked = {0}", pos);
        return true;
    }

    public override bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        if (Math.Abs(e1.GetY() - e2.GetY()) > REL_SWIPE_MAX_OFF_PATH)
            return false;
        if (e1.GetX() - e2.GetX() > REL_SWIPE_MIN_DISTANCE && Math.Abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY)
            RightToLeftFling((int)e2.GetX(), (int)e2.GetY());
        else
        {
            if (e2.GetX() - e1.GetX() > REL_SWIPE_MIN_DISTANCE && Math.Abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY)
                LeftToRightFling((int)e2.GetX(), (int)e2.GetY());
        }
        return true;
    }

    void RightToLeftFling(int x, int y)
    {
        currentGestureDetected = "RTL";
        var posn = listView.PointToPosition(x, y);
        Console.WriteLine("position = {0}", posn);
        Toast.MakeText(context, "Right to left fling", ToastLength.Long).Show();
    }

    void LeftToRightFling(int x, int y)
    {
        currentGestureDetected = "LTR";
        var posn = listView.PointToPosition(x, y);
        Console.WriteLine("position = {0}", posn);
        Toast.MakeText(context, "Left to right fling", ToastLength.Long).Show();
    }
}

 // Resource.Layout.WishListView

<?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"
orientation="vertical">
<!--Header-->
<RelativeLayout
    android:id="@+id/Header"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:background="@color/payar_green"
    android:paddingLeft="15dp"
    android:layout_alignParentTop="true">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Wishlist"
        android:layout_centerVertical="true"
        android:textColor="@color/white"
        android:textSize="16dp" />
</RelativeLayout>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/Header"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp">
    <TextView
        local:MvxBind="Visible ItemsDontExist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your wishlist is currently empty!"
        android:textColor="@color/black"
        android:textSize="16sp"
        android:paddingTop="15dp" />
    <Mvx.MvxListView
        android:id="@+id/listView"
        local:MvxBind="ItemsSource WishListItems; ItemClick ShowProductCommand; Visible ItemsExist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:verticalSpacing="10dp"
        local:MvxItemTemplate="@layout/_wishlistitemtest"
        android:scrollbars="none" />
</RelativeLayout>
</LinearLayout>

// Resource.Layout._wishlistitemtest
<?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:layout_width="fill_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:padding="10dp">
<RelativeLayout
    local:MvxBind="Visible IsTitle"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Mvx.MvxImageView
        local:MvxBind="ImageUrl Image"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true" />
    <TextView
        local:MvxBind="Text Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:paddingRight="10dp"
        android:layout_marginLeft="50dp"
        android:text="store name"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_centerVertical="true" />
</RelativeLayout>
<LinearLayout
    local:MvxBind="Visible IsItem"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    <TextView
        local:MvxBind="Text Name"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="fill_parent"
        android:textSize="18sp"
        android:paddingRight="10dp"
        android:text="product name"
        android:singleLine="true"
        android:ellipsize="end"
        android:gravity="center_vertical" />
    <TextView
        local:MvxBind="Text FormattedTotalPrice"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textSize="18sp"
        android:textStyle="bold"
        android:text="price"
        android:gravity="center_vertical" />
<!--<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginLeft="20dp">
        <Switch
            local:MvxBind="Checked RemoveCommand"
            android:focusable="false"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textOn="Keep"
            android:textOff="Remove"
            android:checked="true" />
    </RelativeLayout>-->
</LinearLayout>
</LinearLayout>

我正在试图弄清楚这是mvvmcross中的错误还是Xamarin.Android或其他问题。

0 个答案:

没有答案