活动显示与方向更改时的片段重叠

时间:2019-02-12 02:10:07

标签: android android-fragments navigation-drawer device-orientation

我正在制作一个使用导航抽屉在片段和活动之间更改的应用程序。

我可以使用导航抽屉在活动(包含空视图或recyclerview)和片段之间进行切换,但是当我加载片段并从视图(空视图或recyclerview)隐藏活动时更改方向时,活动布局方向改变后还原后,片段将与片段重叠。 emptyview / recyclerview不再隐藏。我想要的是在方向更改后使片段保持加载状态,并使活动布局保持隐藏状态。我已经尝试过if (savedInstanceState == null),但这没用。

这是我的代码。任何帮助将有助于解决此问题。

     public class DisplayActivity extends AppCompatActivity implements    
     LoaderManager.LoaderCallbacks<Cursor>, 
      NavigationView.OnNavigationItemSelectedListener,DefinitionFragment.OnFragmentInteractionListener,WeatherFragment.OnFragmentInteractionListener,PlanetFragment.OnFragmentInteractionListener{

    private static final int ASTRONOMY_LOADER = 0;
    private CustomCursorAdapter mAdapter;
    RecyclerView mRecyclerView;
    ViewGroup emptyView;
    private Fragment fragment;
    private FragmentTransaction fragmentTransaction;
    private FloatingActionButton fab;
    private static final  String TAG = "Fragment";




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        if (savedInstanceState == null){
                fab.hide();
                mRecyclerView.setVisibility(View.GONE);
                emptyView.setVisibility(View.GONE);


            }

            fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(DisplayActivity.this, EditorActivity.class);
                    startActivity(intent);
                }
            });

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.addDrawerListener(toggle);
            toggle.syncState();

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


            mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
            emptyView = findViewById(R.id.empty_view);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(), DividerItemDecoration.VERTICAL));


            //Setup an Adapter to create the recycler items for each row of prescription data in the Cursor.
            //There is no prescription data yet (until the loader finishes) so pass in null for the Cursor.
            mAdapter = new CustomCursorAdapter(this);
            mRecyclerView.setAdapter(mAdapter);

            //Kick off the loader
            getLoaderManager().initLoader(ASTRONOMY_LOADER, null, this);




        }



    @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.menu_display, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // User clicked on a menu option in the app bar overflow menu
        switch (item.getItemId()) {
            // Respond to a click on the "Delete all entries" menu option
            case R.id.action_delete_all_entries:
                deleteAllCalculations();
                return true;

        }

        return super.onOptionsItemSelected(item);
    }

     @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if (id == R.id.astronomyCalculations) {
            if(fragment!=null) {
                fragmentTransaction.remove(getSupportFragmentManager().findFragmentById(R.id.frame_layout));
                fragmentTransaction.commit();
                fab.show();
                if (mAdapter.getItemCount() == 0) {
                    mRecyclerView.setVisibility(View.GONE);
                    emptyView.setVisibility(View.VISIBLE);
                } else {
                    mRecyclerView.setVisibility(View.VISIBLE);
                    emptyView.setVisibility(View.GONE);

                }
            }

        } else if (id == R.id.definition) {
            // Handle the definition fragment
            fragment = new DefinitionFragment();
            if (fragment != null) {
                fragmentTransaction.replace(R.id.frame_layout, fragment);
                fragmentTransaction.commit();
                fab.hide();
                mRecyclerView.setVisibility(View.GONE);
                emptyView.setVisibility(View.GONE);

            }
}

1 个答案:

答案 0 :(得分:0)

我能够解决此问题。我要做的就是进入androidmanifest并添加:

android:configChanges="orientation|screenSize|keyboardHidden 

在活动部分。它阻止活动重新启动,这正是我想要的。