Android碎片基础教程

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>

你可能会发现这个链接有帮助:http://www.vogella.com/articles/AndroidFragments/article.html - Nur Rony
你正在使用的HeadLinesFragment是什么? - arqam
2个回答

5

请注意两个布局的位置。

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

由于Java会检查findViewById是否为空,因此我们知道设备是大屏幕还是普通布局。

 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加载最适合该设备的布局。

谢谢您的回答。我的问题是我不明白如何将数据填充到更大的屏幕中。在MainActivity中,我无法跟踪引用,以便触发setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines)); 我是否遗漏了什么? - W J
ArticleFragment 布局只是 ArticalFragment 第 64 行设置的 TextView。第二个片段所做的一切就是显示相应的消息。两个数组长度相同,因此它只会在相应位置显示第二个数组中的消息。 - JBirdVegas
HeadLinesFragment怎么样?在2-pane布局(更大的布局)中,它从哪里获取数据?我的意思是什么代码填充它? - W J
HeadlinesFragment 第44行代码中,写着 setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines)); - JBirdVegas
MainActivity在用户选择列表项时收到一个回调,因为它实现了接口HeadlinesFragment.OnHeadlineSelectedListener,并使用方法public void onArticleSelected(int position)。在第55行中,它使用提供的位置来确定要使用哪个Ipsum文章位置,同时填充第二个视图。 - JBirdVegas
显示剩余3条评论

0

我认为这个问题与以下帖子有关: http://developer.android.com/training/multiscreen/screensizes.html

备注:large 限定符表示将针对屏幕分类为大型设备(例如7英寸平板电脑及以上)的设备选择布局。对于较小的设备,将选择其他布局(不带限定符);

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

关于数据填充: 在MainActivity中,您已经实现了OnHeadlineSelectedListener接口,当你从列表项中点击时(HeadLinesFragment class),
接口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();

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接