Lesson 03 fragment
Fragment
Official website documents: https://developer.android.google.cn/guide/components/fragments
On the mobile phone, activity acts as the interface for user and application interaction. If there are more and more complex views on the interface, the activity will be more complex. Fragment provides a solution to put the view into several fragments, and then assemble these fragments into a complete activity.
If there are the same parts in multiple interfaces, you can put the repeated parts in the fragment, and then use the fragment in multiple activities to achieve the purpose of unified style and code reuse.
Application scenario of fragment:
Note: the oncreateview method needs to be overridden; The fragment class in the support-v4 package is recommended.
public class LeftFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.left_fragment,container,false);
return view;
}
}
These two steps complete a fragment, which is very similar to activity. It also writes the corresponding layout, and then loads the layout in the life cycle method.
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android "
xmlns:tools=" http://schemas.android.com/tools ">
<fragment
Invalid Sign
android:name="com.ncst.coursecode.LeftFragment"
android:layout_ width="wrap_content"
android:layout_ height="match_parent"/>
</LinearLayout>
Note: the ID and name attributes must be explicitly specified. The name attribute is the class name of the fragment.
In addition, activities containing fragments need to inherit from fragmentactivity. The appcompatactivity we use by default inherits from fragmentactivity.
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android "
xmlns:tools=" http://schemas.android.com/tools ">
<FrameLayout
Invalid Sign
android:layout_ width=" match_parent "
android:layout_ height="match_parent"/>
</LinearLayout>
Fragment fragment=new RightFragment();
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id. fragment_container,fragment);
transaction.commit();
Two other methods: replace (r.id. fragment_container, fragment); Remove all existing fragments in the container and add this fragment. remove(R.id. fragment_container,fragment); Remove the fragment from the container.
Note: fragments added statically in XML cannot be removed by the above two methods.
Addtobackstack() adds the transaction to the return stack, which is managed by the activity, so that the user can undo the transaction and go back to the previous fragment by pressing the return button.
Get fragment instance in activity:
getFragmentManager().findFragmentById(R.id.right_fragment);
Get the active instance associated with the current fragment in the fragment:
getActivity();
Get another fragment in the activity associated with the current fragment in one fragment:
getActivity().getFragmentManager().findFragmentById(R.id.left_fragment);
Fragments must always be embedded in activities, and their lifecycles are directly affected by the host activity lifecycles. For example, when an activity is paused, all clips in it will also be paused; When the activity is destroyed, all fragments will also be destroyed.
Picture:
Analysis: the page is divided into two parts: the bottom part (radiogroup) and the top part (placing fragment)
Viewpager function: view switching can be completed by gesture sliding. It is generally used as the guide page of app or to realize picture rotation. Viewpager is in: android.support.v4.view.viewpager.
Viewpager is a simple page switching component. We can fill multiple views into it, and then we can slide left and right to switch different views. Like listview and recyclerview, we also need an adapter to fill our view into viewpager. Viewpager has a specific adapter - pageradapter! In addition, Google officially suggests that we use fragment to fill the viewpager, which can more easily generate each page and manage the life cycle of each page! Two fragment specific adapters are provided to us: fragmentpageadapter and fragmentstatepageradapter.
Instantiateitem (ViewGroup, int): adds a view to the parent layout
Destroy item (ViewGroup, int, object): removes a view at a given location.
Getcount(): get the number of views in the viewpager
Isviewfromobject (view, object): general implementation: return view = = object;
A typical pageradapter class:
public class MyPagerAdapter extends PagerAdapter {
List<View> viewList;
public MyPagerAdapter(List<View> viewList) {
this.viewList = viewList;
}
public int getCount() {
return viewList.size();
}
public boolean isViewFromObject(View view, Object o) {
return view==o;
}
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
}
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.get(position));
}
}
<android.support.v4.view.ViewPager
Invalid Sign
android:layout_ width="match_parent"
android:layout_ height="match_parent">
</android.support.v4.view.ViewPager>
In addition, pagertabstrip is used as the interactive pointer of viewpager. You just need to be a child of viewpager in the XML document. And in pageradapter, you need to override the getpagetitle () method to get the title of the specified page.
Pagertitlestrip is similar to pagertabstrip, except that it cannot interact.
<android.support.v4.view.ViewPager
Invalid Sign
android:layout_ width="match_parent"
android:layout_ height="match_parent">
<android.support.v4.view.PagerTabStrip
android:layout_ width="match_parent"
android:layout_ height="wrap_content"
android:layout_ gravity="top" />
</android.support.v4.view.ViewPager>
Official documents: https://developer.android.google.cn/reference/android/support/v4/view/PagerAdapter
Is the implementation of pageradapter, and each page is a fragment.
This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state. For larger sets of pages, consider @H_ 936_ 301@FragmentStatePagerAdapter.
Fragmentpageradapter is suitable for a small number of fixed number of pages. Memory will be consumed when there are many and non fixed pages. In this case, consider using the fragmentstatepageradapter.
When implementing fragmentpageradapter, you only need to rewrite: @ h_ 936_ 301@getItem (int) and @ h_ 936_ 301@getCount ()@H_ 936_ 301@。
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int i) {
return MyFragment.newInstance(i);
}
public int getCount() {
return NUM_ ITEMS;
}
}
Android provides android.support.design.widget.tablayout control to realize the left-right sliding function of the top title.
<android.support.design.widget.TabLayout
Invalid Sign
android:layout_ width="match_parent"
android:layout_ height="wrap_content" />
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
Official documents:
https://developer.android.google.cn/reference/android/support/v4/app/FragmentPagerAdapter.html