ScrollView shows the button after scrolling and clicks on top

The first step, we first customize a ScorllView, this customized ScrollView completes the following functions:

1. Listen to ScrollView's onScrollChanged scroll change callback 

2. Display the top button when the scroll distance is greater than a certain value (that is, we display the picture)

3. When the top button is clicked, the ScrollView can 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);    
    }
}
2. Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>                
    <chilijie.baway.com.scollviewshow.GoTopScrollview
        android:id="@+id/sv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上部" />
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="400dp"></ListView>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:src="@mipmap/ic_launcher"
        />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:src="@mipmap/ic_launcher"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下部" />
</LinearLayout>
    </chilijie.baway.com.scollviewshow.GoTopScrollview>
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="50dp"
        android:layout_marginBottom="50dp"
        android:src="@mipmap/ic_launcher"
        android:visibility="gone"
        />
</RelativeLayout>
三、Activity代码

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<String> list=new ArrayList<>();
        for (int i = 0; i <50 ; i++) {
           list.add("条目"+i);
        }
        lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,list));
        sv.setImgeViewOnClickListener(iv);
        sv.setHeight(400);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325748272&siteId=291194637