我可以这样做,将焦点更改事件添加到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