2017-07-19 69 views
1

我有一個Android應用程序有一個Horizo​​ntalScrollView嵌入兩個RecyclerViews。頂部RecyclerView有類別,當您選擇一個類別時,底部的RecyclerView會列出該類別中的項目。爲了檢測選定的類別,我嘗試使用Touch事件。當EditText獲取觸摸事件時,該行被選中。這部分工作很好。問題是我還需要能夠編輯類別列表中的某些字段。如果我在EditText上綁定Touch事件,則EditText在您點擊它時不會獲得焦點。我嘗試使用FocusChanged事件,但有時在水平滾動時,頂部RecyclerView的焦點被設置爲第一個類別的EditText,從而導致所選類別發生更改。有沒有辦法在MvvmCross中綁定Touch事件並仍然讓它導致綁定控件獲得焦點?MvvmCross綁定觸摸事件防止重點MvxItemTemplate

這是我的項目模板的佈局:

<?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="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="2dp" 
    android:paddingBottom="2dp" 
    local:MvxBind="This View"> 
    <TextView 
     android:layout_width="300dp" 
     android:layout_height="wrap_content" 
     style="@style/TableEditText" 
     local:MvxBind="Text Name; LongClick Name_LongClick; Touch Touch" 
     android:id="@+id/txtName" /> 
    <EditText 
     android:layout_width="110dp" 
     android:layout_height="wrap_content" 
     style="@style/TableEditText" 
     android:inputType="numberDecimal|numberSigned" 
     android:selectAllOnFocus="true" 
     local:MvxBind="Text Cubes, Converter=FloatToStringConverter; Touch Touch" 
     android:id="@+id/txtCubes" /> 
    <EditText 
     android:layout_width="110dp" 
     android:layout_height="wrap_content" 
     style="@style/TableEditText" 
     android:inputType="numberDecimal|numberSigned" 
     android:selectAllOnFocus="true" 
     local:MvxBind="Text Weight, Converter=IntToStringConverter; Touch Touch" 
     android:id="@+id/txtWeight" /> 
    <EditText 
     android:layout_width="500dp" 
     android:layout_height="wrap_content" 
     style="@style/TableEditText" 
     android:inputType="textCapSentences" 
     local:MvxBind="Text Note; Touch Touch" 
     android:id="@+id/txtNote" /> 
    <ImageButton 
     android:src="@drawable/ic_action_delete" 
     android:scaleType="fitCenter" 
     android:padding="3dp" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_gravity="center" 
     local:MvxBind="Click DeleteClicked" 
     android:id="@+id/btnDeleteCategory" /> 
</LinearLayout> 

這是我的觸摸事件掛鉤:

public MvxCommand Touch 
    { 
     get 
     { 
      return new MvxCommand(() => 
      { 
       SurveyView act = (Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity as SurveyView) ?? null; 
       if (act != null) 
       { 
        act.SetCurrCategory(this); 
       } 
      }); 
     } 
    } 

這是我定製的觸摸事件綁定:

public class MvxViewTouchBinding 
    : MvxAndroidTargetBinding 
{ 
    private readonly View _view; 
    private IMvxCommand _command; 

    public MvxViewTouchBinding(View view) : base(view) 
    { 
     _view = view; 
     _view.Touch += ViewOnTouch; 
    } 

    private void ViewOnTouch(object sender, View.TouchEventArgs eventArgs) 
    { 
     if (_command != null) 
     { 
      _command.Execute(); 
     } 
    } 

    public override void SetValue(object value) 
    { 
     _command = (IMvxCommand)value; 
    } 

    protected override void Dispose(bool isDisposing) 
    { 
     if (isDisposing) 
     { 
      _view.Touch -= ViewOnTouch; 
     } 
     base.Dispose(isDisposing); 
    } 

    protected override void SetValueImpl(object target, object value) 
    { 
    } 

    public override Type TargetType 
    { 
     get { return typeof(IMvxCommand); } 
    } 

    public override MvxBindingMode DefaultMode 
    { 
     get { return MvxBindingMode.OneWay; } 
    } 
} 

這是在Setup.cs中

 registry.RegisterCustomBindingFactory<View>("Touch", 
                view => new MvxViewTouchBinding(view)); 
+1

試試這個 - https://stackoverflow.com/a/36360795/2754727 – pnavk

+2

嘗試設置'eventArgs.Handled = false;' – Cheesebaron

+0

@Cheesebaron這樣做了!感謝奶酪! –

回答

0

我不得不把ViewOnTouch處理器更改爲:

private void ViewOnTouch(object sender, View.TouchEventArgs eventArgs) 
    { 
     eventArgs.Handled = false; 

     if (_command != null) 
     { 
      _command.Execute(); 
     } 
    } 

顯然已處理默認爲真,這將導致事件鏈終止這個被稱爲後。將其設置爲false會告訴系統該事件尚未正確處理,因此它會調用下一個處理程序。我猜測後續處理程序會設置焦點。謝謝@Cheesebaron