I have a problem: I have a listView
in a Fragment
, I implemented a searchView (actionView) to filter the list in the listView
with the searchView onQueryTextChange
event. I don't have problems in this part and everything works fine.
我有一个问题:我在片段中有一个listView,我实现了一个searchView(actionView)来使用searchView onQueryTextChange事件过滤listView中的列表。我在这部分没有问题,一切正常。
adapter = new ListaEmpleadosAdapter(getActivity(), empleadoItems, context);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_buscar).getActionView();
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
try {
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
} catch (Exception e) {
System.err.println("ERROR FragmentEmpleado -> " + e.toString());
}
return true;
}
};
When I search something and press back button in the first time, the soft keyboard is closed. That's normal. When I press back button again, the searchView is closed, that's normal, but the list doesn't return to have the initial items.
当我第一次搜索并按下后退按钮时,软键盘关闭。这很正常。当我再次按下后退按钮时,searchView关闭,这是正常的,但列表没有返回到初始项目。
How I can get the event when searchView is collapsed (because searchView.setOnCloseListener
doesn't work) to restore the initial list items? Or any way to restore the initial list items deleting the adapter filter...
当searchView折叠时(因为searchView.setOnCloseListener不起作用)恢复初始列表项时,我如何获取事件?或者以任何方式恢复删除适配器过滤器的初始列表项...
HISTORY:
历史:
- The list of employees
- 员工名单
- The list of employees with a specific filter (after the first back button press, the keyboard is closed)
- 具有特定过滤器的员工列表(按下第一个后退按钮后,键盘关闭)
- The list of employees with searchView closed (but no restore the original list after second back button press)
- 已关闭searchView的员工列表(但在按下第二个按钮后没有恢复原始列表)
Thanks a lot!
非常感谢!
1 个解决方案
#1
5
There is a method on SearchView
that gets fired when the search view collapses (onActionViewCollapsed
) but there is no event for it. To get around it, I created my own class inheriting from SearchView
and defining 2 events to capture collapsing and expanding. The class also provides methods to hook up your event handlers.
SearchView上有一个方法在搜索视图崩溃时被触发(onActionViewCollapsed),但没有任何事件。为了解决这个问题,我创建了自己的继承自SearchView的类,并定义了2个事件来捕获折叠和扩展。该类还提供了连接事件处理程序的方法。
public class MySearchView extends SearchView {
OnSearchViewCollapsedEventListener mSearchViewCollapsedEventListener;
OnSearchViewExpandedEventListener mOnSearchViewExpandedEventListener;
public MySearchView(Context context) {
super(context);
}
@Override
public void onActionViewCollapsed() {
if (mSearchViewCollapsedEventListener != null)
mSearchViewCollapsedEventListener.onSearchViewCollapsed();
super.onActionViewCollapsed();
}
@Override
public void onActionViewExpanded() {
if (mOnSearchViewExpandedEventListener != null)
mOnSearchViewExpandedEventListener.onSearchViewExpanded();
super.onActionViewExpanded();
}
public interface OnSearchViewCollapsedEventListener {
public void onSearchViewCollapsed();
}
public interface OnSearchViewExpandedEventListener {
public void onSearchViewExpanded();
}
public void setOnSearchViewCollapsedEventListener(OnSearchViewCollapsedEventListener eventListener) {
mSearchViewCollapsedEventListener = eventListener;
}
public void setOnSearchViewExpandedEventListener(OnSearchViewExpandedEventListener eventListener) {
mOnSearchViewExpandedEventListener = eventListener;
}
}
#1
5
There is a method on SearchView
that gets fired when the search view collapses (onActionViewCollapsed
) but there is no event for it. To get around it, I created my own class inheriting from SearchView
and defining 2 events to capture collapsing and expanding. The class also provides methods to hook up your event handlers.
SearchView上有一个方法在搜索视图崩溃时被触发(onActionViewCollapsed),但没有任何事件。为了解决这个问题,我创建了自己的继承自SearchView的类,并定义了2个事件来捕获折叠和扩展。该类还提供了连接事件处理程序的方法。
public class MySearchView extends SearchView {
OnSearchViewCollapsedEventListener mSearchViewCollapsedEventListener;
OnSearchViewExpandedEventListener mOnSearchViewExpandedEventListener;
public MySearchView(Context context) {
super(context);
}
@Override
public void onActionViewCollapsed() {
if (mSearchViewCollapsedEventListener != null)
mSearchViewCollapsedEventListener.onSearchViewCollapsed();
super.onActionViewCollapsed();
}
@Override
public void onActionViewExpanded() {
if (mOnSearchViewExpandedEventListener != null)
mOnSearchViewExpandedEventListener.onSearchViewExpanded();
super.onActionViewExpanded();
}
public interface OnSearchViewCollapsedEventListener {
public void onSearchViewCollapsed();
}
public interface OnSearchViewExpandedEventListener {
public void onSearchViewExpanded();
}
public void setOnSearchViewCollapsedEventListener(OnSearchViewCollapsedEventListener eventListener) {
mSearchViewCollapsedEventListener = eventListener;
}
public void setOnSearchViewExpandedEventListener(OnSearchViewExpandedEventListener eventListener) {
mOnSearchViewExpandedEventListener = eventListener;
}
}