Android programming realizes ListView Head Viewpager Advertisement Poll chart effect _android

Source: Internet
Author: User
Tags abs gety

This article is an example of Android programming to achieve ListView Head Viewpager Advertising Poll chart effect. Share to everyone for your reference, specific as follows:

Before looking at someone else's software, found that its advertising map polling time, the ads feel and viewpager effect is not the same, and later checked, because of the time problem, to find some information, their own practice.

1, in order to solve the ListView head plus Viewpager sliding conflict problem, you must customize the ListView, rewrite the Onintercepttouchevent method, ListView code as follows:

Package Com.example.testa; 
Import Android.content.Context; 
Import Android.util.AttributeSet; 
Import android.view.MotionEvent; 
Import Android.widget.ListView; 
 public class Definedlistview extends ListView {public Definedlistview (context) {super); 
 Public Definedlistview (context, AttributeSet attrs) {Super (context, attrs); 
 Public Definedlistview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); 
 Private float xdistance, ydistance, XLAST, Ylast; @Override public boolean onintercepttouchevent (motionevent ev) {switch (ev.getaction ()) {case Motionevent.actio 
    N_down:xdistance = Ydistance = 0f; 
    XLAST = Ev.getx (); 
    Ylast = Ev.gety (); 
   Break 
    Case MotionEvent.ACTION_MOVE:final Float CurX = Ev.getx (); 
    Final float Cury = ev.gety (); 
    Xdistance + + math.abs (curx-xlast); 
    Ydistance + + math.abs (cury-ylast); 
    XLAST = CurX; 
    Ylast = Cury; If (Xdistance > Ydistance) 
    {return false; 
 } return super.onintercepttouchevent (EV);

 } 
}

2, in order to allow the advertising polling to slow down, you must reset the Viewpager scroller, the custom scroller is as follows:

Package Com.example.testa; 
Import Java.lang.reflect.Field; 
Import Android.annotation.SuppressLint; 
Import Android.content.Context; 
Import Android.os.Handler; 
Import android.support.v4.app.FragmentActivity; 
Import Android.support.v4.view.ViewPager; 
Import Android.view.LayoutInflater; 
Import android.view.MotionEvent; 
Import Android.view.View; 
Import Android.view.View.OnTouchListener; 
Import Android.view.animation.Interpolator; 
Import Android.widget.Scroller; 
 public class Definedscroller extends scroller {private int duration_time = 1000; 
 Private Boolean shouldautoscroll = true; 
 Private Viewpager Viewpager = null; @SuppressLint ("Handlerleak") Private final Handler Handler = new Handler () {public void Handlemessage (android.os.Mes 
    Sage msg) {if (shouldautoscroll) {Viewpager.setcurrentitem (Viewpager.getcurrentitem () + 1); 
   Handler.sendemptymessagedelayed (0, 2000); 
 } 
  }; 
 }; 
 Public Definedscroller {Super (context); 
 }Public Definedscroller (context, Interpolator interpolator) {Super (context, interpolator); Private View Initviewpager {View view = Layoutinflater.from (context). Inflate (R.layout.view_pager_
  layout, NULL);
  This.viewpager = (Viewpager) View.findviewbyid (R.id.viewpager); 
  This.viewPager.setOffscreenPageLimit (2); 
  This.viewPager.setAdapter ((fragmentactivity) (Myfragmentpageradapter). Getsupportfragmentmanager ()); 
 return view; 
 Public Viewpager Getviewpager () {return viewpager; 
  public view Initview {View view = Initviewpager (context); This.viewPager.setOnTouchListener (New Ontouchlistener () {@Override public boolean ontouch (View V, motionevent Eve 
     NT) {if (event.getaction () = = motionevent.action_up) {Shouldautoscroll = true; 
    Handler.sendemptymessagedelayed (0, 2000); 
     else {handler.removemessages (0); 
    Shouldautoscroll = false; 
 return false;  } 
  }); 
   try {Field Mscroller = ViewPager.class.getDeclaredField ("Mscroller"); 
   Mscroller.setaccessible (TRUE); 
  Mscroller.set (Viewpager, this); 
  catch (Nosuchfieldexception e) {} catch (IllegalArgumentException e) {} catch (Illegalaccessexception e) {} 
  Handler.sendemptymessagedelayed (0, 2000); 
 return view; 
  public void Setshouldautoscroll () {handler.removemessages (0); 
   if (Viewpager.isshown ()) {Shouldautoscroll = true; 
  Handler.sendemptymessagedelayed (0, 2000); 
  else {shouldautoscroll = false; @Override public void Startscroll (int startx, int starty, int dx, int dy, int duration) {Super.startscroll (s TARTX, starty, dx, dy, shouldautoscroll? 
 Duration_time:duration);

 } 
}

The key is the Startscroll method.

3, other test classes,

(1) Testfragment:

Package Com.example.testa; 
Import Android.os.Bundle; 
Import android.support.v4.app.Fragment; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.ImageView; 
public class Testfragment extends Fragment {public 
 int imageresource = 0; 
 public static testfragment newinstance (int imageresource) { 
  testfragment newfragment = new Testfragment (); 
  Newfragment.imageresource = Imageresource; 
  return newfragment; 
 } 
 @Override public 
 View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { 
  View view = inflater.inflate (r.layout.pager_layout, null); 
  ImageView TextView = (imageview) View.findviewbyid (r.id.image); 
  Textview.setimageresource (Imageresource); 
  return view; 
 } 


(2) The adapter class of Viewpager:

Package Com.example.testa; 
Import android.support.v4.app.Fragment; 
Import Android.support.v4.app.FragmentManager; 
Import Android.support.v4.app.FragmentStatePagerAdapter; 
public class Myfragmentpageradapter extends Fragmentstatepageradapter {public 
 myfragmentpageradapter ( Fragmentmanager FM) { 
  super (FM); 
 } 
 @Override public 
 int GetCount () {return 
  integer.max_value; 
 } 
 @Override public 
 Fragment getitem (int arg0) { 
  switch (arg0% 4) {case 
   0: 
    return Testfragment.newinstance (R.DRAWABLE.A1); 
   Case 1: Return 
    testfragment.newinstance (R.DRAWABLE.DEFAULT_WH); 
   Case 2: Return 
    testfragment.newinstance (R.DRAWABLE.DEFAULT_WW); 
   Case 3: Return 
    testfragment.newinstance (r.drawable.test); 
   Default: Return 
    testfragment.newinstance (R.DRAWABLE.A1);}} 


(3) Test activity:

Package Com.example.testa; 
Import java.util.ArrayList; 
Import java.util.List; 
Import Android.os.Bundle; 
Import android.support.v4.app.FragmentActivity; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.AbsListView; 
Import Android.widget.AbsListView.OnScrollListener; 
Import Android.widget.AdapterView; 
Import Android.widget.AdapterView.OnItemClickListener; 
Import Android.widget.BaseAdapter; 
Import Android.widget.TextView; 
 public class Mainactivity extends Fragmentactivity {private Definedlistview listView = null; 
 Private list<string> List = null; 
 Private Definedscroller Definedscroller = null; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (R.layout.activity_main); 
  ListView = (Definedlistview) Findviewbyid (R.id.listview); 
  Initlistdata (); 
  Initviewpager (); 
  Listview.setadapter (New ListAdapter ()); Listview.setoNitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (adapterview<?> parent, View view, int position, long id) {Definedscroller.getviewpager (). Setcurrentitem (Definedscroller.getviewpager (). Getcurre 
   Ntitem () + 1); 
  } 
  });  Listview.setonscrolllistener (New Onscrolllistener () {@Override public void onscrollstatechanged (Abslistview view, 
    int scrollstate) {if (scrollstate = = Scroll_state_idle) {definedscroller.setshouldautoscroll (); @Override public void Onscroll (Abslistview view, int firstvisibleitem, int visibleitemcount, int totalitem 
 Count) {}}); 
  private void Initlistdata () {list = new arraylist<string> (); 
  for (int i = 1; I <= ++i) {list.add ("test" + i); 
  } private void Initviewpager () {Definedscroller = new Definedscroller (this); 
 Listview.addheaderview (Definedscroller.initview (this)); Private class ListAdapter extends Baseadapter {
  @Override public int GetCount () {return list.size (); 
  @Override public Object getitem (int position) {return list.get (position); 
  @Override public long getitemid (int position) {return position; @Override public View getview (int position, View Convertview, ViewGroup parent) {TextView TextView = (TextView 
   ) Layoutinflater.from (Mainactivity.this). Inflate (r.layout.text_view_layout, NULL); 
   Textview.setpadding (20, 20, 20, 20); 
   Textview.settext (List.get (position)); 
  return textView;

 } 
 } 
}

4, the layout of the file to forget it

Some of the above is seen in other places, for a long time, forget where to see, will not write the source address, please forgive me.

I hope this article will help you with the Android program.

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.