底部导航视图不更新RecyclerView

时间:2018-04-02 08:39:21

标签: android android-fragments bottomnavigationview

您好我在我的应用程序中使用底部导航,底部有三个标签,我有三个不同的片段。每当用户从底部更改选项卡时,它应显示相对的RecyclerView数据,但问题在于它显示一个RecyclerView的所有三个选项卡。它在所有三个选项卡中显示了ItemOneFragment的RecyclerView。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView bottomNavigationView = (BottomNavigationView)
                findViewById(R.id.navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener
                (new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                      //  Fragment selectedFragment = null;
                        switch (item.getItemId()) {
                            case R.id.navigation_home:
                                Fragment selectedFragment = ItemOneFragment.newInstance();
                                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                                transaction.replace(R.id.frame_layout, selectedFragment);
                                transaction.commit();
                                break;
                            case R.id.navigation_dashboard:
                                Fragment Englishsfrag = ItemTwoFragment.newInstance();
                                FragmentTransaction transactiontwo = getSupportFragmentManager().beginTransaction();
                                transactiontwo.replace(R.id.frame_layout, Englishsfrag);
                                transactiontwo.commit();
                                break;
                            case R.id.navigation_notifications:
                                Fragment mixfrag = ItemThreeFragment.newInstance();
                                FragmentTransaction transactionthree = getSupportFragmentManager().beginTransaction();
                                transactionthree.replace(R.id.frame_layout, mixfrag);
                                transactionthree.commit();
                                break;
                        }

                        return true;
                    }
                });

        //Manually displaying the first fragment - one time only
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, ItemOneFragment.newInstance());
        transaction.commit();

        //Used to select an item programmatically
        //bottomNavigationView.getMenu().getItem(2).setChecked(true);
    }
}

2 个答案:

答案 0 :(得分:1)

删除为每个案例创建 FragmentTransaction

bottomNavigationView.setOnNavigationItemSelectedListener
      (new BottomNavigationView.OnNavigationItemSelectedListener() {
      @Override
      public boolean onNavigationItemSelected(@NonNull MenuItem item) {
      Fragment selectedFragment = null;
      switch (item.getItemId()) {
      case R.id.action_item1:
      selectedFragment = ItemOnFragment.newInstance();
      break;
      case R.id.action_item2:
      selectedFragment = ItemTwoFragment.newInstance();
      break;
      case R.id.action_item3:
      selectedFragment = ItemThreeFragment.newInstance();
      break;
      case R.id.action_item4:
      selectedFragment = ItemFourFragment.newInstance();
      break;
      }
      FragmentTransaction transaction = getSupportFragmentManager().
    beginTransaction();
      transaction.replace(R.id.frame_layout, selectedFragment);
      transaction.commit();
      return true;
      }
      });

      //Manually displaying the first fragment - one time only
      FragmentTransaction transaction = getSupportFragmentManager().
    beginTransaction();
      transaction.replace(R.id.frame_layout, ItemOnFragment.newInstance());
      transaction.commit();

答案 1 :(得分:0)

我认为以下列方式做得更好

public class MainActivity extends AppCompatActivity {

Fragment fragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BottomNavigationView bottomNavigationView = (BottomNavigationView)
            findViewById(R.id.navigation);
    fragment = ItemOneFragment.newInstance();

    bottomNavigationView.setOnNavigationItemSelectedListener
            (new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    //  Fragment selectedFragment = null;
                    switch (item.getItemId()) {
                        case R.id.navigation_home:
                            fragment = ItemOneFragment.newInstance();
                            break;
                        case R.id.navigation_dashboard:
                            fragment = ItemTwoFragment.newInstance();
                            break;
                        case R.id.navigation_notifications:
                            fragment = ItemThreeFragment.newInstance();
                            break;
                    }
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame_layout, fragment);
                    transaction.commit();
                    return true;
                }
            });
}