在方向变化时刷新的片段的活动

时间:2018-02-18 09:54:43

标签: android android-layout android-fragments screen-orientation drawerlayout

在任何人说这看起来像一个重复的帖子之前..让我解释一下,我已经搜索了很多找到我的问题的解决方案但是没有找到任何可以解决我的问题的解决方案..

我正在使用抽屉布局,抽屉中的片段和菜单会更改活动中菜单选择的片段。每个片段都有2个布局,一个用于layout文件夹中的纵向,另一个用于layout-land文件夹中。 。 当活动首次打开时,默认情况下会在onCreate()中加载第一个片段 菜单选择片段改变但是当原点改变时,在布局 - 土地文件夹中再次显示第一个片段,并表示活动被刷新...

如果我将android:configChanges="orientation与活动一起使用..如果我使用android:configChanges="orientation|screenSize"问题仍然存在,则问题已解决,但layout-land中的横向布局未显示,而是显示layout中的纵向布局1}}文件夹

主要活动

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Drawer"
android:background="@mipmap/home_background"

tools:context="com.example.minhasoftphp.radius.Home">



    <FrameLayout
        android:id="@+id/fragmentHolder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>



 <LinearLayout
  android:layout_width="match_parent"
  android:layout_gravity="start"
  android:layout_marginEnd="-65dp"
  android:layout_marginRight="-65dp"
  android:layout_height="match_parent">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

 <include layout="@layout/drawer_menu"/>

 </ScrollView>

  </LinearLayout>

  </android.support.v4.widget.DrawerLayout>

home.class

public class Home extends AppCompatActivity {

private boolean Toggletitle ;
private DrawerLayout mDrawerlayout ;
private ActionBarDrawerToggle mToggle;

private String Current_Fragment = "home" ;


@Override
protected void onResume() {
    super.onResume();
    loadFragment(new home());
    getSupportActionBar().setTitle(R.string.drawerclosed);
}


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




    mDrawerlayout = (DrawerLayout) findViewById(R.id.Drawer) ;


     mToggle = new ActionBarDrawerToggle
    (this,mDrawerlayout,R.string.draweropen,R.string.drawerclosed) ; 

    mDrawerlayout.addDrawerListener(mToggle);
    mToggle.syncState();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


  //  addmenufragments(Current_Fragment);
  //  loadFragment(new home());
}




@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(mToggle.onOptionsItemSelected(item))
    {
        if(!Toggletitle || !mDrawerlayout.isDrawerOpen(GravityCompat.START))
        {
            Toggletitle = true ;
            getSupportActionBar().setTitle(R.string.draweropen);
        }

        else if(Toggletitle || mDrawerlayout.isDrawerOpen(GravityCompat.START))
        {
            Toggletitle = false ;
            getSupportActionBar().setTitle(R.string.drawerclosed);
        }

        return true ;

    }

    return super.onOptionsItemSelected(item);
}





public void menu_click(View v)
{

    switch (v.getId())
    {
        case R.id.home :

            mDrawerlayout.closeDrawer(Gravity.LEFT);
            getSupportActionBar().setTitle("Home");
            Current_Fragment = "Home";
            loadFragment(new home());
            break;

        case R.id.profile :

            mDrawerlayout.closeDrawer(Gravity.LEFT);
            getSupportActionBar().setTitle("Profile");
            Current_Fragment = "Profile";
            loadFragment(new profile());
            break;

        case R.id.sell :

            mDrawerlayout.closeDrawer(Gravity.LEFT);
            getSupportActionBar().setTitle("Sell property");
            Current_Fragment = "Sell";
            break;

        case R.id.trans :

            mDrawerlayout.closeDrawer(Gravity.LEFT);
            getSupportActionBar().setTitle("Transactions");
            Current_Fragment = "Transactions";
            break;

        case R.id.events :

            mDrawerlayout.closeDrawer(Gravity.LEFT);
            getSupportActionBar().setTitle("Events");
            Current_Fragment = "Events";
            break;

        case R.id.share :
            mDrawerlayout.closeDrawer(Gravity.LEFT);
            getSupportActionBar().setTitle("Events");
            Current_Fragment = "Events";
            break;
    }

}

public void Onclick(View view)
{
    Intent theIntent = new Intent(this, Catagory.class);


    switch (view.getId())
    {
        case R.id.bharia :

            theIntent.putExtra("category", "Baharia");
            startActivity(theIntent);

            break;


        case R.id.DHAcity :

            theIntent.putExtra("category", "DHA city");
            startActivity(theIntent);

            break;

        case R.id.DHAlhr :
            theIntent.putExtra("category", "DHA Lahore");
            startActivity(theIntent);

            break;

    }

}


private void loadFragment(Fragment fragment) {
// create a FragmentManager
    FragmentManager fm = getFragmentManager();
// create a FragmentTransaction to begin the transaction and replace the Fragment
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
// replace the FrameLayout with new Fragment
    fragmentTransaction.replace(R.id.fragmentHolder, fragment);
    fragmentTransaction.commit(); // save the changes
}


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putString("fragment", Current_Fragment);
    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);


}

 @Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);
    Current_Fragment = savedInstanceState.getString("fragment");

 }



}

1 个答案:

答案 0 :(得分:0)

我认为你应该在Layout Activity类中使用onConfigurationChanged并为Orientations写一个if / else条件。

@override  
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);  
}