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 );}}