In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how Android ViewPager can automatically switch pictures every two seconds". In daily operation, I believe many people have doubts about how Android ViewPager can automatically switch pictures every two seconds. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "how Android ViewPager can automatically switch pictures every two seconds". Next, please follow the editor to study!
Layout code:
Normal shape graphics dot_normal.xml:
Shape graphic dot_focused.xml that gets the focus
Specific implementation code:
Import java.util.ArrayList;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView Public class MainActivity extends Activity {private int imageIds []; private String [] titles; private ArrayList images; private ArrayList dots; private TextView title; private ViewPager mViewPager; private ViewPagerAdapter adapter; private int oldPosition = 0 savedInstanceState / record the location of the last point private int currentItem; / / current page private ScheduledExecutorService scheduledExecutorService; @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main) / / Picture ID imageIds = new int [] {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e} / / the picture title titles = new String [] {"Gong Li is not vulgar, I can't be vulgar", "the tree is back!" Then sing classic old songs to attract thousands of people to sing a chorus, "reveal how Beijing movies are upgraded", "Leeco's TV version", "Anti-Kill of warm-blooded losers"}; / / the picture shown is images = new ArrayList (); for (int I = 0; I < imageIds.length; iTunes +) {ImageView imageView = new ImageView (this) ImageView.setBackgroundResource (imageIdes [I]); images.add (imageView);} / / the displayed point dots = new ArrayList (); dots.add (findViewById (R.id.dot_0)); dots.add (findViewById (R.id.dot_1)); dots.add (findViewById (R.id.dot_2)) Dots.add (findViewById (R.id.dot_3)); dots.add (findViewById (R.id.dot_4)); title = (TextView) findViewById (R.id.title); title.setText (titles [0]); mViewPager = (ViewPager) findViewById (R.id.vp); adapter = new ViewPagerAdapter (); mViewPager.setAdapter (adapter) MViewPager.setOnPageChangeListener (new OnPageChangeListener () {@ Override public void onPageSelected (int position) {/ / TODO Auto-generated method stub title.setText (title [position]); dots.get (oldPosition) .setBackgroundResource (R.drawable.dot_normal); dots.get (position) .setBackgroundResource (R.drawable.dot_focused)) OldPosition = position; currentItem = position } @ Override public void onPageScrolled (int arg0, float arg1, int arg2) {/ / TODO Auto-generated method stub} @ Override public void onPageScrollStateChanged (int arg0) {/ / TODO Auto-generated method stub}}) } private class ViewPagerAdapter extends PagerAdapter {@ Override public int getCount () {/ / TODO Auto-generated method stub return images.size ();} / / whether it is the same picture @ Override public boolean isViewFromObject (View arg0, Object arg1) {/ / TODO Auto-generated method stub return arg0 = = arg1 } @ Override public void destroyItem (ViewGroup view, int position, Object object) {/ / TODO Auto-generated method stub// super.destroyItem (container, position, object); / / view.removeViewAt (position); view.removeView (images.get (position)) } @ Override public Object instantiateItem (ViewGroup view, int position) {/ / TODO Auto-generated method stub view.addView (images.get (position)); return images.get (position);} @ Override protected void onStart () {/ / TODO Auto-generated method stub super.onStart (); scheduledExecutorService = Executors.newSingleThreadScheduledExecutor () / / switch a picture scheduledExecutorService.scheduleWithFixedDelay (new ViewPagerTask (), 2,2, TimeUnit.SECONDS) every 2 seconds;} / / switch picture private class ViewPagerTask implements Runnable {@ Override public void run () {/ / TODO Auto-generated method stub currentItem = (currentItem + 1)% imageIds.length; / / update interface / / handler.sendEmptyMessage (0) Handler.obtainMessage () .sendToTarget ();}} private Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {/ / TODO Auto-generated method stub / / set the current page mViewPager.setCurrentItem (currentItem);}} @ Override protected void onStop () {/ / TODO Auto-generated method stub super.onStop ();}} at this point, the study on "how Android ViewPager can automatically switch pictures every two seconds" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.