In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The knowledge of this article "how to use ViewPager2 to achieve page sliding switching effect in Android" is not quite understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use ViewPager2 in Android to achieve page sliding switching effect" article.
1. Introduction
In many applications, we often see the scene of sliding and switching between multiple pages. ViewPager2 is an upgraded version of ViewPager.
two。 Implement page sliding switch 2.1 and introduce ViewPager2 library
To use ViewPager2, you need to introduce the ViewPager2 library as follows:
Implementation "androidx.viewpager2:viewpager2:1.0.0" 2.2 using ViewPager2
Use ViewPager2 in a layout, as shown in the following example:
2.3 build Fragment
This Fragment is for simple demonstration use only, and its layout is as follows:
The implementation of the ContentFragment class is as follows:
Public class ContentFragment extends Fragment {private String content; public ContentFragment (String content) {this.content = content;} private TextView tv_content; @ Nullable @ Override public View onCreateView (@ NonNull LayoutInflater inflater, @ Nullable ViewGroup container, @ Nullable Bundle savedInstanceState) {View view = inflater.inflate (R.layout.fragment_content, container, false); tv_content = view.findViewById (R.id.tv_content) Tv_content.setText (content); return view;} public void setContent (String content) {this.content = content; tv_content.setText (content);}} 2.4 inherit FragmentStateAdapter
Create a custom class ContentPagerAdapter that inherits FragmentStateAdapter and implement the createFragment (int position) and getItemCount () methods, as shown in the following example:
Public class ContentPagerAdapter extends FragmentStateAdapter {private List datas; public ContentPagerAdapter (@ NonNull FragmentActivity fragmentActivity,List datas) {super (fragmentActivity); this.datas = datas;} @ NonNull @ Override public Fragment createFragment (int position) {return datas.get (position);} @ Override public int getItemCount () {return datas.size ();}} 2.5 bind ViewPager2 to adapter
After binding ViewPager2 to the adapter, you can switch pages by sliding. Examples are as follows:
Datas = new ArrayList (); datas.add (new ContentFragment ("Page 1")); datas.add (new ContentFragment ("Page 2")); datas.add (new ContentFragment ("Page 3")); datas.add (new ContentFragment ("Page 4")); datas.add (new ContentFragment ("Page 5")); contentPagerAdapter = new ContentPagerAdapter (this, datas); viewPager2.setAdapter (contentPagerAdapter); 2.6Vertical sliding switch
ViewPager2 supports not only horizontal sliding, but also vertical sliding. It is quite easy to achieve vertical sliding. Just add the android:orientation= "vertical" attribute to the layout file, as shown below:
Or calling the setOrientation (ViewPager2.ORIENTATION_VERTICAL) method of ViewPager2 in the code can also allow ViewPager2 to slide vertically.
2.7 Fragment updates
When the Fragment collection changes and needs to be updated, it is also convenient to use FragmentStateAdapter to update. Because ViewPager2 is based on RecyclerView, you can call notifyItemChanged (int position), notifyItemInserted (int position) and other methods to update the data.
The above is about the content of this article on "how to use ViewPager2 to achieve page sliding switching effect in Android". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.