【问题标题】:Prevent fragment refreshing with bottom navbar使用底部导航栏防止片段刷新
【发布时间】:2019-10-27 05:30:06
【问题描述】:

我有以下底部导航栏代码在 3 个片段之间切换:

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment = null;

            switch (item.getItemId()) {
                case R.id.navigation_home:
                    fragment = new HomeFragment();
                    break;

                case R.id.navigation_dashboard:
                    fragment = new DashboardFragment();
                    break;

                case R.id.navigation_notifications:
                    fragment = new NotificationsFragment();
                    break;

            }

            return loadFragment(fragment);
        }

    };

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

        loadFragment(new HomeFragment());


        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    private boolean loadFragment(Fragment fragment) {
        //switching fragment
        if (fragment != null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment_container, fragment)
                    .commit();
            return true;
        }
        return false;
    }
}

在片段中有带列表的 RecyclerViews。每次我在选项卡之间(片段之间)切换时,看起来片段被重新加载,并且列表跳到顶部。我想防止重新加载,以便用户在切换片段之前留在他查看的列表中的同一位置

【问题讨论】:

标签: android android-fragments


【解决方案1】:

问题是您每次都在创建一个新实例。您可以像这样缓存实例:

    private Fragment mHomeFragment = new HomeFragment();
    private Fragment mDashboardFragment = new DashboardFragment();
    private Fragment mNotificationsFragment = new NotificationsFragment();


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment = null;

        switch (item.getItemId()) {
            case R.id.navigation_home:
                fragment = mHomeFragment;
                break;

            case R.id.navigation_dashboard:
                fragment = mDashboardFragment;
                break;

            case R.id.navigation_notifications:
                fragment = mNotificationsFragment;
                break;

        }

        return loadFragment(fragment);
    }

【讨论】:

  • 你能告诉我上面代码中缓存fragment的语句在哪里吗?
  • 在创建字段时正在创建实例,在您的情况下,每次调用 onNavigationItemSelected 时都会创建实例。
【解决方案2】:

正如我们所见,当您点击底部导航时,您总是会替换您的片段,替换意味着删除之前的片段并清除状态。解决方案是不要每次都创建片段并使用附加/分离方法来显示实际片段。 Here is already described 关于这些方法。

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多