【问题标题】:Android - Fragments OverlapAndroid - 片段重叠
【发布时间】:2016-03-13 22:19:00
【问题描述】:

所以我遇到了一个问题,当我单击浮动操作按钮时,会弹出下一个片段,但另一个片段会保留。我想要发生的是当我单击按钮时,它会随着片段一起消失,然后弹出下一个片段。

这是带有按钮的片段类

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

这里是我要弹出的片段类

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

【问题讨论】:

    标签: java android xml android-fragments overlap


    【解决方案1】:

    我认为这是因为您将当前片段中的视图 id 传递给了 replace 方法。视图层次结构中的该节点当前不包含片段,因此只会添加它。您必须传递包含 ScoutFragment 的视图的 ID。 (如果您使用 xml 对片段进行膨胀,则 id 应该是 &lt;fragment&gt; 标记上的那个。如果您从代码中膨胀它,则传递与您在活动中传递的相同的 id)

    【讨论】:

    • 我只是在 java 类中设置了它。它可以工作,但是第一个片段没有退出,它停留在那里然后被第二个片段覆盖。无论如何我可以在下一个片段进入时看到它退出吗?
    • 啊,我明白了,我认为是因为您将当前片段中的视图 id 传递给了 replace 方法。视图层次结构中的该节点当前不包含片段,因此只会添加它。您必须传递包含 ScoutFragment 的视图的 ID。 (如果您使用 xml 对片段进行膨胀,则 id 应该是 &lt;fragment&gt; 标记上的那个。如果您从代码中膨胀它,则传递与您在活动中传递的相同的 id)
    • 我仍然对在这里做什么感到困惑,你能给我一个编码示例吗?
    • 如何启动 ScoutFragment?
    • 在这种情况下,您需要将 R.id.fragment_container 而不是 R.id.layoutScout 传递给 ScoutFragment 中的 replace 方法。请注意,支持 Fragment 中的 getFragmentManager() 方法返回管理该片段的 FragmentManager,因此它是您活动中的 SupportFragmentManager
    猜你喜欢
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2012-03-11
    相关资源
    最近更新 更多