10Building 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.




Add a Fragment to an Activity at Runtime


Rather than defining the fragments for an activity in the layout file—as shown in the previous lesson with the<fragment> element—you can add a fragment to the activity during the activity runtime. This is necessary if you plan to change fragments during the life of the activity.

To perform a transaction such as add or remove a fragment, you must use the FragmentManager to create aFragmentTransaction, which provides APIs to add, remove, replace, and perform other fragment transactions.

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 onCreate() method.

An important rule when dealing with fragments—especially when adding fragments at runtime—is that your activity layout must include a container View in which you can insert the fragment.

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 FrameLayout that acts as the fragment container.

Notice that the filename is the same as the layout file in the previous lesson, but the layout directory does nothave the large qualifier, so this layout is used when the device screen is smaller than large because the screen does not fit both fragments at the same time.

res/layout/news_articles.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" />

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 a new Fragment to be placed in the activity layout
            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();
        }
    }
}

Replace One Fragment with Another


The procedure to replace a fragment is similar to adding one, but requires the replace() method instead ofadd().

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 addToBackStack() before you commit theFragmentTransaction.

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  addToBackStack()  method takes an optional string parameter that specifies a unique name for the transaction. The name isn't needed unless you plan to perform advanced fragment operations using the FragmentManager.BackStackEntry  APIs.




Great! Building a logistics management system with Spring Boot is a good choice. Spring Boot is a popular Java framework that can help you quickly build web applications. Here are some steps you can follow: 1. Define the requirements: Before starting the development process, you should define the requirements for the logistics management system. This will help you understand what features are needed and what data needs to be stored. 2. Choose a database: You need to choose a database to store the data for your logistics management system. You can use a SQL database like MySQL or PostgreSQL, or a NoSQL database like MongoDB. 3. Set up the development environment: You need to set up the development environment to start building the logistics management system. You can use an IDE like Eclipse or IntelliJ IDEA, and install the necessary dependencies like Spring Boot and the database driver. 4. Design the database schema: You need to design the database schema to define the tables and relationships between them. This will help you to store and retrieve data efficiently. 5. Create the entities and repositories: You need to create the Java entities that represent the database tables, and the repositories that handle the database operations like insert, update, delete, and select. 6. Implement the business logic: You need to implement the business logic for the logistics management system, like creating orders, managing inventory, and tracking shipments. 7. Implement the REST endpoints: You need to implement the REST endpoints that allow clients to interact with the logistics management system. This includes creating endpoints for creating orders, managing inventory, and tracking shipments. 8. Test the system: You need to test the logistics management system to ensure that it works as expected. You can use tools like JUnit and Mockito to write unit tests for the individual components, and use tools like Postman or Swagger to test the REST endpoints. 9. Deploy the system: You need to deploy the logistics management system to a production environment. You can use tools like Docker and Kubernetes to containerize and orchestrate the application. By following these steps, you can build a robust logistics management system with Spring Boot. Good luck with your project!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值