Android - 片段重叠
Android - Fragments Overlap
所以我遇到了一个问题,当我单击我的浮动操作按钮时,下一个片段会弹出,但另一个片段会保留下来。我想要发生的是,当我单击按钮时,它会随着打开的片段一起消失,然后弹出下一个片段。
这是带有按钮
的片段class
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class ScoutFragment extends Fragment {
FloatingActionButton addDataScout;
public ScoutFragment() {
// Required empty public constructor
} //End of ScoutFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_scout, container, false);
view.setBackgroundColor(Color.WHITE);
//Setups Floating Action Button
FloatingActionButton addDataScout = (FloatingActionButton) view.findViewById(R.id.fab);
addDataScout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AddScoutDataFragment fragment = new AddScoutDataFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
fragmentTransaction.replace(R.id.layoutScout, fragment);
fragmentTransaction.commit();
} //End of onClick
}); //End of setOnClickListener
return view;
} //End of onCreateView
} //End of class
这里是我要弹出的片段class
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class AddScoutDataFragment extends Fragment {
public AddScoutDataFragment() {
// Required empty public constructor
} //End of AddScoutDataFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_add_scout_data, container, false);
} //End of onCreateView
} //End of class
这里是按钮片段的xml文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layoutScout"
tools:context="com.compscitutorials.basigarcia.ramfernoscout.ScoutFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="61dp"
android:layout_height="57dp"
android:src="@mipmap/ic_action_add"
app:backgroundTint="#F28016"
app:elevation="5dp"
app:borderWidth="0dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="12dp"
android:layout_marginRight="10dp"
android:scaleType="fitCenter"
android:clickable="true">
</android.support.design.widget.FloatingActionButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Western University"
android:id="@+id/testText"
android:layout_centerInParent="true"
android:textSize="24dp"
android:textStyle="bold" />
</RelativeLayout>
</FrameLayout>
这是我要弹出的片段的 xml 文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_add_scout_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.compscitutorials.basigarcia.ramfernoscout.AddScoutDataFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accept Me"
android:id="@+id/testViewData"
android:layout_gravity="center"
android:textSize="24dp"
android:textStyle="bold" />
</FrameLayout>
通过扩展 AppCompatActivity 并实现 NavigationView 的 MainAcitivty 启动我的片段
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_welcome) {
//Set the fragment initially
WelcomeFragment fragment = new WelcomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
// Handle the camera action
}
else if (id == R.id.nav_facebook) {
FacebookFragment fragment = new FacebookFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
else if (id == R.id.nav_members) {
//Set the fragment initially
MembersFragment fragment = new MembersFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
else if (id == R.id.nav_robot) {
}
else if (id == R.id.nav_scout) {
//Set the fragment initially
ScoutFragment fragment = new ScoutFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
else if (id == R.id.nav_match) {
//Set the fragment initially
MatchFragment fragment = new MatchFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} //End of if statement
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
} //End of onNavigationItemSelected
我认为这是因为您将当前片段中的视图 ID 传递给了 replace 方法。视图层次结构中的该节点当前不包含片段,因此将只添加它。您必须传递包含 ScoutFragment 的视图的 ID。 (如果您使用 xml 扩充片段,则 id 应该是 <fragment>
标签上的那个。如果您通过代码扩充它,请传递与您在 activity 中传递的相同的 id)
所以我遇到了一个问题,当我单击我的浮动操作按钮时,下一个片段会弹出,但另一个片段会保留下来。我想要发生的是,当我单击按钮时,它会随着打开的片段一起消失,然后弹出下一个片段。
这是带有按钮
的片段classimport android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class ScoutFragment extends Fragment {
FloatingActionButton addDataScout;
public ScoutFragment() {
// Required empty public constructor
} //End of ScoutFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_scout, container, false);
view.setBackgroundColor(Color.WHITE);
//Setups Floating Action Button
FloatingActionButton addDataScout = (FloatingActionButton) view.findViewById(R.id.fab);
addDataScout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AddScoutDataFragment fragment = new AddScoutDataFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
fragmentTransaction.replace(R.id.layoutScout, fragment);
fragmentTransaction.commit();
} //End of onClick
}); //End of setOnClickListener
return view;
} //End of onCreateView
} //End of class
这里是我要弹出的片段class
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class AddScoutDataFragment extends Fragment {
public AddScoutDataFragment() {
// Required empty public constructor
} //End of AddScoutDataFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_add_scout_data, container, false);
} //End of onCreateView
} //End of class
这里是按钮片段的xml文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layoutScout"
tools:context="com.compscitutorials.basigarcia.ramfernoscout.ScoutFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="61dp"
android:layout_height="57dp"
android:src="@mipmap/ic_action_add"
app:backgroundTint="#F28016"
app:elevation="5dp"
app:borderWidth="0dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="12dp"
android:layout_marginRight="10dp"
android:scaleType="fitCenter"
android:clickable="true">
</android.support.design.widget.FloatingActionButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Western University"
android:id="@+id/testText"
android:layout_centerInParent="true"
android:textSize="24dp"
android:textStyle="bold" />
</RelativeLayout>
</FrameLayout>
这是我要弹出的片段的 xml 文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_add_scout_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.compscitutorials.basigarcia.ramfernoscout.AddScoutDataFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accept Me"
android:id="@+id/testViewData"
android:layout_gravity="center"
android:textSize="24dp"
android:textStyle="bold" />
</FrameLayout>
通过扩展 AppCompatActivity 并实现 NavigationView 的 MainAcitivty 启动我的片段
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_welcome) {
//Set the fragment initially
WelcomeFragment fragment = new WelcomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
// Handle the camera action
}
else if (id == R.id.nav_facebook) {
FacebookFragment fragment = new FacebookFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
else if (id == R.id.nav_members) {
//Set the fragment initially
MembersFragment fragment = new MembersFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
else if (id == R.id.nav_robot) {
}
else if (id == R.id.nav_scout) {
//Set the fragment initially
ScoutFragment fragment = new ScoutFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
else if (id == R.id.nav_match) {
//Set the fragment initially
MatchFragment fragment = new MatchFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} //End of if statement
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
} //End of onNavigationItemSelected
我认为这是因为您将当前片段中的视图 ID 传递给了 replace 方法。视图层次结构中的该节点当前不包含片段,因此将只添加它。您必须传递包含 ScoutFragment 的视图的 ID。 (如果您使用 xml 扩充片段,则 id 应该是 <fragment>
标签上的那个。如果您通过代码扩充它,请传递与您在 activity 中传递的相同的 id)