如何在景观中并排显示2个片段

时间:2015-04-24 04:17:56

标签: java android android-layout android-fragmentactivity android-listfragment

我有一个活动1托管ListFragmentA 点击列表中的项目后,我启动了托管ListFragmentB的Activity2 无论是纵向还是横向,我都会一次看到1个列表 当我处于横向模式(或者我认为是平板电脑)时,我感兴趣的是看到两个片段 即显示屏左侧的ListFragmentA和右侧的ListFragmentB。因此,当用户按下ListFragmentA中的项目时,会显示ListFragmentB中的正确数据 这是怎么做到的?横向模式中的布局在我看来是单调乏味/错误的,并且不确定如何做到诚实 有没有一个很好的例子我可以研究一下?

1 个答案:

答案 0 :(得分:1)

纵向模式的fragment_layout.xml是:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/titles"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="edu.dartmouth.cs.FragmentLayout$TitlesFragment" />

</FrameLayout>

在横向模式下,单个活动(FragmentLayout)处理两个片段。我们还将考虑以编程方式插入片段。考虑景观res / layout-land / fragment_layout。

横向模式的fragment_layout.xml是:

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

    <fragment
        android:id="@+id/titles"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="edu.dartmouth.cs.FragmentLayout$TitlesFragment" />

    <FrameLayout
        android:id="@+id/details"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="?android:attr/detailsElementBackground" />

</LinearLayout>

如果您在TitlesFragment中注释掉下面显示的代码行:onActivityCreated()(当FragmentLayout onCreate()返回时调用),那么您将在上面的frame_layout中看到加载的表单上的空白。如果未添加该行,则在用户从列表中选择项目之前,不会将DetailsFragment加载到该行中 - 此时将创建DetailsFragment并将其放入FrameLayout。

 public void onActivityCreated(Bundle savedInstanceState) {
                                    super.onActivityCreated(savedInstanceState);

                                    **snippet**

                                    if (mDualPane) {
                                                // In dual-pane mode, the list view highlights the selected item.
                                                getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                                                // Make sure our UI is in the correct state.
                        showDetails(mCurCheckPosition);
                                    } else {

FragmentLayout(主要活动)在onCreate()期间以通常的方式应用布局:

public class FragmentLayout extends Activity {

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

                        // root view inflated
                        setContentView(R.layout.fragment_layout);
            }

当用户点击ListFragment中的一个项目时,然后调用onListItemClick()回调,然后调用showDetails(position)将启动

 @Override
        public void onListItemClick(ListView l, View v, int position, long id) {

            Toast.makeText(getActivity(),
                    "onListItemClick position is" + position, Toast.LENGTH_LONG)
                    .show();

            showDetails(position);
        }

TitlesFragment

片段使用辅助函数来显示所选项目的详细信息。

 public static class TitlesFragment extends ListFragment {
        boolean mDualPane;
        int mCurCheckPosition = 0;

        // onActivityCreated() is called when the activity's onCreate() method
        // has returned.

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

            // You can use getActivity(), which returns the activity associated
            // with a fragment.
            // The activity is a context (since Activity extends Context) .

            Toast.makeText(getActivity(), "TitlesFragment:onActivityCreated",
                    Toast.LENGTH_LONG).show();

            // Populate list with our static array of titles in list in the
            // Shakespeare class
            setListAdapter(new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_activated_1,
                    Shakespeare.TITLES));

            // Check to see if we have a frame in which to embed the details
            // fragment directly in the containing UI.
            // R.id.details relates to the res/layout-land/fragment_layout.xml
            // This is first created when the phone is switched to landscape
            // mode

            View detailsFrame = getActivity().findViewById(R.id.details);

            Toast.makeText(getActivity(), "detailsFrame " + detailsFrame,
                    Toast.LENGTH_LONG).show();

            // Check that a view exists and is visible
            // A view is visible (0) on the screen; the default value.
            // It can also be invisible and hidden, as if the view had not been
            // added.
            //
            mDualPane = detailsFrame != null
                    && detailsFrame.getVisibility() == View.VISIBLE;

            Toast.makeText(getActivity(), "mDualPane " + mDualPane,
                    Toast.LENGTH_LONG).show();

            if (savedInstanceState != null) {
                // Restore last state for checked position.
                mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
            }

            if (mDualPane) {
                // In dual-pane mode, the list view highlights the selected
                // item.
                getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                // Make sure our UI is in the correct state.
                showDetails(mCurCheckPosition);
            } else {
                // We also highlight in uni-pane just for fun
                getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                getListView().setItemChecked(mCurCheckPosition, true);
            }
        }

管理方向翻转之间的状态 该应用程序跟踪当前检查的选择,以便在它恢复时 - 再次在横向上再次回到它作为片段生命周期中使用onSaveInstanceState()突出显示的最后位置。该片段保存其当前的动态状态,因此稍后可以在重新启动其进程的新实例中重建它。如果稍后需要创建片段的新实例,那么您在Bundle中放置的数据将在提供给onCreate(Bundle),onCreateView(LayoutInflater,ViewGroup,Bundle)和onActivityCreated(Bundle)的Bundle中可用。在代码中,新片段恢复onActivityCreated()中的状态。这里的状态只是mCurCheckPosition。

 @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            Toast.makeText(getActivity(), "onSaveInstanceState",
                    Toast.LENGTH_LONG).show();

            outState.putInt("curChoice", mCurCheckPosition);
        }

FragmentManager和Fragment Transactions

辅助函数(showDetails(position)),用于显示所选项目的详细信息,方法是在当前UI中就地显示片段,或者启动显示它的全新活动。

 void showDetails(int index) {
            mCurCheckPosition = index;

            // The basic design is mutli-pane (landscape on the phone) allows us
            // to display both fragments (titles and details) with in the same
            // activity; that is FragmentLayout -- one activity with two
            // fragments.
            // Else, it's single-pane (portrait on the phone) and we fire
            // another activity to render the details fragment - two activities
            // each with its own fragment .
            //
            if (mDualPane) {
                // We can display everything in-place with fragments, so update
                // the list to highlight the selected item and show the data.
                // We keep highlighted the current selection
                getListView().setItemChecked(index, true);

                // Check what fragment is currently shown, replace if needed.
                DetailsFragment details = (DetailsFragment) getFragmentManager()
                        .findFragmentById(R.id.details);
                if (details == null || details.getShownIndex() != index) {
                    // Make new fragment to show this selection.

                    details = DetailsFragment.newInstance(index);

                    Toast.makeText(getActivity(),
                            "showDetails dual-pane: create and replace fragment",
                            Toast.LENGTH_LONG).show();

                    // Execute a transaction, replacing any existing fragment
                    // with this one inside the frame.
                    FragmentTransaction ft = getFragmentManager()
                            .beginTransaction();
                    ft.replace(R.id.details, details);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                    ft.commit();
                }

            } else {
                // Otherwise we need to launch a new activity to display
                // the dialog fragment with selected text.
                // That is: if this is a single-pane (e.g., portrait mode on a
                // phone) then fire DetailsActivity to display the details
                // fragment

                // Create an intent for starting the DetailsActivity
                Intent intent = new Intent();

                // explicitly set the activity context and class
                // associated with the intent (context, class)
                intent.setClass(getActivity(), DetailsActivity.class);

                // pass the current position
                intent.putExtra("index", index);

                startActivity(intent);
            }
        }

DetailsActivity:人像模式处理

如前所述如果用户单击列表项并且当前布局不包含R.id.details视图(DetailsFragment执行此操作),则应用程序将启动DetailsActivity活动以显示项的内容。辅助函数在横向中创建一个新片段,以纵向绘制细节,启动一个活动(DetailsActivity)来管理细节片段 - 即创建一个新的DetailsFragment并使用FragmentManager将其添加到根视图,如下所示。当屏幕处于纵向时,DetailsActivity会嵌入DetailsFragment以显示所选的播放摘要:

//这是一个辅助活动,用于显示用户在选择时所选择的内容 //屏幕不够大,无法在一个活动中显示所有内容。

公共静态类DetailsActivity扩展了Activity {

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

    Toast.makeText(this, "DetailsActivity", Toast.LENGTH_SHORT).show();

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // If the screen is now in landscape mode, we can show the
        // dialog in-line with the list so we don't need this activity.
        finish();
        return;
    }

    if (savedInstanceState == null) {
        // During initial setup, plug in the details fragment.

        // create fragment
        DetailsFragment details = new DetailsFragment();

        // get and set the position input by user (i.e., "index")
        // which is the construction arguments for this fragment
        details.setArguments(getIntent().getExtras());

        //
        getFragmentManager().beginTransaction()
                .add(android.R.id.content, details).commit();
    }
}

}

DetailsFragment

首先创建片段。片段生命周期确保onCreateView()构建片段的布局。它使用textview构建片段--text.setText(Shakespeare.DIALOGUE [getShownIndex()]) - 并将其附加到滚动条(ScrollView)并返回(并呈现)绘制的视图。

    // This is the secondary fragment, displaying the details of a particular
    // item.        

    public static class DetailsFragment extends Fragment {

                **snippet**

                public int getShownIndex() {
                            return getArguments().getInt("index", 0);
                }

                // The system calls this when it's time for the fragment to draw its
                // user interface for the first time. To draw a UI for your fragment,
                // you must return a View from this method that is the root of your
                // fragment's layout. You can return null if the fragment does not
                // provide a UI.

                // We create the UI with a scrollview and text and return a reference to
                // the scoller which is then drawn to the screen

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                        Bundle savedInstanceState) {

                    **snippet**

                        // programmatically create a scrollview and textview for the text in
                        // the container/fragment layout. Set up the properties and add the view

                        ScrollView scroller = new ScrollView(getActivity());
                        TextView text = new TextView(getActivity());
                        int padding = (int) TypedValue.applyDimension(
                                            TypedValue.COMPLEX_UNIT_DIP, 4, getActivity()
                                                          .getResources().getDisplayMetrics());
                        text.setPadding(padding, padding, padding, padding);
                        scroller.addView(text);
                        text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
                        return scroller;
                }
    }