导航抽屉后按的片段未加载

时间:2018-07-06 12:43:42

标签: java android android-fragments android-recyclerview

我有一个TypeError: obj.hasOwnProperty is not a function 。一开始,我默认显示navigation drawer with fragments

导航菜单上有选项。在2个片段中,我有Home fragment。在提取所有SwipeRefreshLayout数据之前,我将显示数据并显示不可见的recyclerview

其中一个片段(包含在SwipeRefreshLayout中)可以正常工作,但在Home片段中出了点问题。

例如(用例)

您启动了应用程序,看到了Home片段

您点击了导航菜单上的个人资料片段

您运行onBackPressed(后退按钮)。

在这种情况下,数据永远不会加载,并且SwipeRefReshLayout始终在旋转。 (我也尝试过不刷新,仍然一样)

有人知道如何解决此问题吗?我的想法是,它与显示默认片段有关。

导航抽屉活动

SwipeRefReshLayout

首页片段

public class Page_Navigation extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

    Fragment fragment;
    FragmentManager fragmentManager = getSupportFragmentManager();
    NavigationView navigationView;

    SharedPreferences mSharedPref;
    DrawerLayout drawer;
    private Tracker mTracker;
    FragmentTransaction fragmentTransaction;

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

        //
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView toolbar_head = findViewById(R.id.toolbar_head);
        ImageView toolbar_image = findViewById(R.id.toolbar_image);
        ImageView toolbar_profile = findViewById(R.id.toolbar_profile);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        //

        AnalyticsApplication application = (AnalyticsApplication) getApplication();
        mTracker = application.getDefaultTracker();
        mTracker.setScreenName("page_navigation");
        mTracker.send(new HitBuilders.ScreenViewBuilder().build());


        //FIRST SETTINGS
        setSupportActionBar(toolbar);
        Typeface customFont = Typeface.createFromAsset(getAssets(), "Montserrat-Medium.ttf");
        toolbar_head.setTypeface(customFont);

        //Get Sessions
        mSharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String session_user_name = mSharedPref.getString("session_user_name", "");
        String session_user_photo = mSharedPref.getString("session_user_photo", "");

        //Navigation Drawer
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setItemIconTintList(null);
        //

        View headerView = navigationView.getHeaderView(0);
        TextView nav_userName = (TextView) headerView.findViewById(R.id.textView_nav_userName);
        CircleImageView imageView_navigation = (CircleImageView) headerView.findViewById(R.id.imageView_navigation);
        Glide.with(getApplicationContext()).load(session_user_photo).into(imageView_navigation);
        nav_userName.setText(session_user_name);

        headerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (drawer.isDrawerOpen(Gravity.START)) {
                    drawer.closeDrawer(Gravity.START);
                }

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        showProfileFragment();
                    }
                }, 300);
            }
        });


        toolbar_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawer.openDrawer(GravityCompat.START);
            }
        });

        toolbar_profile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment;
                FragmentManager manager = getSupportFragmentManager();
                fragment = new Nav_Profile();
                fragmentTransaction = manager.beginTransaction();
                fragmentTransaction.replace(R.id.navContent, fragment).addToBackStack(null).commit();
            }
        });

        displayDefaultFragment();

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.page__navigation, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }


        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.

        final int id = item.getItemId();


        drawer.closeDrawer(GravityCompat.START);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                switch (id) {
                    case R.id.nav_home:
                        fragment = new Nav_Home();
                        break;

                    case R.id.nav_contact:
                        fragment = new Nav_Contact();
                        break;

                    case R.id.nav_articles:
                        fragment = new Nav_Article();
                        break;

                    case R.id.nav_about:
                        fragment = new Nav_AboutUs();
                        break;

                    case R.id.nav_suggest:
                        fragment = new Nav_Suggest();
                        break;

                    case R.id.nav_share:
                        fragment = new Nav_Share();
                        break;

                    case R.id.nav_rateApp:
                        fragment = new Nav_RateApp();
                        break;


                }

                fragmentManager.beginTransaction()
                        .replace(R.id.navContent, fragment)
                        .addToBackStack(null)
                        .commit();

            }
        }, 350);


        return true;
    }

    public void displayDefaultFragment() {
        fragment = new Nav_Home();
        fragmentManager.beginTransaction().replace(R.id.navContent, fragment).commit();

    }


    public void showProfileFragment() {
        fragment = new Nav_Profile();
        fragmentManager.beginTransaction().replace(R.id.navContent, fragment).addToBackStack(null).commit();
    }
}

1 个答案:

答案 0 :(得分:1)

首先,成功或未获取数据时必须禁用SwipeRefreshLayout旋转:

swipeRefresh_home.setRefreshing(false);

如果您不这样做,则微调器将一直旋转。

另一个问题是您有一个片段,并尝试为其分配Nav_Home片段和Nav_Profile片段。

Fragment fragment;

public void displayDefaultFragment() {
    fragment = new Nav_Home();
    fragmentManager.beginTransaction().replace(R.id.navContent, fragment).commit();
}

public void showProfileFragment() {
    fragment = new Nav_Profile();
    fragmentManager.beginTransaction().replace(R.id.navContent, fragment).addToBackStack(null).commit();
}

尝试将它们分开并像这样显示:

Fragment homeFragment;
Fragment profileFragment;

public void displayDefaultFragment() {
    homeFragment = new Nav_Home();
    fragmentManager.beginTransaction().replace(R.id.navContent, homeFragment).commit();
}

public void showProfileFragment() {
    profileFragment = new Nav_Profile();
    fragmentManager.beginTransaction().replace(R.id.navContent, profileFragment).addToBackStack(null).commit();
}