在片段之间移动时,片段中的ListView不会刷新

时间:2019-03-13 15:28:43

标签: java listview android-fragments google-cloud-firestore

从“活动”移到“片段”时,FragmentA中的

ListView会完美刷新。这是我在fragmentA中非常成功的代码,但是在此之前,这是我将在此之后显示代码的输出:

输出来自第一个查询

EAT
DRINK
POOP

来自第二个查询

DONALD TRUMP
BARRACK OBAMA
GEORGE BUSH

当我离开时,会获得高于输出的成功

FragmentA -> 活动->返回 FragmentA

当我这样移动时,

FragmentA -> FragmentB ->返回 FragmentA

ListView显示不必要的升序结果

// wrong
POOP
DRINK
EAT

// true
EAT
DRINK
EAT

,根本没有第二个查询。

非常感谢您的回复。在此之后显示代码之前, 这就是为什么为什么要转到“活动”然后成功刷新 因为我将Fragments放在MainActivity中,所以:

MainActiviy -> 活动->在MainActivity中再次调用Create FragmentA -> FragmentB ->不在MainActivity中调用onCreate

FragmentA内部下面的所有代码。

好,首先查询代码:

// when scroll reach bottom   
recyclerSeen.addOnScrollListener(new RecyclerView.OnScrollListener() {
  @Override
  public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);    

       // vertical scroll
       Boolean reachBottom = !recyclerView.canScrollVertically(-1);


       if (reachBottom) {
          secondQuery();
       }
  }
});


                Query firstQuery = firebaseFirestore
                        .collection("Posts")                        
                        .orderBy("timestamp", Query.Direction.DESCENDING)
                        .limit(3);    
                                firstQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
                    @Override
                    public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

                        if (isFirstPageFirstLoad) {
                            lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
                        }

                        for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                            if (doc.getType() == DocumentChange.Type.ADDED) {

                                //String postId = doc.getDocument().getId();
                                ContentProfile contentSeen = doc.getDocument().toObject(ContentProfile.class);

                                /* how to store multi ArrayList */

                                if (isFirstPageFirstLoad) {
                                    contentList.add(contentSeen);
                                } else {
                                    contentList.add(0, contentSeen);
                                }

                                adapterSeen.notifyDataSetChanged();
                            }
                        }

                        isFirstPageFirstLoad = false;
                    }
                });

第二个查询代码:

    public void loadMorePost() {       

        Query nextQuery = firebaseFirestore                    
                .orderBy("timestamp", Query.Direction.DESCENDING)
                .collection("Posts") 
                .startAfter(lastVisible)
                .limit(3);

        nextQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

                if (!documentSnapshots.isEmpty()) {

                    lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
                    for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                        if (doc.getType() == DocumentChange.Type.ADDED) {

                            //String postId = doc.getDocument().getId();
                            ContentProfile contentSeen = doc.getDocument().toObject(ContentProfile.class);
                            contentList.add(contentSeen);

                            adapterSeen.notifyDataSetChanged();

                        }
                    }
                }
            }
        });
    }

MainActivity.class

public class MainActivity extends AppCompatActivity {

    private HomeFragment homeFragment;
    private Search search;
    private CenterFragment centerFragment;
    private Notification notification;
    private ProfileFragment profileFragment;
    private BottomNavigationView bottomNavigation;

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

        homeFragment = new HomeFragment();
        search = new Search();
        centerFragment = new CenterFragment();
        notification = new Notification();
        profileFragment = new ProfileFragment();

        bottomNavigation = findViewById(R.id.bottom_navigation);
        BottomNavigationViewHelper.removeShiftAnimation(bottomNavigation);

        //initializeFragment
        replaceFragment(homeFragment);   


    }

    @Override
    public void onStart() {
        super.onStart();

        bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener(){
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item){

                switch(item.getItemId()){

                    case R.id.action_home :
                        replaceFragment(homeFragment);
                        return true;

                    case R.id.action_search :
                        replaceFragment(search);
                        return true;

                    case R.id.action_center :
                        replaceFragment(centerFragment);
                        return true;

                    case R.id.action_notif :
                        replaceFragment(notification);
                        return true;

                    case R.id.action_profile :
                        replaceFragment(profileFragment);
                        return true;

                    default:
                        return false;
                }
            }
        });
    }

    private void replaceFragment(Fragment fragment){
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        fragmentTransaction.replace(R.id.main_container, fragment);
        fragmentTransaction.commit();
    }


   }

我有一个解决上述问题的解决方案,但是我不知道如何刷新它,或者不手动调用MainActivity onCreate()remove()片段或whatever干净代码,我要做的就是将replace更改为hide。下面的这段代码就像一个魅力一样工作,我不必刷新列表。

public class MainActivity extends AppCompatActivity {

    private HomeFragment homeFragment;
    private Search search;
    private CenterFragment centerFragment;
    private Notification notification;
    private ProfileFragment profileFragment;
    private BottomNavigationView bottomNavigation;

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

        homeFragment = new HomeFragment();
        search = new Search();
        centerFragment = new CenterFragment();
        notification = new Notification();
        profileFragment = new ProfileFragment();

        bottomNavigation = findViewById(R.id.bottom_navigation);
        BottomNavigationViewHelper.removeShiftAnimation(bottomNavigation);

        initializeFragment();

        //replaceFragment(homeFragment);

        Toast.makeText(MainActivity.this, "MainActivity : onCreate() ", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onStart() {
        super.onStart();


        bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener(){
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item){

                Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.main_container);

                switch(item.getItemId()){

                    case R.id.action_home :
                        replaceFragment(homeFragment, currentFragment);
                        return true;

                    case R.id.action_search :
                        replaceFragment(search, currentFragment);
                        return true;

                    case R.id.action_center :
                        replaceFragment(centerFragment, currentFragment);
                        return true;

                    case R.id.action_notif :
                        replaceFragment(notification, currentFragment);
                        return true;

                    case R.id.action_profile :
                        replaceFragment(profileFragment, currentFragment);
                        return true;

                    default:
                        return false;
                }
            }
        });
    }

     private void initializeFragment(){

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        fragmentTransaction.add(R.id.main_container, homeFragment);
        fragmentTransaction.add(R.id.main_container, search);
        fragmentTransaction.add(R.id.main_container, centerFragment);
        fragmentTransaction.add(R.id.main_container, notification);
        fragmentTransaction.add(R.id.main_container, profileFragment);

        fragmentTransaction.hide(search);
        fragmentTransaction.hide(centerFragment);
        fragmentTransaction.hide(notification);
        fragmentTransaction.hide(profileFragment);

        fragmentTransaction.commit();

    }

//    private void replaceFragment(Fragment fragment){
//        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
//
//        fragmentTransaction.replace(R.id.main_container, fragment);
//        fragmentTransaction.commit();
//    }


    private void replaceFragment(Fragment fragment, Fragment currentFragment){

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if(fragment == homeFragment){

            fragmentTransaction.hide(search);
            fragmentTransaction.hide(centerFragment);
            fragmentTransaction.hide(notification);
            fragmentTransaction.hide(profileFragment);

        }

        if(fragment == search){

            fragmentTransaction.hide(homeFragment);
            fragmentTransaction.hide(centerFragment);
            fragmentTransaction.hide(notification);
            fragmentTransaction.hide(profileFragment);

        }

        if(fragment == centerFragment){

            fragmentTransaction.hide(search);
            fragmentTransaction.hide(homeFragment);
            fragmentTransaction.hide(notification);
            fragmentTransaction.hide(profileFragment);

        }

        if(fragment == notification){

            fragmentTransaction.hide(search);
            fragmentTransaction.hide(centerFragment);
            fragmentTransaction.hide(homeFragment);
            fragmentTransaction.hide(profileFragment);

        }

        if(fragment == profileFragment){

            fragmentTransaction.hide(search);
            fragmentTransaction.hide(centerFragment);
            fragmentTransaction.hide(notification);
            fragmentTransaction.hide(homeFragment);

        }

        fragmentTransaction.show(fragment);

        //fragmentTransaction.replace(R.id.main_container, fragment);
        fragmentTransaction.commit();

    }

0 个答案:

没有答案