简体   繁体   English

ActionBarSherlock中的SearchView折叠动画

[英]SearchView collapse animation in ActionBarSherlock

I have implemented ActionBarSherlock into my android project with no problems. 我已经将ActionBarSherlock实施到我的android项目中,没有任何问题。 I am now trying to add a search widget to the ActionBar and have done that successfully. 我现在正在尝试将search widget添加到ActionBar并成功完成。 Now, I would like to have the Searchview slide in from the right when expanded and out to the right when collapsed. 现在,我想让Searchview幻灯片在展开时从右侧滑入,而在折叠时Searchview滑入。 I have two XML animations that I believe allow me to do that. 我有两个XML动画,我相信可以做到这一点。

search_in.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 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>

They are called form inside a SherlockFragmentActivity 它们在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
}

Here is how it looks now(since I can't post images yet...): 这是现在的样子(因为我还不能发布图片...):

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

The search_in animation works beautifully when the icon is tapped and the search_out works as well, when the back hardware key is pressed. 轻按图标时, search_in动画效果很好,按下后部硬件键时, search_out效果也很好。 I run into a snag when I try to collapse the Searchview through the small X though. 当我尝试通过小X折叠Searchview时遇到障碍。 It seems that the Searchview is collapsing before the animation is called and I don't know what to do about it. 似乎在调用动画之前Searchview崩溃了,我不知道该怎么做。 I've also tried searchView.onActionViewCollapsed() but that did no good. 我也尝试过searchView.onActionViewCollapsed()但这没有任何好处。 My guess is that I need to somehow intercept the call that is made when that X is pressed and do the animation first. 我的猜测是,我需要以某种方式拦截在按下X时所进行的调用,并先执行动画。

I am attempting to support devices as low as 2.3.5. 我正在尝试支持低至2.3.5的设备。 Any help with this would be greatly appreciated. 任何帮助,将不胜感激。

Update: Is there anyway to detect when the small 'X' is tapped? 更新:是否有检测小“ X”被点击的时间? That would solve my problem. 那会解决我的问题。 I've been Googling but haven't been able to find anything. 我一直在谷歌搜索,但没有找到任何东西。

I'm not sure about ActionBarSherlock. 我不确定ActionBarSherlock。 But in android SearchView widget you can use this 但是在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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM