ScrollView滚动后显示按钮并点击置顶

第一步、我们先自定义一个ScorllView,这个自定义的ScrollView完成以下功能:

1.监听ScrollView 的onScrollChanged 滚动改变回调 

2.当滚动距离大于某个值时显示置顶按钮(即我们显示图片)

3.当点击置顶按钮时能让ScrollView滑动到顶部

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);
    }
}
二、布局

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

猜你喜欢

转载自blog.csdn.net/zhangkaiyazky/article/details/79634265