Android custom control 6-Implementation of carousel image advertisement and android6 --

Source: Internet
Author: User

Android custom control 6-Implementation of carousel image advertisement and android6 --

This article continues to implement the monitoring and rolling of carousel images and advertisements.

URL: http://www.cnblogs.com/wuyudong/p/5920757.html.

First, add the layout file to implement

Shape_bg_point_enable.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="oval" >    <corners android:radius="5dp" />    <solid android:color="@android:color/white" /></shape>

Shape_bg_point_disable.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="oval" >    <corners android:radius="5dp" />    <solid android:color="@android:color/darker_gray" /></shape>

Add another selector.

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_enabled="true" android:drawable="@drawable/shape_bg_point_enable"></item>    <item android:state_enabled="false" android:drawable="@drawable/shape_bg_point_disable"></item></selector>

Add selector to view:

For (int I = 0; I <imageResIds. length; I ++) {imageView = new ImageView (this); imageView. setBackgroundResource (imageResIds [I]); imageViewList. add (imageView); // add a white dot, indicator pointView = new View (this); pointView. setBackgroundResource (R. drawable. selector_bg_point); layoutParams = new LayoutParams (7, 7); if (I! = 0) {layoutParams. leftMargin = 10;} pointView. setEnabled (false); ll_point_container.addView (pointView, layoutParams );}

Add a sliding listener for ViewPager

Public class MainActivity extends Activity implements OnPageChangeListener ...... @ Override public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {// call during scrolling} @ Override public void onPageSelected (int position) {// call position = position % 5; TV _desc.setText (contentDesc [position]) when the new entry is selected; // disable the previous one and enable the latest ll_point_container.getChildAt (lastEnablePoint ). setEnabled (false); ll_point_container.getChildAt (position ). setEnabled (true); lastEnablePoint = position;} @ Override public void onPageScrollStateChanged (int state) {// called when the rolling status changes}

Enable automatic page round robin:

// Enable polling for new Thread () {public void run () {while (isRunning) {try {Thread. sleep (2000);} catch (InterruptedException e) {e. printStackTrace ();} // jump down to a runOnUiThread (new Runnable () {@ Override public void run () {viewPager. setCurrentItem (viewPager. getCurrentItem () + 1 );}});}};}. start ();

The complete code is as follows:

Package com. wuyudong. viewpager; import java. util. arrayList; import android. r. layout; import android. OS. bundle; import android. app. activity; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. support. v4.view. viewPager. onPageChangeListener; import android. view. menu; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; impor T android. widget. imageView; import android. widget. linearLayout; import android. widget. linearLayout. layoutParams; import android. widget. textView; public class MainActivity extends Activity implements OnPageChangeListener {private ViewPager viewPager; private int [] imageResIds; private ArrayList <ImageView> imageViewList; private ArrayList <View> pointViews; private LinearLayout parameters; priva Te String [] contentDesc; private TextView TV _desc; private int lastEnablePoint = 0; private boolean isRunning = true; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // initialize the Layout View initViews (); // Model data initData (); // Controller initAdapter (); // enable polling new Thread () {public void run () {while (isRunnin G) {try {Thread. sleep (2000);} catch (InterruptedException e) {e. printStackTrace ();} // jump down to a runOnUiThread (new Runnable () {@ Override public void run () {viewPager. setCurrentItem (viewPager. getCurrentItem () + 1 );}});}};}. start () ;}@ Override protected void onDestroy () {super. onDestroy (); isRunning = false;} private void initViews () {viewPager = (ViewPager) findViewById (R. id. viewpager ); ViewPager. setOnPageChangeListener (this); // sets the rolling update TV _desc = (TextView) findViewById (R. id. TV _desc); ll_point_container = (LinearLayout) findViewById (R. id. ll_point_container);} private void initAdapter () {ll_point_container.getChildAt (0 ). setEnabled (true); TV _desc.setText (contentDesc [0]); // sets the adapter viewPager. setAdapter (new MyAdapter (); // It is set to a certain position in the middle by default. int pos = Integer. MAX_VALUE/2; // viewPager. set CurrentItem (pos-3); viewPager. setCurrentItem (5000000);} private void initData () {// initialize the data to be displayed // image resource id array imageResIds = new int [] {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d, R. drawable. e}; // text description contentDesc = new String [] {"Gong Li is not vulgar, so I cannot be vulgar", "Park Shu is back! Sing a classic old song to attract a chorus of thousands of people "," reveal how Beijing movies are upgraded "," letv TV edition big delivery "," hot-blooded anti-Kill "}; imageViewList = new ArrayList <ImageView> (); pointViews = new ArrayList <View> (); ImageView imageView; View pointView; LayoutParams layoutParams; for (int I = 0; I <imageResIds. length; I ++) {imageView = new ImageView (this); imageView. setBackgroundResource (imageResIds [I]); imageViewList. add (imageView); // add a white dot, indicator pointView = new View (this); poi NtView. setBackgroundResource (R. drawable. selector_bg_point); layoutParams = new LayoutParams (7, 7); if (I! = 0) {layoutParams. leftMargin = 10;} pointView. setEnabled (false); ll_point_container.addView (pointView, layoutParams);} class MyAdapter extends PagerAdapter {@ Override public int getCount () {return Integer. MAX_VALUE;} // 3. Specify the reusable judgment logic @ Override public boolean isViewFromObject (View view, Object object) {// when sliding to a new entry, return again, whether the view can be reused return view = object;} // 1. return the content of the entry to be displayed. Create the entry @ Override public Object instantiateItem (ViewGroup container, int position) {// container: ViewPager // position: position of the current entry to be displayed = position % 5; ImageView imageView = imageViewList. get (position); System. out. println ("instantItem creation:" + position); // a. Add the view object to the view iner. addView (imageView); // B. return the view object to the Framework. The adapter returns imageView. // It must be rewritten, otherwise, an exception is thrown.} // 2. The destroy entry @ Override public void destroyItem (ViewGroup container, int position, Object object) {// The object System to be destroyed. out. println ("destroyItem destruction:" + position); container. removeView (View) object); }}@ Override public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {// call during scrolling} @ Override public void onPageSelected (int position) {// call position = position % 5 when the new entry is selected; TV _desc.setText (contentDesc [position]); // disable the previous one and enable the latest ll_point_container.getChildAt (lastEnablePoint ). setEnabled (false); ll_point_container.getChildAt (position ). setEnabled (true); lastEnablePoint = position;} @ Override public void onPageScrollStateChanged (int state) {// called when the rolling status changes }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.