【问题标题】:Replace one fragment with another, go back to previous fragment on toolbar back button press用另一个片段替换一个片段,按下工具栏后退按钮返回上一个片段
【发布时间】:2017-08-03 23:34:25
【问题描述】:

我有两个片段。

第一个片段有一个Button 视图,点击后应该会打开第二个片段。工具栏的标题应该在两个片段之间改变(从“第一个片段”到“第二个片段”)。第二个片段应该在工具栏中有一个“后退按钮”(左箭头图标),点击后,用户可以返回到第一个片段。

我该如何实现?

此外,是否可以为两个片段之间的过渡设置动画,使其看起来好像第二个片段在新“窗口”中打开(something like this)?

这是我的活动课:

public class MainActivity extends AppCompatActivity {

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        if (findViewById(R.id.fragment_container) != null) {
            if (savedInstanceState != null) {
                return;
            }

            FirstFragment firstFragment = new FirstFragment();

            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();
        }
    }
}

这是活动布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

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

</LinearLayout>

这是我的第一个片段类:

public class FirstFragment extends Fragment implements View.OnClickListener {

    public FirstFragment() {
        // Required empty public constructor
    }

    public static FirstFragment newInstance() {
        FirstFragment fragment = new FirstFragment();

        Bundle args = new Bundle();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            //
        }
    }

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

        Button openSecondFragmentButton = (Button) view.findViewById(R.id.openSecondFragmentButton);
        openSecondFragmentButton.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        Fragment fragment = null;

        switch (v.getId()) {
            case R.id.openSecondFragmentButton:
                // ?
                break;
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

这是我的第一个片段的布局:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:isScrollContainer="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <Button
                android:id="@+id/openSecondFragmentButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Open Second Fragment" />

        </RelativeLayout>

    </RelativeLayout>

</ScrollView>

这是我的第二个片段类:

public class SecondFragment extends Fragment {

    public SecondFragment() {
        // Required empty public constructor
    }

    public static SecondFragment newInstance() {
        SecondFragment fragment = new SecondFragment();

        Bundle args = new Bundle();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            //
        }
    }

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

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

最后,这是我的第二个片段的布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="10" />

</LinearLayout>

我怎样才能达到我想要的结果?

【问题讨论】:

    标签: android android-layout android-studio android-fragments android-activity


    【解决方案1】:

    在事务上使用addToBackStack 以获得“返回”行为。

    在事务上使用setCustomAnimations 为您的片段过渡设置动画。

            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment)
                    .addToBackStack(null)
                    .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_out_right, R.anim.slide_in_left)
                    .commit();
    

    以编程方式按下后退按钮:

            getSupportFragmentManager().popBackStack();
    

    由于您显然在使用支持库的FragmentTransaction,因此动画应该是旧的R.anim 类型的动画。 (不支持FragmentTransaction使用较新的R.animator种类。)

    /res/anim/slide_in_left.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <translate
            android:duration="@android:integer/config_mediumAnimTime"
            android:interpolator="@android:anim/decelerate_interpolator"
            android:fromXDelta="-100%p"
            android:toXDelta="0" />
    
    </set>
    

    /res/anim/slide_in_right.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <translate
            android:duration="@android:integer/config_mediumAnimTime"
            android:interpolator="@android:anim/decelerate_interpolator"
            android:fromXDelta="100%p"
            android:toXDelta="0" />
    
    </set>
    

    /res/anim/slide_out_left.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <translate
            android:duration="@android:integer/config_mediumAnimTime"
            android:interpolator="@android:anim/decelerate_interpolator"
            android:fromXDelta="0"
            android:toXDelta="-100%p" />
    
    </set>
    

    /res/anim/slide_out_right.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <translate
            android:duration="@android:integer/config_mediumAnimTime"
            android:interpolator="@android:anim/decelerate_interpolator"
            android:fromXDelta="0"
            android:toXDelta="100%p" />
    
    </set>
    

    将此代码添加到活动中的onCreate

        mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    

    然后覆盖onOptionsItemSelected():

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            switch (item.getItemId()) {
            case android.R.id.home:
    
                getSupportFragmentManager().popBackStack();
                return true;
            }
    
            return false;
         }
    

    【讨论】:

    • getSupportFragmentManager().popBackStack(); 去哪儿了?
    猜你喜欢
    • 2015-08-26
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多