Click the scroll button to display the ScrollView and click the top button.

Source: Internet
Author: User

Click the scroll button to display the ScrollView and click the top button.

Step 1: first define an ScorllView. This custom ScrollView completes the following functions:

1. Listen to the onScrollChanged rolling change callback of ScrollView

2. When the scroll distance is greater than a certain value, the pin button is displayed (that is, the image is displayed)

3. When you click the top button, the ScrollView will slide to the top.

public class GoTopScrollview extends ScrollView implements View.OnClickListener {    private int height=300;    private ImageView iv;    public GoTopScrollview(Context context) {        super(context);    }    public GoTopScrollview(Context context, AttributeSet attrs) {        super(context, attrs);    }    public GoTopScrollview(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }   public void setHeight(int height){       this.height=height;   }    public void setImgeViewOnClickListener(ImageView iv) {        this.iv = iv;        this.iv.setOnClickListener(this);    }    @Override    protected void onScrollChanged(int l, int t, int oldl, int oldt) {        super.onScrollChanged(l, t, oldl, oldt);        if (height!=0){            if (t>height){                iv.setVisibility(VISIBLE);            }else{                iv.setVisibility(GONE);            }        }    }    @Override    public void onClick(View v) {       this.scrollTo(0,0);    }}
Ii. Layout
 
     
  
       
        
         
         
       
       
      
     
    
       
      
  
 
3. Activity Code
Public class MainActivity extends AppCompatActivity {private ImageView iv; private GoTopScrollview sv; @ TargetApi (Build. VERSION_CODES.M) @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout.Activity_main); ListView lv = (ListView) findViewById (R. id.Lv); Sv = (GoTopScrollview) findViewById (R. id.Sv); Iv = (ImageView) findViewById (R. id.Iv); List
 
  
List = new ArrayList <> (); for (int I = 0; I <50; I ++) {list. add ("entry" + I);} lv. setAdapter (new ArrayAdapter
  
   
(This, android. R. layout.
   Simple_expandable_list_item_1, List); sv. setImgeViewOnClickListener (iv); sv. setHeight (400 );}}
  
 

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.