Listview刷新(在fragment和viewpager中)

时间:2017-04-12 15:20:50

标签: android

如何在fragement中刷新listview? 现在,仅当键盘输入消失时才会刷新listview 我该怎么做才能刷新listview 这是创建viewpager的主要活动。

      ```java

        public class Login extends AppCompatActivity {

            private FirebaseAuth mFirebaseAuth;
            private FirebaseAuth.AuthStateListener mAuthStateListener;

            public static final int RC_SIGN_IN = 1;

            private TabLayout tabLayout;
            private ViewPager viewPager;
            TabPagerAdapter pagerAdapter;


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

                mFirebaseAuth = FirebaseAuth.getInstance();
                mAuthStateListener = new FirebaseAuth.AuthStateListener() {
                    @Override
                    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                        FirebaseUser user = firebaseAuth.getCurrentUser();
                        if (user != null) {

                        } else {
                            startActivityForResult(
                                    AuthUI.getInstance()
                                            .createSignInIntentBuilder()
                                            .setTheme(R.style.FullscreenTheme)
                                            .setIsSmartLockEnabled(false)
                                            .setProviders(
                                                    AuthUI.EMAIL_PROVIDER,
                                                    AuthUI.GOOGLE_PROVIDER,
                                                    AuthUI.FACEBOOK_PROVIDER,
                                                    AuthUI.TWITTER_PROVIDER)
                                            .build(),
                                    RC_SIGN_IN);
                        }
                    }
                };


                tabLayout = (TabLayout) findViewById(R.id.tabLayout);
                tabLayout.addTab(tabLayout.newTab().setText("Category"));
                tabLayout.addTab(tabLayout.newTab().setText("Search"));
                tabLayout.addTab(tabLayout.newTab().setText("My page"));
                tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

                viewPager = (ViewPager) findViewById(pager);
                pagerAdapter = new TabPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());

                viewPager.setAdapter(pagerAdapter);
                viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));


                tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        viewPager.setCurrentItem(tab.getPosition());
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });

            }


            @Override
            protected void onPause() {
                super.onPause();
                if (mAuthStateListener != null) {
                    mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
                }
            }

            @Override
            protected void onResume() {
                super.onResume();
                mFirebaseAuth.addAuthStateListener(mAuthStateListener);
            }

        }

        ```

这是创建listview的地方。 我是否必须在这里做一些事情来刷新listview?

    ```java
    public class SearchFragment extends android.support.v4.app.Fragment {
        FirebaseDatabase mFirebaseDatabase;


        public SearchFragment() {
            // Required empty public constructor
        }

        Spinner spinner;
        String[] categories = {"Family", "Love", "Friends", "Job", "Happiness", "Encouragement"};
        Context mCotext;
        String category;
        EditText editText;
        String searchTarget;
        Button searchBtn;
        ListView listView;
        private PostAdapter mPostAdapter;
        List<PostItem> postItems;
        InputMethodManager inputMethodManager;


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.fragment_search, null);

            mCotext = getActivity().getApplicationContext();

            // spinner part -> get target search category
            spinner = (Spinner) view.findViewById(R.id.spinner);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(mCotext, android.R.layout.simple_spinner_dropdown_item, categories);
            spinner.setAdapter(adapter);

            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    category = categories[position];
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    category = categories[0];
                }
            });


            editText = (EditText) view.findViewById(R.id.searchBox);


            searchBtn = (Button) view.findViewById(R.id.searchBtn);
            searchBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPostAdapter.clear(); //리스트 어답터를 비워준다
                    searchTarget = editText.getText().toString();
                    serach(category, searchTarget);
                    mPostAdapter.notifyDataSetChanged();

                    //downKeyboard(mCotext, editText);
                }
            });

            listView = (ListView) view.findViewById(R.id.list);
            postItems = new ArrayList<>();
            mPostAdapter = new PostAdapter(mCotext, R.layout.post_item, postItems);
            listView.setAdapter(mPostAdapter);

            return view;
        }

        private void serach(String category, final String target) {

            if (target.trim().equals("")) {
                Toast.makeText(mCotext, "Please type something to search", Toast.LENGTH_LONG).show();
            } else {
                mFirebaseDatabase = FirebaseDatabase.getInstance();

                Query postSearchQuery = mFirebaseDatabase.getReference().child(category).limitToFirst(30);

                postSearchQuery.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                        for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                            if (postSnapshot.getValue().toString().contains(target)) {
                                PostItem post = dataSnapshot.getValue(PostItem.class);
                                mPostAdapter.insert(post, 0);
                            }
                        }
                    }

                    @Override
                    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                    }

                    @Override
                    public void onChildRemoved(DataSnapshot dataSnapshot) {

                    }

                    @Override
                    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

            }


        }


        public static void downKeyboard(Context context, EditText editText) {
            InputMethodManager mInputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            mInputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);

        }




    }

    ```

1 个答案:

答案 0 :(得分:0)

可能是这样的:

adapter.notifyDataSetChanged();