如何在android中制作可伸缩的滚动视图?

时间:2015-05-16 11:46:35

标签: android android-layout

我想在我的应用程序中制作一个可伸缩的scrollview,但我不知道如何制作它。我试图搜索,但找不到任何东西。

1 个答案:

答案 0 :(得分:1)

已编辑希望这会有所帮助

public class ElasticListView extends ListView
{
    private static final int MAX_Y_OVERSCROLL_DISTANCE = 250;

    private Context mContext;
    private int mMaxYOverscrollDistance;

    public ElasticListView(Context context)
    {
        super(context);
        mContext = context;
        initBounceListView();
    }

    public ElasticListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mContext = context;
        initBounceListView();
    }

    public ElasticListView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        mContext = context;
        initBounceListView();
    }

    private void initBounceListView()
    {
        //get the density of the screen and do some maths with it on the max overscroll distance
        //variable so that you get similar behaviors no matter what the screen size
        final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
        final float density = metrics.density;
        mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent)
    {
        //This is where the magic happens, we have replaced the incoming maxOverScrollY with our own custom variable mMaxYOverscrollDistance;
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, mMaxYOverscrollDistance, isTouchEvent);
    }

}