Android homepage carousel images are used directly, and android homepage is used

Source: Internet
Author: User

Android homepage carousel images are used directly, and android homepage is used

Import java. util. arrayList; import java. util. list; import java. util. concurrent. executors; import java. util. concurrent. scheduledExecutorService; import java. util. concurrent. timeUnit; import android. content. context; import android. OS. handler; import android. support. v4.view. viewPager; import android. support. v4.view. viewPager. onPageChangeListener; import android. util. attributeSet; import android. view. gravity; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. widget. imageView; import android. widget. imageView. scaleType; import android. widget. linearLayout; import android. widget. secret; public class HeadAD extends RelativeLayout {private ViewPager vp; private LinearLayout symbolLayout, bottomLayout; private LayoutParams lp; // private ArrayList <ImageView> symbolViews; private List <ViewPagerBean> showResults; private boolean isContinue = true; private int currentItem = 0; // Index Number of the current image private ScheduledExecutorService updateService; private Context context Context; public void initData (List <ViewPagerBean> pResult) {showResults = pResult;} public HeadAD (Context context, AttributeSet attrs) {super (context, attrs); this. context = context; // setBackgroundResource (R. color. black);} public void initView (Context context) {// TODO Auto-generated method stublp = new LayoutParams (LayoutParams. MATCH_PARENT, DensityUtil. dip2px (context, 180); vp = new ViewPager (context); vp. setLayoutParams (lp); lp = new LayoutParams (LayoutParams. MATCH_PARENT, DensityUtil. dip2px (context, 20); lp. addRule (RelativeLayout. ALIGN_PARENT_BOTTOM); bottomLayout = new LinearLayout (context); bottomLayout. setGravity (Gravity. CENTER); bottomLayout. setOrientation (LinearLayout. VERTICAL); bottomLayout. setLayoutParams (lp); bottomLayout. setBackgroundResource (R. color. footerBg); lp = new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT); symbolLayout = new LinearLayout (context); symbolLayout. setGravity (Gravity. CENTER); symbolLayout. setOrientation (LinearLayout. HORIZONTAL); symbolLayout. setPadding (0, 0, 5, 5); ImageView symbolIv; for (int I = 0; I <showResults. size (); I ++) {if (showResults. size ()> 1) {symbolIv = new ImageView (context); LinearLayout. layoutParams params = new LinearLayout. layoutParams (LinearLayout. layoutParams. WRAP_CONTENT, LinearLayout. layoutParams. WRAP_CONTENT); params. rightMargin = 10; symbolIv. setBackgroundResource (R. drawable. point_bg); symbolIv. setEnabled (false); if (I = 0) {symbolIv. setEnabled (true);} else {symbolIv. setEnabled (false);} symbolIv. setLayoutParams (params); symbolLayout. addView (symbolIv) ;}} bottomLayout. addView (symbolLayout); adapter = new ViewPagerAdapter (context, showResults); vp. setAdapter (adapter); vp. setOnTouchListener (new OnTouchListener () {@ Overridepublic boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stubswitch (event. getAction () {case MotionEvent. ACTION_DOWN: isContinue = false; break; case MotionEvent. ACTION_MOVE: isContinue = false; break; case MotionEvent. ACTION_UP: isContinue = true; break; case MotionEvent. ACTION_CANCEL: break;} return false ;}}); vp. setOnPageChangeListener (new MyPageChangeListener (); addView (vp); addView (bottomLayout);} private Handler viewPagerHandler = new Handler () {public void handleMessage (android. OS. message msg) {vp. setCurrentItem (currentItem % showResults. size (); // switch the currently displayed image}; public ViewPagerAdapter adapter; // call this function directly in OnResume of the activity, the current interface starts to loop public void startUpdate () {this. updateService = Executors. newSingleThreadScheduledExecutor (); this. updateService. scheduleAtFixedRate (new ScrollTask (), 5, 5, TimeUnit. SECONDS);} // called directly in the OnPause of the activity. When the current interface is not the activity, the public void stopUpdate () {this. updateService. shutdown (); updateService = null;}/*** call ***/private class MyPageChangeListener implements OnPageChangeListener {private int oldPosition = 0 when the Page Status in ViewPager changes; public void onPageSelected (int position) {currentItem = position % showResults. size (); symbolLayout. getChildAt (oldPosition ). setEnabled (false); symbolLayout. getChildAt (position ). setEnabled (true); oldPosition = position;} public void onPageScrollStateChanged (int arg0) {} public void onPageScrolled (int arg0, float arg1, int arg2) {}}/*** newline switching task ** @ author Administrator **/private class ScrollTask implements Runnable {public void run () {if (isContinue) {synchronized (vp) {System. out. println ("currentItem:" + currentItem); currentItem = (currentItem + 1) % showResults. size (); viewPagerHandler. obtainMessage (). sendToTarget (); // switch the image through Handler }}}}}

HEADAD enables custom carousel timing.

The following describes how to delete some project logic by using the adapter.

Import java. util. list; import android. content. context; import android. content. intent; import android. support. v4.view. pagerAdapter; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. imageView; import com. nostra13.universalimageloader. core. displayImageOptions; /*************************************** **************************************** * ********************************/public class ViewPagerAdapter extends PagerAdapter {List <ViewPagerBean> lists; context ct; private ImageView imgView; private int currentPosition = 0; private DisplayImageOptions options; public ViewPagerAdapter (Context ct, List <ViewPagerBean> mList) {lists = mList; this. ct = ct; options = new DisplayImageOptions. builder (). cacheInMemory (true ). cacheOnDisk (true ). showImageOnLoading (R. drawable. default_news_logo ). build ();}/*** get the total number of pages */public int getCount () {return lists. size ();}/*** get the container of view container view at the corresponding position */public Object instantiateItem (ViewGroup container, final int position) {// imgView. setOnClickListener (this); imgView = new ImageView (ct); imgView. setScaleType (ImageView. scaleType. FIT_XY); if (lists. size () = 0) {} else {// write your own logic currentPosition = position % lists. size ();} // Add a viewcontainer to the container. addView (imgView); // returns an objectreturn imgView relative to the view;}/*** determines the ing between the view and the object */public boolean isViewFromObject (View view, object object) {if (view = object) {return true;} else {return false ;}} /*** destroy the object at the corresponding position */public void destroyItem (ViewGroup container, int position, Object object) {container. removeView (View) object); object = null;} @ Overridepublic int getItemPosition (Object object) {// TODO Auto-generated method stubreturn super. getItemPosition (object );}}
Entity bean will not be pasted here

Tools...

Import android. content. context; public class DensityUtil {/*** convert the unit of dip to px (pixel) */public static int dip2px (Context context, float dpValue) based on the resolution of the mobile phone) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dpValue * scale + 0.5f);}/*** based on the resolution of the phone) to dp */public static int px2dip (Context context, float pxValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (pxValue/scale + 0.5f );}}



How to call:

mViewPager = new HeadAD(getActivity(), null);mViewPager.initData(viewPagerLists);mViewPager.initView(getActivity());
That's OK.


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.