Android RecyclerView将偶尔更新

Android RecyclerView将偶尔更新,android,android-recyclerview,Android,Android Recyclerview,我已经查看了此链接和其他链接: 无法找到解决我遇到的问题的方法。 在某些情况下,RecyclerView会更新其数据,而在其他情况下则不会 在我的代码中,我使用底部导航器,并相应地更新数据。 当我选择MAP,然后选择HISTORY,或者MAP,然后选择底部导航器附近的位置时,数据将更新并正常工作。 如果我选择了历史记录,那么附近的数据将不会更新。 我玩过这个,尝试过几个选项,但都没能让它工作 这是底部导航器中的代码: bottomNavigationView.setOnNavigationIt

我已经查看了此链接和其他链接:

无法找到解决我遇到的问题的方法。 在某些情况下,RecyclerView会更新其数据,而在其他情况下则不会

在我的代码中,我使用底部导航器,并相应地更新数据。 当我选择MAP,然后选择HISTORY,或者MAP,然后选择底部导航器附近的位置时,数据将更新并正常工作。 如果我选择了历史记录,那么附近的数据将不会更新。 我玩过这个,尝试过几个选项,但都没能让它工作

这是底部导航器中的代码:

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case (R.id.action_History):
                    showHistory();
                    break;

                case (R.id.action_near):
                    showNear();
                    break;

                case (R.id.action_fav):
                    showFav();
                    break;

                case (R.id.action_map):
                    showMap();
                    break;
                default:
                    break;
            }
            return true;
        }
    });
  private void showMap(){
    constraintMap.setVisibility(View.VISIBLE);

    fragmentManager
            .beginTransaction()
            .replace(container, emptyFragment)
            .commit();

}//showMap

private void showHistory(){
    Bundle bundle = new Bundle();
    bundle.putInt(Util.BOTTOM_NAVIGATOR, Util.HISTORY);
    placeListFragment.setArguments(bundle);

    constraintMap.setVisibility(View.GONE);

    fragmentManager
            .beginTransaction()
            .replace(container, placeListFragment)
            .commit();

}//showHistory

private void showNear(){
    Bundle bundle = new Bundle();
    bundle.putInt(Util.BOTTOM_NAVIGATOR, Util.NEAR);
    placeListFragment.setArguments(bundle);


    constraintMap.setVisibility(View.GONE);

    fragmentManager
            .beginTransaction()
            .replace(container, placeListFragment)
            .commit();

}//showNear
以下是从底部导航器调用的函数:

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case (R.id.action_History):
                    showHistory();
                    break;

                case (R.id.action_near):
                    showNear();
                    break;

                case (R.id.action_fav):
                    showFav();
                    break;

                case (R.id.action_map):
                    showMap();
                    break;
                default:
                    break;
            }
            return true;
        }
    });
  private void showMap(){
    constraintMap.setVisibility(View.VISIBLE);

    fragmentManager
            .beginTransaction()
            .replace(container, emptyFragment)
            .commit();

}//showMap

private void showHistory(){
    Bundle bundle = new Bundle();
    bundle.putInt(Util.BOTTOM_NAVIGATOR, Util.HISTORY);
    placeListFragment.setArguments(bundle);

    constraintMap.setVisibility(View.GONE);

    fragmentManager
            .beginTransaction()
            .replace(container, placeListFragment)
            .commit();

}//showHistory

private void showNear(){
    Bundle bundle = new Bundle();
    bundle.putInt(Util.BOTTOM_NAVIGATOR, Util.NEAR);
    placeListFragment.setArguments(bundle);


    constraintMap.setVisibility(View.GONE);

    fragmentManager
            .beginTransaction()
            .replace(container, placeListFragment)
            .commit();

}//showNear
这是片段中的onCreateView:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_list_view, container, false);

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());

    placesRecyclerView = view.findViewById(R.id.location_recycler_view);
    placesRecyclerView.setLayoutManager(linearLayoutManager);
    placesRecyclerView.setItemAnimator(new DefaultItemAnimator());


    int bottomNavigator;
    if(getArguments() != null){
        bottomNavigator = getArguments().getInt(Util.BOTTOM_NAVIGATOR);

        switch (bottomNavigator){
            case Util.HISTORY:
                placeLocations = dal.getAllPlaceLocationSearchResults();
                break;
            case Util.NEAR:
                placeLocations = dal.getAllPlaceLocationNearResults();
                break;
            case Util.FAVORITE:
                placeLocations = dal.getAllPlaceLocationFavoritesResults();
                break;
            case Util.MAP:
                break;
        }

    } else{
        placeLocations = dal.getAllPlaceLocationSearchResults();
    }

    placeListAdapter = new PlaceListAdapter(placeLocations, listener, mContext);
    placesRecyclerView.setAdapter(placeListAdapter);
    placeListAdapter.refreshListAdapter();


    return view;
}//onCreateView
正如Gal Yedidovich所建议的那样。 我已覆盖onStart。 但这没有帮助

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

    int bottomNavigator;
    if(getArguments() != null){
        bottomNavigator = getArguments().getInt(Util.BOTTOM_NAVIGATOR);

        switch (bottomNavigator){
            case Util.HISTORY:
                placeLocations = dal.getAllPlaceLocationSearchResults();
                break;
            case Util.NEAR:
                placeLocations = dal.getAllNearLocations();
                break;
            case Util.FAVORITE:
                placeLocations = dal.getAllPlaceLocationFavoritesResults();
                break;
            case Util.MAP:
                break;
        }

    } else{
        placeLocations = dal.getAllPlaceLocationSearchResults();
    }

    placeListAdapter = new PlaceListAdapter(placeLocations, listener, mContext);
    placesRecyclerView.setAdapter(placeListAdapter);
    placeListAdapter.refreshListAdapter();


}//onStart

无论何时更新recyclerview数据,都应该使用placeListAdapter.refreshListAdapter(),而不是placeListAdapter.notifydatasetchange()。

我不知道是否要实例化片段一次或多次,但我很确定这是因为片段的生命周期(如果只实例化一次)

覆盖片段中的“onStart”方法 在onStart方法中刷新适配器:

@Override
public void onStart() {
     int bottomNavigator;
     if(getArguments() != null){
        bottomNavigator = getArguments().getInt(Util.BOTTOM_NAVIGATOR);

        switch (bottomNavigator){  
            case Util.HISTORY:
                placeLocations = dal.getAllPlaceLocationSearchResults();
                break;
            case Util.NEAR:
                placeLocations = dal.getAllPlaceLocationNearResults();
                break;
            case Util.FAVORITE:
                placeLocations = dal.getAllPlaceLocationFavoritesResults();
                break;
            case Util.MAP:
                break;
        }

    } else{
        placeLocations = dal.getAllPlaceLocationSearchResults();
    }

    placeListAdapter = new PlaceListAdapter(placeLocations, listener, mContext);
    placesRecyclerView.setAdapter(placeListAdapter); //keep the recycler view as field in fragment calls

    //refresh your adapter
    placeListAdapter.notifyDataSetChanged();
    //or as you already wrote
    placeListAdapter.refreshListAdapter();
}
另一个选项:

请在每次对片段执行事务时尝试实例化它

private void showMap(){
    constraintMap.setVisibility(View.VISIBLE);

    emptyFragment = new EmptyFrag(); //instantiating

    fragmentManager
            .beginTransaction()
            .replace(container, emptyFragment)
            .commit();

}//showMap

private void showHistory(){
    Bundle bundle = new Bundle();
    bundle.putInt(Util.BOTTOM_NAVIGATOR, Util.HISTORY);
    placeListFragment.setArguments(bundle);

    constraintMap.setVisibility(View.GONE);

    placeListFragment = new YourFragment(); //instantiating

    fragmentManager
            .beginTransaction()
            .replace(container, placeListFragment)
            .commit();

}//showHistory

private void showNear(){
    Bundle bundle = new Bundle();
    bundle.putInt(Util.BOTTOM_NAVIGATOR, Util.NEAR);
    placeListFragment.setArguments(bundle);

    placeListFragment = new YourFragment(); //instantiating   

    constraintMap.setVisibility(View.GONE);

    fragmentManager
            .beginTransaction()
            .replace(container, placeListFragment)
            .commit();

}//showNear

那么,你也可以发布你的PlaeListAdapter的源代码吗?还有一个问题是,如果每次单击底部导航栏,您是否都在使用
placeListFragment
的单个实例,或者实例化此片段。
adapter.notifydatasetchange()?我很确定这是因为您不是每次都实例化您的片段,这就是为什么只有当片段被销毁时,才会调用方法“onCreateView”,并刷新适配器。另一种猜测是,问题不在于片段,而在于适配器或DAL。您应该调试它以查看数据是否准确谢谢您的重播。我试过你的建议,没什么区别。也许需要做些额外的事情?谢谢你重播。尝试你的想法,两种选择,结果都是一样的。我想把我的代码粘贴到这里,但不能…太棒了!很高兴这有帮助。