One sentence makes your ScrollView and ListView fully scalable, scrollviewlistview

Source: Internet
Author: User

One sentence makes your ScrollView and ListView fully scalable, scrollviewlistview

The default ScrollView and ListView of android do not show a bounce effect when they are pulled from the top or bottom of the screen. It is so stiff that you cannot continue to drag, unlike iOS, I personally think that iOS interaction is a little better, so we can also implement this function in Android. Let's take a look at it first:


So our goal today is to achieve it in one sentence. How can we do it?

Let's take a look at the Code:

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

I can't see it. In fact, although we have rewritten ScrollView, we only changed the value of one of its methods!

That is, we changed maxOverScrollY in overScrollBy to our own value.

Test layout:

<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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" 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 = "...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n "/> </com. xys. flexible. flexibleScrollView> </RelativeLayout>

The default value is maxOverScrollY = 0, so we cannot see any effect. If we change it to a value greater than 0, it will have an effect. In fact, we just override the parent class method of android, however, we don't know why it didn't achieve this effect ~~

Above.



Solve the problem of having a scroll bar between listview and scrollView

What effect do you want to achieve? You can use ExpandableList to achieve the same effect as qq friends. If you want to embed a listview in scrollview and have other views, we recommend that you rewrite scrollview, when you touch it, decide whether to pass it to listview or digest it by yourself.

ScrollView nested listview for android

The listItem method can only be used to display static data, that is, the data is written to death.
To Display Dynamic Data, you must customize a ListView and write a class to inherit Linearlayout.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.