Using fragment in Android is displayed as two screens

Source: Internet
Author: User
I mainly learned a small example of Google official (http://developer.android.com/training/basics/fragments/index.html), how to display on the tablet as two screens, this is more suitable for news-like applications, first look at the next ~


In the last two articles, the viewpager adapter is used. FragmentPagerAdapter, FragmentStatePagerAdapterTo use fragment, we can also directly use fragment, Android
SDK V4 + support provides fragmentactivity for us to manage fragment. When using fragment, we need to understand the layout file of fragment (whether it is static layout file or dynamic creation) will be added to the view container containing it
Do you still remember how to create a returned view when fragment is dynamically created in the previous article? The layoutinflater's inflate () method implements this ~
        @Override        public View onCreateView(LayoutInflater inflater, ViewGroup container,                    Bundle savedInstanceState) {             Log. i( "INFO", "onCreateView : " + (currentPageNum + 1));                          ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.per_pager1 ,                           container, false );              switch (currentPageNum ) {              case 0:                    rootView.setBackgroundResource(R.drawable. page1_bg );                     break ;              case 1:                    rootView.setBackgroundResource(R.drawable. page2_bg );                     break ;              case 2:                    rootView.setBackgroundResource(R.drawable. page3_bg );                     break ;              default :                     break ;             }              return rootView;       }

The configuration file is used to create fragment in this article, which may be more convenient and intuitive. In this example provided by Google, listfragment is used. You can regard it as a list fragment, it has a built-in listview and effectively manages it, which is very convenient and practical. It inherits from the fragment configuration in the configuration file, note that you must specify the full name of the fragment class. Android builds the fragment instance based on this parameter during runtime.

< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"    android:orientation= "horizontal"    android:layout_width= "match_parent"    android:layout_height= "match_parent" >    <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>

In this official Google example, the tablet and the mobile phone on the normal screen are also adapted. The normal mobile phone only displays one screen. The fragment in the Article Title List is used first. When you click an article, the fragment of the Article Title List will be replaced by the fragment of the article details. Here, you must note that the transaction corresponding to the fragment of the article details must be added to the rollback stack of the background, so that the transaction can be rolled back and returned to the fragment of the Article Title List

             // 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();

When adding a fragment to the fragmentactivity, You need to specify a view container, and the fragment transaction needs to be committed.
             // 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();

Another point is that, in this example, a callback interface onheadlineselectedlistener is defined to match the tablet and the mobile phone,
In the callback method, you can determine whether the article details fragment exists to determine whether to replace the currently displayed fragment (one screen) or update the fragment (two screens) when the article is clicked)

Public void onarticleselected (INT position) {// The user selected the headline of an article from the headlinesfragment // capture the Article fragment from the activity layout // find the article details fragment articlefragment articlefrag = (articlefragment) Detail (). findfragmentbyid (R. id. article_fragment); If (articlefrag! = NULL) {// if it exists, update the details fragment // 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 it does not exist, replace it with the article details fragment // If the frag is not available, we're 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. commit ();}}

I think in this example, we need to pay attention to these points. If there are more places to pay attention to, please let me know ~








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.