Detailed explanation
Detailed explanation
Fragment is an important component in Android development that can help developers modify the interface and make applications more reusable and flexible.The Android SUPPORT Library V4 provides support for Fragment, which can also comply with the low version of the Android system.
1. Import SUPPORT LIBRARY
Add the following dependencies in the project's build.gradle file:
dependencies {
implementation 'com.android.support:support-v4:28.0.0'
}
Then click on Android Studio's "Sync Now" button to synchronize.
2. Create a Fragment class
Create a new Java class inherited from Android.support.v4.app.fragment and achieve the necessary methods.For example, create a Fragment class called "ExampleFragment":
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class ExampleFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_example, container, false);
// Initialize interface layout
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Execute related operations, such as setting a monitor
}
}
3. Use Fragment in Activity
In the Activity of Fragment, managing Fragment's addition, replacement, and removal operation through FragmentManager is used to manage Fragment.Add a Framelayout as a placeholder to display the content of Fragment in the Activity layout file.For example, create an Activity class called "ExampleActivity"::
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
public class ExampleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
// Create a Fragment instance
Fragment fragment = new ExampleFragment();
// Get FragmentManager example
FragmentManager fragmentManager = getSupportFragmentManager();
// Open a transaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// Add fragments to the place occupies
fragmentTransaction.add(R.id.fragment_container, fragment);
// Submit a transaction
fragmentTransaction.commit();
}
}
4. Add a place in the layout document
Add a Framelayout as a placeholder in the layout file corresponding to the Activity to display the content of the Fragment.For example, create a layout file called "Activity_example.xml"::
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Through the above steps, you can successfully use Fragment in Android Support Library V4.Developers can add multiple Fragment to achieve different interface modules according to actual needs.