使用searchview搜索时,后台动画停止

时间:2018-06-10 17:07:30

标签: android android-animation searchview

我有一个活动,其中有一个用户列表,在回收站视图中显示。另外,我有一个带有searchview小部件的菜单。这是一张图片:

How the activity looks like

正如你在图片中看到的那样,我也有一个泡泡背景。这些气泡是动画的一部分,它们正在屏幕上移动。

现在,每当我按下搜索按钮时,动画就会停止,我所做的一切(除了完成活动之外)都会继续动画。

此外,这个问题只发生在我使用回收站视图时,我知道因为事先我有一个listview并且它工作得很好,但后来我决定改为适配器视图。

以下是一些代码段:

活动:

public class FollowingActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;

    ArrayList<String> member_names = new ArrayList<>();
    ArrayList<String> users_uid = new ArrayList<>();

    ValueEventListener uidListener;
    ValueEventListener namesListener;



    ArrayList<RowItem> rowItems = new ArrayList<>();
    RecyclerView recyclerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friends_search);


        Toolbar toolbar = findViewById(R.id.toolbar_add_friends);
        setSupportActionBar(toolbar);
        ActionBar actionbar = getSupportActionBar();
        if(actionbar != null) {
            actionbar.setDisplayHomeAsUpEnabled(true);
            actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);
        }


        setUpRecyclerView();



        mDrawerLayout = findViewById(R.id.drawer_layout);
        final DatabaseReference accountRef = FirebaseDatabase.getInstance().getReference().child("users");


        final ImageView drawerAccountImageView = findViewById(R.id.drawer_image);
        drawerAccountImageView.setImageResource(R.drawable.user);

        final DatabaseReference friendsRef = FirebaseDatabase.getInstance().getReference().child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Following");


        uidListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                users_uid = new ArrayList<>();
                member_names = new ArrayList<>();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                    if(!users_uid.contains(snapshot.getKey()))
                        users_uid.add(snapshot.getKey());
                    friendsRef.removeEventListener(this);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }




        };

        namesListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                TextView nameView = mDrawerLayout.findViewById(R.id.drawer_name);

                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                    User user = snapshot.getValue(User.class);

                    /*check if the user wasn't added before*/


                    if ((user != null) && user.email.equals(FirebaseAuth.getInstance().getCurrentUser().getEmail())) {
                        String fullName = user.firstName + " " + user.lastName;
                        nameView.setText(fullName);


                    }
                    String fullName;
                    if(user != null) {
                        fullName = user.firstName + " " + user.lastName;

                        for(int i=0; i<users_uid.size(); i++){
                            for(int j=i+1; j<users_uid.size(); j++){
                                if(users_uid.get(i).equals(users_uid.get(j)))
                                    users_uid.remove(j);
                            }
                        }

                        for (int i = 0; i < users_uid.size(); i++) {
                            if (users_uid.get(i).equals(snapshot.getKey())) {
                                member_names.add(fullName);
                            }
                        }
                    }
                }

                rowItems = new ArrayList<>();

                for (int i = 0; i < users_uid.size(); i++) {
                    RowItem item = new RowItem(users_uid.get(i), member_names.get(i));
                    rowItems.add(item);


                }

                RelativeLayout mRelativeLayout = findViewById(R.id.zero_following_text);

                if(users_uid.size() == 0){
                    mRelativeLayout.setVisibility(View.VISIBLE);
                }
                else{
                    mRelativeLayout.setVisibility(View.GONE);
                }

                FollowingCustomAdapter adapter = new FollowingCustomAdapter(FollowingActivity.this, rowItems);
                recyclerView.setAdapter(adapter);

                accountRef.removeEventListener(this);


            }




            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };


        friendsRef.addValueEventListener(uidListener);


        accountRef.addValueEventListener(namesListener);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        final SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(true);
        HelperMethods.changeSearchViewTextColor(searchView);



        return true;
    }

    @Override
    protected void onRestart() {
        final DatabaseReference accountRef = FirebaseDatabase.getInstance().getReference().child("users");

        final DatabaseReference friendsRef = FirebaseDatabase.getInstance().getReference().child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Following");

        friendsRef.addValueEventListener(uidListener);
        accountRef.addValueEventListener(namesListener);

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.getMenu().findItem(R.id.nav_search_for_friends).setChecked(true);



        super.onRestart();
    }

    private void setUpRecyclerView() {
        recyclerView = findViewById(R.id.following_recycler_view);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

}

options_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <item
        android:title="@string/search_title"
        android:id="@+id/action_search"
        android:icon="@drawable/ic_search_final"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView" />
</menu>

有人可以帮助我让动画继续吗?

编辑:当我隐藏循环视图时,它可以正常工作。我不知道问题是什么

0 个答案:

没有答案