2017-09-12 6 views
1

オブジェクトをクリックすると、すぐに次のページを呼び出すことはできません。しかし、あなたがワイプをスクロールして次のページにスクロールするまで、クリックはオブジェクトに残ります。アイテムをクリックし、他のサイトにスワイプ

アイテムにクリックコマンドをどのように保持できますか?

クリックしたアイテムから他のページにスワイプする方法はありますか?ニコラウス項目から

更新

クリックして一つのアイテム>保留中>スワイプ左右に。

これは、実際の動作です:

private int index = -1; 

       break; 
     } 
     return true; 
    } 
} 

答えて

0

それがクリックされたとき、あなたは各項目にスワイプジェスチャを実行するために、アイテムのビューに背景色を設定することができます項目をハイライト表示するには、私はあなたが必要になると思います各項目にIOnTouchListenerを実装します。

public class LVAdapter : BaseAdapter<ListItemModel>, View.IOnTouchListener 
{ 
    private List<ListItemModel> items = new List<ListItemModel>(); 
    private Activity context; 

    private int index = -1; 

    public enum SwipeAction 
    { 
     LR, // Left to Right 
     RL, // Right to Left 
     TB, // Top to bottom 
     BT, // Bottom to Top 
     None // when no action was detected 
    } 

    private int MIN_DISTANCE = 100; 
    private float downX, downY, upX, upY; 
    private SwipeAction maction = SwipeAction.None; 

    public LVAdapter(Activity context, List<ListItemModel> items) : base() 
    { 
     this.context = context; 
     this.items = items; 
    } 

    public override ListItemModel this[int position] 
    { 
     get { return items[position]; } 
    } 

    public override int Count 
    { 
     get { return items.Count; } 
    } 

    public override long GetItemId(int position) 
    { 
     return position; 
    } 

    private void SetSelectedItem(int position) 
    { 
     index = position; 
     NotifyDataSetChanged(); 
    } 

    private class MyViewHolder : Java.Lang.Object 
    { 
     public TextView Name { get; set; } 
     public TextView Description { get; set; } 
     public int index { get; set; } 
    } 

    public override View GetView(int position, View convertView, ViewGroup parent) 
    { 
     MyViewHolder holder = null; 
     var view = convertView; 

     if (view != null) 
      holder = view.Tag as MyViewHolder; 

     if (holder == null) 
     { 
      holder = new MyViewHolder(); 
      view = context.LayoutInflater.Inflate(Resource.Layout.ItemCell, null); 
      holder.Name = view.FindViewById<TextView>(Resource.Id.nametxt); 
      holder.Description = view.FindViewById<TextView>(Resource.Id.detailtxt); 
      holder.index = position; 
      view.Tag = holder; 
     } 

     holder.Name.Text = items[position].Name; 
     holder.Description.Text = items[position].Description; 

     if (index != -1 && position == index) 
     { 
      holder.Name.SetBackgroundColor(Android.Graphics.Color.Red); 
      holder.Description.SetBackgroundColor(Android.Graphics.Color.Pink); 
     } 
     else 
     { 
      holder.Name.SetBackgroundColor(Android.Graphics.Color.RoyalBlue); 
      holder.Description.SetBackgroundColor(Android.Graphics.Color.SeaGreen); 
     } 

     view.SetOnTouchListener(this); 

     return view; 
    } 

    public bool OnTouch(View v, MotionEvent e) 
    { 
     switch (e.Action) 
     { 
      case MotionEventActions.Down: 
       downX = e.GetX(); 
       downY = e.GetY(); 
       maction = SwipeAction.None; 
       break; 

      case MotionEventActions.Move: 
       upX = e.GetX(); 
       upY = e.GetY(); 
       var deltaX = downX - upX; 
       var deltaY = downY - upY; 

       if (Math.Abs(deltaX) > MIN_DISTANCE) 
       { 
        if (deltaX < 0) 
        { 
         maction = SwipeAction.LR; 
        } 
        else if (deltaX > 0) 
        { 
         maction = SwipeAction.RL; 
        } 
        return true; 
       } 
       else if (Math.Abs(deltaY) > MIN_DISTANCE) 
       { 
        if (deltaY < 0) 
        { 
         maction = SwipeAction.TB; 
        } 
        else if (deltaY > 0) 
        { 
         maction = SwipeAction.BT; 
        } 
        return false; 
       } 
       break; 

      case MotionEventActions.Up: 
       var holder = v.Tag as MyViewHolder; 
       if (maction == SwipeAction.None) 
       { 
        SetSelectedItem(holder.index); 
       } 
       else if (maction == SwipeAction.LR | maction == SwipeAction.RL) 
       { 
        if (holder.index == index) 
         context.StartActivity(typeof(Activity1)); 
       } 
       break; 
     } 
     return true; 
    } 
} 

ListItemModelが私の側で非常に簡単です:あなたはあなたが必要として、モデルとホルダーを変更しようとすることができ

public class ListItemModel 
{ 
    public string Name { get; set; } 
    public string Description { get; set; } 
} 

ここで私は、この機能を実装するためのアダプタを作成しました。

enter image description here

+0

@My_yzは、あなたが私のコードを試してみましたか?それは同じ振る舞いを持つはずです。 –

+0

@ My_yzの場合、それらは1つのアクティビティでなければなりません。私は新しい活動を始めました。 –

+1

@ My_yz、単純ではなく、同じアクティビティにある場合、レイアウトを変更する必要があります。おそらく、それは「ViewPager」なのかもしれません。ジェスチャの問題はこの答えで解決されます。次に、このレイアウトについて新しいスレッドを投稿する必要があります。 –

関連する問題