代码之家  ›  专栏  ›  技术社区  ›  ProfileForStack4

当我单击SearchView时,单击事件不会触发(xamarin.android)

  •  1
  • ProfileForStack4  · 技术社区  · 6 年前

    我有以下内容 SearchView

    <SearchView
        android:minWidth="25px"
        android:minHeight="25px"
        android:background="@drawable/rounded_border"
        android:clickable="true"
        android:iconifiedByDefault="false"
        android:focusable="false"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/searchViewCustomers" />
    

    当我单击它时,我希望活动的一个小部件成为 visible 另一个变成 gone 就像这样:

    searchView.Click += delegate
    {
        customersRecyclerView.Visibility = ViewStates.Visible;
        customerDataContainer.Visibility = ViewStates.Gone;
    };
    

    但是当我运行应用程序并点击 searchView 小部件不会变成 visible/gone 当我打开断点时 serachView.Click ,程序执行永远不会停止。当我点击 搜索视图 小装置?

    1 回复  |  直到 6 年前
        1
  •  2
  •   FreakyAli    6 年前

    我可以这样做,将焦点更改事件添加到SearchView:

     searchView.FocusChange += SearchView_FocusChange;
     private void SearchView_FocusChange(object sender, View.FocusChangeEventArgs e)
        {
                  if(searchView.HasFocus)
                  {
                   //Visibility code
                  }
        }
    

    首先,我将使用AppCompat SearchView实现兼容性,如下所示:

    <android.support.v7.widget.SearchView
    android:minWidth="25px"
    android:minHeight="25px"
    android:background="@drawable/rounded_border"
    android:clickable="true"
    android:iconifiedByDefault="false"
    android:focusable="false"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/searchViewCustomers" />
    

    如果这不起作用,我将在查询焦点更改侦听器上使用类似的方法:

    searchView.SetOnQueryTextFocusChangeListener(new FocusChangeListenerClass());
    

    public class FocusChangeListenerClass : Java.Lang.Object, IOnFocusChangeListener
    {
        public void OnFocusChange(Android.Views.View v, bool hasFocus)
        {
    
        }
    }
    

    Activity:

    public class ClientsActivity : Activity
    {
        .......
        .......
        Android.Widget.SearchView searchView;
        RecyclerView customersRecyclerView;
        ScrollView customerDataContainer;
        .......
        protected override void OnCreate(Bundle savedInstanceState)
        {
             ........
             ........
             searchView.SetOnQueryTextFocusChangeListener(new FocusChangeListenerClass(ref customersRecyclerView, ref customerDataContainer));
        }
    }
    

    FocusChangeListenerClass.cs:

    public class FocusChangeListenerClass : Java.Lang.Object, IOnFocusChangeListener
    {
        RecyclerView customersRecyclerView;
        ScrollView customerDataContainer;
        public FocusChangeListenerClass(ref RecyclerView recyclerView, ref ScrollView dataContainer)
        {
            customersRecyclerView = recyclerView;
            customerDataContainer = dataContainer;
        }
    
        public void OnFocusChange(Android.Views.View v, bool hasFocus)
        {
            if(hasFocus == true)
            {
                customersRecyclerView.Visibility = ViewStates.Visible;
                customerDataContainer.Visibility = ViewStates.Gone;
            }
            else
            {
                customersRecyclerView.Visibility = ViewStates.Gone;
                customerDataContainer.Visibility = ViewStates.Visible;
            }
        }
    
    }
    

    <SearchView> <android.support.v7.widget.SearchView> .axml