Open source Pull_to_refresh controls use

Source: Internet
Author: User

Learning Knowledge points
    1. onTouchThe event delivery mechanism.
    2. LisenterListening
    3. ImageViewThe src background scaleType display of different properties.
    4. onTouchThe slide lifted up the call MotionEvent.ACTION_CANCEL is not MotionEvent.ACTION_UP
Development steps

① Creating Activity Layout
② Inherit Viewpager Create Autoscrollviewpager
③ Display points
④ adding monitoring Onpagechangedlistener
⑤ Automatic scrolling
⑥ Stop scrolling when manual scrolling

Inherit Viewpager Create Autoscrollviewpager
public  Span class= "Hljs-class" >class  autoscrollviewpager  extends  viewpager   {

//. xml
public Span class= "Hljs-title" >autoscrollviewpager (context context, AttributeSet attrs) {
super (context, attrs);
}
<com.itheima.autoscroll.view.autoscrollviewpager
Android:id= "@+id/sroll_ Viewpager "
Android:layout_width=" match_parent "
Android:layout_ Height= "match_parent"
Android:background= "# 47659E "/>
Set dot
//Selector: Selector objects that manage footage display different pictures depending on the state of the press
//Select=true false
//view.setslected ()
Privatelist<imageview> dots =NewArraylist<imageview> ();
Public void init(int pagenumber, linearlayout layoutdot) {
//3
PageCount = pagenumber;
//. XML
//. Java
for(inti =0; i < pagenumber; i++) {
ImageView img =NewImageView (GetContext ());
Img.setbackgroundresource (R.drawable.dot_selector);
Img.setselected (false);//Red
//. XML Layout_width layout_height
//. Java linearlayout.layoutparams Layout Parameters
Linearlayout.layoutparams p =NewLinearlayout.layoutparams (//
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//. XML MarginRight
//. Java rightMargin
P.rightmargin =6;
Layoutdot.addview (IMG, p);
Dots.add (IMG);
}
Dots.get (0). setselected (true);
//Set content
Pageradapter adpater =NewImageviewadapter ();
This. Setadapter (Adpater);//Pageadpater fragmentpageadapter

//④ Adding monitoring Onpagechangedlistener
//⑤ Auto-Scroll hold point synchronization
//⑥ Stop Scrolling when manual scrolling
Onpagechangelistener listener =NewMyonpagechangelistener ();
This. Setonpagechangelistener (listener);//Onpagechangelistener monitor swipe to page
}
Add a page Select Listen listen 同步

ViewPagecontrol the page's title and Dot synchronization by checking the status


Private class Myonpagechangelistener implements Onpagechangelistener {
@Override
Public void onpagescrollstatechanged(int arg0) {

}
@Override
Public void onpagescrolled(int arg0, float arg1, int ARG2) {

}

//Slide The first few pages
@Override
Public void onpageselected(int arg0) {

//3
//0 1 2 3 4 5
//Take -out operation
//0 1 2->0 1 2
//Old dot not lit
if(islooping) {
Dots.get (currpageindex% pagecount). setselected (false);
//New Point highlighting
Currpageindex = arg0;
Dots.get (currpageindex% pagecount). setselected (true);
}Else{
Dots.get (Currpageindex). setselected (false);
//New Point highlighting
Currpageindex = arg0;
Dots.get (Currpageindex). setselected (true);
}

}

}

Private intCurrpageindex =0;

Private class imageviewadapter extends pageradapter {

//Pages
@Override
Public int getcount() {
if(islooping) {
returnInteger.max_value;
}Else{
returnPageCount;
}
}

@Override
Public boolean isviewfromobject(View arg0, Object arg1) {
returnarg0 = = Arg1;
}

//Current display view
@Override
Public Object instantiateitem(ViewGroup container, int position) {
//Create a displayed page is a picture
ImageView imag =NewImageView (GetContext ());
Imag.setbackgroundresource (R.drawable.home_scroll_default);
//. XML Layout_width layout_height
//. Java viewpager.layoutparams
Viewpager.layoutparams p =NewViewpager.layoutparams ();
P.width = ViewPager.LayoutParams.MATCH_PARENT;
P.height = ViewPager.LayoutParams.MATCH_PARENT;
Container.addview (Imag, p);


returnImag
}

//Remove display

@Override
Public void destroyitem(ViewGroup container, int position, object object) {
Container.removeview ((ImageView) object);
}

}
Automatic Carousel

principle: by Handle sending a delay message. Implementations similar to recursion

2.4. Auto Scroll
//Support unlimited sliding
Private BooleanIslooping =false;
Public void setlooping(boolean flag) {
islooping = Flag;
}
PrivateHandler Handler =NewHandler () {
///callback function: 1. Override 2. System or other object invocation
// on
Public void handlemessage(android.os.Message msg) {
if(Msg.what = =1) {
LOG.I ("WZX","Switch to next page");
//Switch to next page
intCurr = Getcurrentitem ();
++curr;
Setcurrentitem (Curr);//Display specified page
//Rewrite to send a message to yourself
//Message msg=new message ();
Message MSG2 = Handler.obtainmessage ();//Obtain=get is an optimized notation
//Internally Find reusable message if there is no reuse to create new.
Msg2.what =1;
Handler.sendmessagedelayed (MSG2, the);//---->handlemessage
//Cyclic transmission of interest
}
};
};

//Support auto Play
//exposed to outside calls.
Public void startscroll() {
//
//Message msg=new message ();
Message msg = Handler.obtainmessage ();//Obtain=get is an optimized notation
//Internally Find reusable message if there is no reuse to create new.
Msg.what =1;
Handler.sendmessagedelayed (MSG, the);//---->handlemessage

}

/**
* Stop Playing
*/
Public void stopscroll() {
//Clear All messages Handlemessage will not execute
//Use callback function without condition to satisfy
Handler.removecallbacksandmessages (NULL);//

}
Stop scrolling when manual scrolling
Ontouchlistener listener =NewMyontouchlistener ();
Imag.setontouchlistener (listener);

Private class Myontouchlistener implements Ontouchlistener {

Private intDownx =0;
Private LongDowntime =0;

//Motionevent location Change small rocket
@Override
Public boolean onTouch(View V, motionevent event) {
Switch(Event.getaction ()) {//Get Action
CaseMotionevent.action_down://Press
Stopscroll ();
Downx = (int) Event.getx ();//Get on the inferior
Downtime = System.currenttimemillis ();//Save press time
LOG.I ("WZX","Action_down");
Break;
CaseMotionevent.action_move://Mobile
LOG.I ("WZX","Action_move");
Break;
CaseMOTIONEVENT.ACTION_UP://Filed
LOG.I ("WZX","Action_up");
intUpX = (int) Event.getx ();
if(Downx = = UpX && system.currenttimemillis ()-Downtime < -) {

//Use command design mode to add an interface as a listener
//1. Creating interface
//2. Abstract method (extract code to remove method body)
//3. Adding listeners
//4. The listener is to be called.
//Let the developer write the code in the Listener (outside the control)
//Onitemclick ();
if(listener!=NULL)
{
Listener.onitemclick ();//code inside the method
}
}
Startscroll ();
Break;
CaseMotionevent.action_cancel://Cancel
LOG.I ("WZX","Action_cancel");
Startscroll ();
Break;
}
return true;//Return value handling this event returns True
}


}
Stand-alone callback CommandDesign Patterns

principle:

    1. Setting the listener interface, defining the method
private OnViewClickListener listener ;
publicinterface OnViewClickListener
{
public void onViewClick();
}
    1. Setting the Listening method
public void setOnViewClickListener(OnViewClickListener listener)
{
this.listener = listener;
}
    1. ACTION_UPinvoke callback method on touch
case MotionEvent.ACTION_UP:
startScroll();
//保证是单机,
if(dowx == event.getX() & System.currentTimeMillis() - mDownTimel<300)
{
//回调事件
listener.onViewClick();
}

break;

Open source Pull_to_refresh controls use

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.