首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android片段基础教程

Android片段基础教程
EN

Stack Overflow用户
提问于 2013-03-20 19:51:30
回答 2查看 25.2K关注 0票数 5

因此,我在Android开发者关于片段的网站上的第三个教程已经被困住了几天了。我只是不明白当我在平板电脑上运行应用程序(大屏幕布局)时,应用程序是如何填充数据的。我可以理解数据是如何在一个较小的屏幕(电话屏幕)上填充的。

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

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

MainActivity类

代码语言:javascript
运行
复制
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();
        }
    }
}

HeadLineFragment

代码语言:javascript
运行
复制
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版图

代码语言:javascript
运行
复制
<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版图

代码语言:javascript
运行
复制
<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>
EN

回答 2

Stack Overflow用户

发布于 2013-03-20 19:59:53

注意这两个布局的位置。

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

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

代码语言:javascript
运行
复制
 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处理为设备提供的最佳布局。

票数 5
EN

Stack Overflow用户

发布于 2013-08-01 12:45:34

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

注意:限定符意味着在屏幕大的设备上选择布局(例如,7“平板及以上)。其他布局(没有限定符)将被选择为较小的设备;

另外,如果您想要为小屏幕使用双窗格布局,请创建目录:res/lay-sw600dp/main.xml;

关于数据填充:在MainActivity中,当您单击列表中的项(HeadLinesFragment类)时,您已经实现了接口OnHeadlineSelectedListener。

接口mCallback从MainActivity onArticleSelected调用函数(位置);

在这个函数中有articleFlag

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

代码语言:javascript
运行
复制
    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();

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15533343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档