一句話讓你的ScrollView、ListView彈力十足,scrollviewlistview

來源:互聯網
上載者:User

一句話讓你的ScrollView、ListView彈力十足,scrollviewlistview

android預設的ScrollView、ListView在最頂端下拉或者最底端上拉的時候,都不會帶有反彈效果,很生硬的讓你不能繼續拖動,不像iOS那樣可以回彈,個人認為,iOS的互動還是略好一點,那麼我們也來在Android下實現下這個功能,先看下:


那麼我們今天的目標是一句話實現,如何去做呢

我們還是先看下代碼:

package com.xys.flexible;import android.content.Context;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.widget.ScrollView;public class FlexibleScrollView extends ScrollView {private Context mContext;private static int mMaxOverDistance = 50;public FlexibleScrollView(Context context, AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);this.mContext = context;initView();}public FlexibleScrollView(Context context, AttributeSet attrs) {super(context, attrs);this.mContext = context;initView();}public FlexibleScrollView(Context context) {super(context);this.mContext = context;initView();}private void initView() {DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();float density = metrics.density;mMaxOverDistance = (int) (density * mMaxOverDistance);}@Overrideprotected boolean overScrollBy(int deltaX, int deltaY, int scrollX,int scrollY, int scrollRangeX, int scrollRangeY,int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {return super.overScrollBy(deltaX, deltaY, scrollX, scrollY,scrollRangeX, scrollRangeY, maxOverScrollX, mMaxOverDistance,isTouchEvent);}}

看見沒,其實我們雖然重寫了ScrollView,但是我們只改了它的一個方法的一個值!

也就是將overScrollBy中的maxOverScrollY改成了我們自己寫的值。

測試布局:

<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <com.xys.flexible.FlexibleScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="@string/hello_world" >        <TextView            android:id="@+id/tv"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n......我是字串......\n" />    </com.xys.flexible.FlexibleScrollView></RelativeLayout>

就是這樣一個判斷,預設的maxOverScrollY=0,所以我們看不見任何效果,只要改為>0的值,就有效果了,其實我們只是重寫了android的父類方法,但它為什麼沒有實現這樣的效果,我們就不得而知了~~

以上。



解決listview與scrollView之間怎讓listView仍有捲軸

這個,同學,你想實現咋樣的效果呢?可以考慮用 ExpandableList,實現qq好友那樣的效果,如果是想在scrollview中嵌入一個listview,而且還有其他的view,那麼,建議你重寫scrollview,touch的時候決定是傳遞給listview,還是自己消化。
 
對於android ,ScrollView嵌套listview的問題

擷取listItem的方法,只能用來顯示靜態資料,也就是說資料是寫死的。
如果要用來顯示動態,那就必須自訂一個ListView,寫一個類去繼承Linearlayout
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.