当我单击SearchView时,不会触发Click事件(Xamarin.Android)

时间:2018-06-29 10:47:24

标签: xamarin.android click searchview

我有以下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上放置断点时,程序的执行就永远不会在那里停止。当我点击searchView小部件时如何使小部件可见/消失?

1 个答案:

答案 0 :(得分:2)

我要这样做的方法是,将焦点更改事件添加到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