Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement an Infinite Carousel Graph with android ViewPager

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces android ViewPager how to achieve an infinite rotation map, the article introduces in great detail, has a certain reference value, interested friends must read it!

First we need to build a package, and then create a new java class with a random name

We just need to inherit this class from a viewGroup. ViewGroup is a view that can store child controls. Our various layout, such as LinearLayour or RelativeLayout, can put things in it, while TextView or ImageView, which can only put content but not other view, is ordinary view.

Then we select three constructors.

Package com.example.viewpager.views; import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.RelativeLayout; import androidx.annotation.NonNull; import com.example.viewpager.R; import java.util.AbstractSet; public class LooperPager extends RelativeLayout {public LooperPager (Context context) {super (context);} public LooperPager (Context context,@NonNull AbstractSet abstrs) {super (context, (AttributeSet) abstrs) } public LooperPager (Context context,@NonNull AbstractSet abstrs,int defStyleAttr) {super (context, (AttributeSet) abstrs,defStyleAttr);}}

Then we create a new layout file to write in the layout we want to implement.

Because we are implementing an infinite rotation graph for ViewPager, first, of course, we write a ViewPager, then a title at the top, and we write a textview, because we want to distinguish it from sadness, we set the background to red, the title to white, and then center the text, and finally, because we want the picture to slide, there is a row below that shows the logo that represents the picture according to the number of pictures. We set a linear layout centered at the bottom of the control, and then set three white view with 5dp before and after the interval of 5dp in the linear layout.

That's how it works.

The next step is to bind our custom layout to our custom class, because we want to call our binding method no matter which constructor we want to call, so we have to change the supper in other methods to this.

Package com.example.viewpager.views; import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.RelativeLayout; import androidx.annotation.NonNull; import com.example.viewpager.R; import java.util.AbstractSet; public class LooperPager extends RelativeLayout {public LooperPager (Context context) {this (context,null);} public LooperPager (Context context,@NonNull AbstractSet abstrs) {this (context, abstrs,0) } public LooperPager (Context context,@NonNull AbstractSet abstrs,int defStyleAttr) {super (context, (AttributeSet) abstrs,defStyleAttr); / / Custom layout binds the current class, this: current class, ture: make sure to bind LayoutInflater.from (context) .propagate (R.layout.looperpager.thisthjing.true);}}

The next step is to test whether our custom control is successful.

Recreate a startup file and then create another lauout file

Here we right-click the looppager selection

Then paste the set width and height in the newly created Layout file

Finally, bind the layout file you just wrote in our newly created activity

Package com.example.viewpager; import android.os.Bundle;import android.os.PersistableBundle; import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity; public class supper_MainActivity extends AppCompatActivity {@ Override public void onCreate (@ Nullable Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.supper_activity_main);}}

The effect will be realized.

The above is all the content of the article "how to achieve an infinite rotation map in android ViewPager". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report