Android: How can I navigate from one “detail” to another “detail”, while having the “Up” button go back to the “Master” list?

≡放荡痞女 提交于 2019-12-04 07:11:29

Here's an alternative to using Fragments and ViewPager (as they could usually end in more boilerplate codes).

Lay all of your detail's view in one layout.xml and toggle their visibility via View.setVisibility() upon a click of a button. You can label this button as "Continue", "Next", or whatever. The main idea is to make the user aware that they're on the same context despite the change of screen.

Here's an illustration on how you should arrange your detail's layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/layout_page_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">

        <!-- Put your detail 1 content here -->

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_page_2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">

        <!-- Put your detail 2 content here -->

    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Continue"
        android:id="@+id/bt_continue"/>

</RelativeLayout>

As I said, the idea is to toggle the visibility of layout_page_1 and layout_page_2 upon bt_continue got clicked:

@Override
public void onClick(View v) {
    if(v.getId() == R.id.bt_continue) {
        layoutPage1.setVisibility(View.INVISIBLE);
        layoutPage2.setVisibility(View.GONE);
    }
}

With this logic, you could also implement a "Previous" button to navigate back to the first page of your detail.

If you want your user to able to set a default view then that's easy too. Just save an int in your SharedPreferences which will represent the page that will be "opened" - that is the view that will be set to View.VISIBLE - the first time your user opens the detail view.

I would solve that like this:

  • Master (fragment inside MainActivity)
  • Detail 1 (fragment inside FirstDetailActivity)
  • Detail 2 (fragment inside SecondDetailActivity)

Manifest file (for example):

<application>

    <!-- The main/home activity (it has no parent activity) -->
    <activity
        android:name="...MainActivity" ...>
        ...
    </activity>


   <!-- First detail activity -->
   <activity
       android:name="...FirstDetailActivity"
       android:parentActivityName="...MainActivity" >
       <!-- Parent activity meta-data to support 4.0 and lower -->
       <meta-data
           android:name="android.support.PARENT_ACTIVITY"
           android:value="...MainActivity" />
   </activity>

   <!-- Second detail activity -->
   <activity
        android:name="...SecondDetailActivity"
        android:parentActivityName="...MainActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
           android:name="android.support.PARENT_ACTIVITY"
           android:value="...MainActivity" />
   </activity>

</application>

So basically:

  1. Click on list (Master) opens FirstDetailActivity
  2. Click on FirstDetailActivity (Tab, Spinner, Button etc.) opens SecondDetailActivity
  3. Up button in either of FirstDetailActivity or SecondDetailActivity provides navigation to MainActivity

Reference: http://developer.android.com/training/implementing-navigation/ancestral.html

Implement your detail pages as Fragments and use a ViewPager to hold them both. This way, the user can, by swiping left or right, switch between the both of them. If you look at the Gmail app for instance, there you also can swipe through your mail-details once you've opened one from the mail list.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!