5

所以几天来,我一直被困在Android 开发者网站上关于 Fragments的第三个教程上。当我在平板电脑(大屏幕布局)上运行应用程序时,我只是无法理解应用程序如何填充数据。我可以理解如何在较小的屏幕(电话屏幕)上填充数据。

更大的屏幕列表如何填充数据?

这是来自Android.com教程的整个项目的链接。

MainActivity 类

public class MainActivity extends FragmentActivity 
    implements HeadlinesFragment.OnHeadlineSelectedListener {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Here, the system will decide which news_article layout it will use based on the screen size. Will use layout if small or layout-large if it's big. 
        setContentView(R.layout.news_articles);

        // Check whether the activity is using the layout version with
        // the fragment_container FrameLayout. If so, we must add the first fragment
        //This check is to determine which layout to be used, either small screen or big screen.
        //fragment_container used FrameLayout for small screens.
        //fragment_container is the id of FrameLayout in news_article for small screen. 
        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create an instance of ExampleFragment
            HeadlinesFragment firstFragment = new HeadlinesFragment();

            // In case this activity was started with special instructions from an Intent,
            // pass the Intent's extras to the fragment as arguments
             firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();
        }
    }

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment

        // Capture the article fragment from the activity layout
        ArticleFragment articleFrag = (ArticleFragment)
            getSupportFragmentManager().findFragmentById(R.id.article_fragment);

        if (articleFrag != null) {
            // If article frag is available, we're in two-pane layout...

            // Call a method in the ArticleFragment to update its content
            articleFrag.updateArticleView(position);

        } else {
            // If the frag is not available, we're in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            ArticleFragment newFragment = new ArticleFragment();
            Bundle args = new Bundle();
            args.putInt(ArticleFragment.ARG_POSITION, position);
            newFragment.setArguments(args);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }
    }
}

标题片段

public class HeadlinesFragment extends ListFragment {

// The container Activity must implement this interface so the frag can deliver messages
    public interface OnHeadlineSelectedListener {
        /** Called by HeadlinesFragment when a list item is selected */
        public void onArticleSelected(int position);
    }

    OnHeadlineSelectedListener mCallback;

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

        // We need to use a different list item layout for devices older than Honeycomb
        int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
            android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;

        // Create an array adapter for the list view, using the Ipsum headlines array
        setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));

    }

    @Override
    public void onStart() {
        super.onStart();

        // When in two-pane layout, set the listview to highlight the selected list item
        // (We do this during onStart because at the point the listview is available.)
        if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Notify the parent activity of selected item
        mCallback.onArticleSelected(position);

        // Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);
    }
}

小屏 news_article.xml的布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

大屏幕news_article.xml的布局

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

    <fragment android:name="com.example.android.fragments.HeadlinesFragment"
              android:id="@+id/headlines_fragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="com.example.android.fragments.ArticleFragment"
              android:id="@+id/article_fragment"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>
4

2 回答 2

5

注意两个布局的位置。

大屏幕位于平板电脑箱(文件夹)res/layout-large/main.xml中,而小屏幕布局位于通用中res/layout/main.xml

由于 java 询问 findViewById 是否为 null,我们知道设备是大屏幕还是普通布局。

 ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager().findFragmentById(R.id.article_fragment);
    if (articleFrag != null) { 
        /* not null because we are in res/layout-large */
    } else {
        /* we are in single pain view /res/layout/... */
    }

当您调用setContentView(int);它们时,系统会根据提供的 DPI bin 加载您为设备提供的最佳布局。

于 2013-03-20T19:59:53.757 回答
0

我认为,这个问题与这篇文章有关:http: //developer.android.com/training/multiscreen/screensizes.html

注意:large qualifier 表示将在屏幕分类为大的设备(例如,7" 平板电脑及以上)上选择布局。对于较小的设备,将选择其他布局(没有限定符);

此外,如果您想用于小屏幕两窗格布局,请创建目录:res/layout-sw600dp/main.xml;

关于数据填充:在MainActivity中你已经实现了接口OnHeadlineSelectedListener,当你点击列表中的项目时(HeadLinesFragment类)
接口mCallback从MainActivity调用函数onArticleSelected(position);

在这个函数中你有 articleFlag

ArticleFragment articleFlag = (ArticleFragment) getSupportFragmentManager()。findFragmentById(R.id.article_fragment);

    if(articleFlag != null){
        //we have 2 panes (big screen)
articleFlag.updateArticleView(position);

    }else{
        //small screen 
       ArticleFragment newFragment = new ArticleFragment();
        Bundle args = new Bundle();
        args.putInt(ArticleFragment.ARG_POSITION, position);
        newFragment.setArguments(args);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        //Commit the transaction 
        transaction.commit();

}

于 2013-08-01T12:45:34.653 回答