Android implements the floating effect on the top of the ListView, androidlistview

Source: Internet
Author: User

Android implements the floating effect on the top of the ListView, androidlistview

First, implement the following results:




At first, I searched the internet to achieve this effect. The "purchase box" floating Effect of Meituan and public comments is also the same, but the implementation by the author is quite troublesome, I thought about it and implemented it based on some features provided by ListView.

The entire layout is a ListView. If the content on the top of the listview is relatively high, you can use it as the header of the listview, this can also avoid the trouble caused by ScrollView nested ListView (remember this will not cause various problems of ScrollView and ListView at ordinary times ). To achieve the floating effect, the operation is mainly based on the first entry visible to the ListView. Note that the "floating" here is actually implemented by hiding/displaying the floating part: there is a hidden "floating part" behind the "ListView ", when the floating part in the ListView entry becomes the first visible part of the ListView, the floating part of the non-ListView entry is displayed, which causes the floating part to keep floating on the top, when the first entry in the visible part of the ListView is not suspended, the "hidden part" of the non-ListView part continues to be hidden. The expression ability is not strong, and you are almost dizzy by yourself. In short, there are two floating parts: the items in the ListView entry and hidden at the top of the layout (Gone/invisible) are not displayed at first, these two operations are used to complete the entire function. Hope readers can understand ......

Main Code:

@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_sticky); invis = (LinearLayout) findViewById (R. id. invis); strs = new String [100]; for (int I = 0; I <20; I ++) {strs [I] = "data -----" + I ;} lv = (ListView) findViewById (R. id. lv); View header = View. inflate (this, R. layout. stick_header, null); // the header content lv. addHeaderView (header); // Add the lv header. addHeaderView (View. inflate (this, R. layout. stick_action, null); // Add the floating part in the ListView entry to the header lv. setAdapter (new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, strs); lv. listener (new OnScrollListener () {@ Overridepublic void onScrollStateChanged (AbsListView view, int scrollState) {}@ Overridepublic void onScroll (AbsListView view, int cursor, int visibleItemCount, int totalItemCount) {if (firstVisibleItem> = 1) {invis. setVisibility (View. VISIBLE);} else {invis. setVisibility (View. GONE );}}});}


Layout:

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <TextView android: id = "@ + id/title" android: layout_width = "match_parent" android: layout_height = "30dp" android: background = "# 332b3b" android: gravity = "center" android: text = "title" android: textColor = "# ffffff"/> <FrameLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_below = "@ id/title"> <ListView android: id = "@ + id/lv" android: layout_width = "match_parent" android: layout_height = "match_parent"/> <LinearLayout android: id = "@ + id/invis" android: layout_width = "fill_parent" android: layout_height = "50dp" android: background = "# ccedc7" android: orientation = "horizontal" android: visibility = "gone"> <TextView android: id = "@ + id/TV" android: layout_width = "match_parent" android: layout_height = "50dp" android: gravity = "center" android: text = "floating part"/> </LinearLayout> </FrameLayout> </RelativeLayout>

Code: http://download.csdn.net/download/ljfbest/7804769


How does android listview achieve this rounded corner and press effect? The question is sent again. Come and get the score.

It can be done by layout and a little logic. Specifically, the list body actually has a Margin value left and right, while the above transparent cover layer is full between the left and right, which makes it easy.

You can see the demo of my attachment. I have not adjusted the color. I am not very good at Color adjustment. But the effect is similar.


How does Android achieve a pass-through effect? For example, if listview has only one item, it can slide up or down.

Overscroll is a feature provided by the version.
Enabled by default from android2.3

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.