ViewPaper implements carousel advertising and viewpaper advertising

Source: Internet
Author: User

ViewPaper implements carousel advertising and viewpaper advertising

Use the viewPaper component in the V4 package to customize the carousel advertising effect.


Implements sliding switching and timed automatic switching of viewpaper.

Upper



Layout File

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <android.support.v4.view.ViewPager        android:id="@+id/viewpaper"        android:layout_width="fill_parent"        android:layout_height="200dp" >    </android.support.v4.view.ViewPager>    <LinearLayout        android:gravity="center_horizontal"        android:id="@+id/ll_footer"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignBottom="@id/viewpaper"        android:background="#33000000"        android:orientation="vertical" >        <TextView            android:id="@+id/tv_imageDesc"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <LinearLayout                        android:id="@+id/ll_pointGroup"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="horizontal" >        </LinearLayout>    </LinearLayout></RelativeLayout>


There is also a selector

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


Class File

Package com. example. viewpaperdemo; import java. util. arrayList; import java. util. list; 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 Oid. widget. imageView; import android. widget. linearLayout; import android. widget. textView; public class MainActivity extends Activity {private ViewPager viewpaper; private TextView TV _imageDesc; private LinearLayout ll_pointGroup; private List <ImageView> imageList; // image resource ID private final int [] imageIds = {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d, R. drawable. e}; // image title set private fina L String [] imageDescriptions = {"Gong Li is not vulgar, so I cannot be vulgar", "the tree 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"}; protected int lastPosition; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); viewpaper = (ViewPager) findViewById (R. id. viewpaper); TV _imageDesc = (TextView) findViewById (R. id. TV _imageDesc); ll_pointGroup = (LinearLayout) findViewById (R. id. ll_pointGroup); TV _imageDesc.setText (imageDescriptions [0]); imageList = new ArrayList <ImageView> (); for (int I = 0; I <imageIds. length; I ++) {ImageView imageView = new ImageView (MainActivity. this); imageView. setImageResource (imageIds [I]); imageList. add (imageView); // add the indicator ImageView point = new ImageView (this); LinearLayout. layoutParams params = new LinearLayout. layoutParams (LinearLayout. layoutParams. WRAP_CONTENT, LinearLayout. layoutParams. WRAP_CONTENT); params. rightMargin = 15; point. setLayoutParams (params); point. setBackgroundResource (R. drawable. doet); if (I = 0) {point. setEnabled (true);} else {point. setEnabled (false);} ll_pointGroup.addView (point);} // sets the adapter viewpaper. setAdapter (new MyAdapter (); // initialize the position viewpaper. setCurrentItem (Integer. MAX_VALUE/2-(Integer. MAX_VALUE/2% imageList. size (); // sets the listener viewpaper. setOnPageChangeListener (new OnPageChangeListener () {// After sliding @ Override public void onPageSelected (int position) {position = position % imageList. size (); // set the text description content TV _imageDesc.setText (imageDescriptions [position]); // change the status of the indicator vertex // set the current vertex enbale to true ll_pointGroup.getChildAt (position ). setEnabled (true); // set the previous vertex to false ll_pointGroup.getChildAt (lastPosition ). setEnabled (false); lastPosition = position; TV _imageDesc.setText (imageDescriptions [position]);} // @ Override public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {}// status change @ Override public void onPageScrollStateChanged (int state) {}}); isRunning = true; handler. sendEmptyMessageDelayed (0, 2000);} private boolean isRunning = false; Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {// slide to the next page viewpaper. setCurrentItem (viewpaper. getCurrentItem () + 1); // execute if (isRunning) {handler. sendEmptyMessageDelayed (0, 2000) ;}}; protected void onDestroy () {isRunning = false ;}; public class MyAdapter extends PagerAdapter {@ Override public int getCount () {return Integer. MAX_VALUE ;}@ Override public boolean isViewFromObject (View view, Object object) {return view = object ;}@ Override public Object instantiateItem (ViewGroup container, int position) {position = position % imageList. size (); container. addView (imageList. get (position); return imageList. get (position) ;}@ Override public void destroyItem (ViewGroup container, int position, Object object) {container. removeView (View) object); object = null ;}}}



Related Article

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.