3

我已经毫无问题地实施ActionBarSherlock到我的android项目中。我现在正在尝试将 a 添加search widget到 ActionBar 并已成功完成。现在,我想让Searchview幻灯片在展开时从右侧滑入,在折叠时从右侧滑出。我有两个 XML 动画,我相信它们可以让我这样做。

search_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >

<translate
    android:duration="1000"
    android:fromXDelta="100%"
    android:fromYDelta="0%"
    android:toXDelta="0%"
    android:toYDelta="0%" />

</set>

search_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >

<translate
    android:duration="700"
    android:fromXDelta="0%"
    android:fromYDelta="0%"
    android:toXDelta="100%"
    android:toYDelta="0%" />

</set>

它们在 SherlockFragmentActivity 中被称为表单

public boolean onCreateOptionsMenu(Menu menu) {
    ...
final SearchView searchView = new SearchView(this.getSupportActionBar().getThemedContext());
    final Animation in = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.search_in);
    final Animation out = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.search_out);

    out.setAnimationListener(new Animation.AnimationListener(){

        @Override
        public void onAnimationEnd(Animation animation) {
            searchView.setIconified(true);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

    });

     menu.add("Search")
        .setIcon(android.R.drawable.ic_menu_search)
        .setActionView(searchView)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    final MenuItem searchMenuItem = menu.getItem(0);

    searchView.setQueryHint(getResources().getText(R.string.searchHint));
    searchView.setIconifiedByDefault(true);
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean queryTextFocused) {

            if(!queryTextFocused){
                searchView.startAnimation(out);
                searchMenuItem.collapseActionView();
            }
            else
                searchView.startAnimation(in);
        }
    });
    return super.onCreateOptionsMenu(menu);
}//end onCreateOptionsMenu
}

这是它现在的样子(因为我还不能发布图片......):

http://i194.photobucket.com/albums/z31/raitono/SearchviewCollapseProblem_zpsefee50e9.gif

search_in点击图标时动画效果search_out很好,当按下后退硬件键时效果也很好。不过,当我尝试通过小 X 折叠 Searchview 时遇到了障碍。似乎 Searchview 在调用动画之前就崩溃了,我不知道该怎么办。我也试过searchView.onActionViewCollapsed()了,但是没有用。我的猜测是,我需要以某种方式拦截按下 X 时发出的调用并首先执行动画。

我正在尝试支持低至 2.3.5 的设备。对此的任何帮助将不胜感激。

更新:是否有办法检测小“X”何时被点击?那将解决我的问题。我一直在谷歌搜索,但找不到任何东西。

4

1 回答 1

1

我不确定 ActionBarSherlock。但在 android SearchView 小部件中,您可以使用它

searchView = (SearchView) menu.findItem(R.id.action_search)
                .getActionView();
        //Get the ID for the search bar LinearLayout
        int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null);
        //Get the search bar Linearlayout
        LinearLayout searchBar = (LinearLayout) searchView.findViewById(searchBarId);
        //Give the Linearlayout a transition animation.
        searchBar.setLayoutTransition(new LayoutTransition());
于 2014-08-08T16:36:47.477 回答