Building a Flexible UI
When designing your application to support a wide range of screen sizes,you can reuse your fragments in different layout configurations to optimize the user experience based on the available screen space. For example,on a handset device it might be appropriate to display just one fragment at a time for a single-pane user interface. Conversely,you may want to set fragments side-by-side on a tablet which has a wider screen size to display more information to the user. Figure 1. Two fragments,displayed in different configurations for the same activity on different screen sizes. On a large screen,both fragment fit side by side,but on a handset device,only one fragment fits at a time so the fragments must replace each other as the user navigates. The Add a Fragment to an Activity at RuntimeRather than defining the fragments for an activity in the layout file—as shown in the previous lesson with the To perform a transaction such as add or remove a fragment,you must use the If your activity allows the fragments to be removed and replaced,you should add the initial fragment(s) to the activity during the activity's An important rule when dealing with fragments—especially those that you add at runtime—is that the fragment must have a container The following layout is an alternative to the layout shown in the previous lesson that shows only one fragment at a time. In order to replace one fragment with another,the activity's layout includes an empty Notice that the filename is the same as the layout file in the previous lesson,but the layout directory does not have the
<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" /> Inside your activity,call You can perform multiple fragment transaction for the activity using the same For example,here's how to add a fragment to the previous layout: import android.os.Bundle; import android.support.v4.app.FragmentActivity; public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); // Check that the activity is using the layout version with // the fragment_container FrameLayout 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(); } } } Because the fragment has been added to the Replace One Fragment with AnotherThe procedure to replace a fragment is similar to adding one,but requires the Keep in mind that when you perform fragment transactions,such as replace or remove one,it's often appropriate to allow the user to navigate backward and "undo" the change. To allow the user to navigate backward through the fragment transactions,you must call Note: When you remove or replace a fragment and add the transaction to the back stack,the fragment that is removed is stopped (not destroyed). If the user navigates back to restore the fragment,it restarts. If you do not add the transaction to the back stack,then the fragment is destroyed when removed or replaced. Example of replacing one fragment with another: // Create fragment and give it an argument specifying the article it should show 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(); The (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |