@falcon_sapsan
DevOps

Как избавиться от наложения фрагментов друг на друга?

есть
public class MainActivity extends AppCompatActivity {
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
}

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragment = new SettingsActivityFragment();
            ft.replace(R.id.main, fragment);
            ft.commit();
        }

        return super.onOptionsItemSelected(item);
    }
}


код activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:name=".MainActivityFragment"
    tools:layout="@layout/fragment_main_new"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


код fragment_main_new.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

      <FrameLayout
            android:id="@+id/main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2">
            <ListView
			    android:id="@+id/AC_ListView"
			    android:layout_width="match_parent"
			    android:layout_height="match_parent"
			    android:background="#FFF" >
			</ListView>
    </FrameLayout>

</FrameLayout>


Код fragment_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/GeneralSettingsText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:text="@string/general_settings_lbl"
                android:textStyle="bold" />

            <View
                android:id="@+id/AdvCount"
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_gravity="center_vertical"
                android:layout_marginTop="5dp"
                android:background="#fccccccc" />

            <CheckBox
                android:id="@+id/checkBox1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/show_alert_lbl" />

            <TextView
                android:id="@+id/ShowedText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:text="@string/showed_text_lbl"
                android:textStyle="bold" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textMultiLine"
                android:ems="10"
                android:id="@+id/editText"
                android:layout_gravity="center_horizontal"
                android:lines="3" />


            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="@string/alert_time_lbl"
                android:textStyle="bold" />

    </ScrollView>

</RelativeLayout>


Код SettingsFragment

public class SettingsActivityFragment extends Fragment {
    public static Fragment newInstance(Context context) {
        SettingsActivityFragment f = new SettingsActivityFragment();
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_settings, container, false);

        return rootView;
    }
}


Собственно при клике на меню настроек, должен поменяться фрагмент. но происходит наложение двух фрагментов. Как исправить?
  • Вопрос задан
  • 592 просмотра
Пригласить эксперта
Ответы на вопрос 1
enq3
@enq3
Android engineer at #ITX5
MainActivity.java
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment fragment = MainActivityFragment.newInstance(this);
        ft.replace(R.id.main, fragment);
        ft.commit();
    }

    @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_main, 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) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragment = SettingsActivityFragment.newInstance(this);
            ft.replace(R.id.main, fragment);
            ft.addToBackStack(null);
            ft.commit();
        }

        return super.onOptionsItemSelected(item);
    }
}


MainActivityFragment.java
public class MainActivityFragment extends Fragment {
    public static Fragment newInstance(Context context) {
        MainActivityFragment f = new MainActivityFragment();
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }
}


SettingsActivityFragment.java
public class SettingsActivityFragment extends Fragment {
    public static Fragment newInstance(Context context) {
        SettingsActivityFragment f = new SettingsActivityFragment();
        return f;
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_settings_activity, container, false);
    }
}


activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" />


fragment_main.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/AC_ListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"></ListView>


fragment_settings_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top">

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/GeneralSettingsText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:text="GeneralSettingsText"
                android:textStyle="bold" />

            <View
                android:id="@+id/AdvCount"
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_gravity="center_vertical"
                android:layout_marginTop="5dp"
                android:background="#fccccccc" />

            <CheckBox
                android:id="@+id/checkBox1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="checkBox1" />

            <TextView
                android:id="@+id/ShowedText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:text="ShowedText"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:ems="10"
                android:inputType="textMultiLine"
                android:lines="3" />


            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="textView2"
                android:textStyle="bold" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы