2013-03-20 87 views
5

所以我一直卡在Android Developer Site about Fragments几天的第三个教程。当我在平板电脑上运行应用(大屏幕布局)时,我无法理解该应用如何填充数据。我可以理解数据在小屏幕(电话屏幕)上的填充方式。Android片段基础教程

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

这里是从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(); 
     } 
    } 
} 

HeadLineFragment

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" /> 

布局bigscreen 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> 
+0

你会发现这个链接有用http://www.vogella.com/articles/AndroidFragments/article.html – 2013-11-23 21:08:09

+0

什么是HeadLinesFragment您正在使用? – Arqam 2016-02-19 19:21:34

回答

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箱设备提供最佳的布局。

+0

谢谢你的回答。我的问题是,我不明白大屏幕如何填充数据。我无法跟踪MainActivity中引用的引发setListAdapter(new ArrayAdapter (getActivity(),layout,Ipsum.Headlines)); 我是我缺少的东西? – 2013-03-20 21:27:28

+0

'ArticleFragment'布局只是从'ArticalFragment'行64中设置的'TextView'。所有第二个片段都会显示相应的消息。这两个数组的长度都是相同的,所以它只在第二个数组中显示相应位置的消息 – JBirdVegas 2013-03-20 21:45:46

+0

HeadLinesFragment怎么样,它从2窗格布局中获取数据的位置(更大的布局?我指的是代码填充它的位置? – 2013-03-20 22:32:10

0

我认为,这个问题与该职位相关:http://developer.android.com/training/multiscreen/screensizes.html

注:预选赛意味着布局上的设备能够划分为大(例如,7" 平板电脑及以上)的屏幕进行选择。其他布局(无限定符)将被选定为较小的设备;

另外,如果你想使用小屏幕双窗格布局,创建目录: RES /布局sw600dp/main.xml中;

关于数据人口: 在MainActivity中,您已经实现了接口OnHeadlineSelectedListener,当您单击列表中的项目时(HeadLinesFragment类)
从MainActivity接口mCallback调用函数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(); 

}