Android Project Combat (20): A brief talk on the display effect of the ListView suspension head

Source: Internet
Author: User

Original: Android Project Combat (20): A brief talk on the display effect of the ListView suspension head

First look at the effect: The requirement is a sliding list, and some of the views (the number of fans, the number of followers) do not disappear while sliding to the top, but instead stay on the entire interface head.

Let's first analyze the problem to be solved:

1. How to Implement list ListView top view follow ListView slide together

2, how to achieve the sliding process need to stay on the top of the view

Solve:

The first problem, implementing a ListView slide along with the top view, the ListView provides a way to Addheadview (view); It means adding a view to the top of the ListView. Then this view can be scrolled with the ListView.

The second question is, how do you make sure that some part of the interface in the middle of the screen slides to the top?

First of all, the view that stays at the top (called View1) is Listview.addheadview (), that is, the sliding list, the View1 will be drawn out, then how to let it not go out? As long as the ListView is at the top of the layout

Also write a same view (called View2,view2 and ListView belonging to a fragmentlayout) and hide it first (visible = ' gone '), when View1 just draw the top, View2 display.

View1 just drew the top of the time is:

When sliding firstvisibleitem>= to suspend the item's position, let View2 display, otherwise hidden.

Code implementation:

First, the layout file:

Head layout:

1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3android:orientation="Vertical"4Android:layout_width="match_parent"5android:layout_height="wrap_content"6android:gravity="Center"7>8<ImageView9Android:layout_width="match_parent"Tenandroid:layout_height="90DP" OneAndroid:src="@mipmap/P1" AAndroid:scaletype="Fitxy" -/> -</LinearLayout>

To stay at the top of the View layout: (here is to stay at the top of the view, here Addheadview to the top of the ListView, the follower of the ListView sliding to the top disappears, then satisfies the firstvisibleitem>= to hover Item's position condition, the main interface is to write an identical view display can be)

<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Horizontal"Android:layout_width="fill_parent"Android:layout_height="wrap_content"android:gravity="Center"> <ImageView android:layout_width="match_parent"Android:layout_height="50DP"android:src="@mipmap/p2"Android:scaletype="Fitxy"/></linearlayout>

Main layout:

<linearlayout 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:orientation="Vertical"> <TextView Android:id="@+id/title"Android:layout_width="match_parent"Android:layout_height="30DP"android:gravity="Center"Android:text="Personal Center"android:textsize="20DP"/> <framelayout android:layout_width="match_parent"Android:layout_height="wrap_content"> <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="wrap_content"android:orientation="Horizontal"android:visibility="Gone"> <ImageView android:layout_width="match_parent"Android:layout_height="50DP"android:src="@mipmap/p2"Android:scaletype="Fitxy"/> </LinearLayout> </FrameLayout></LinearLayout>

Java code:

1 Privatelinearlayout Invis;2     PrivateListView LV;3 string[] STRs;4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6 super.oncreate (savedinstancestate);7 Setcontentview (r.layout.activity_main);8 9Invis =(LinearLayout) Findviewbyid (R.id.invis);Ten  OneSTRs =Newstring[ -]; A  -          for(inti =0; I < -; i++) { -Strs[i] ="Data-----"+i; the         } -  -LV =(ListView) Findviewbyid (r.id.lv); -View Header = View.inflate ( This, R.layout.stick_header,NULL);//Head Content +Lv.addheaderview (header);//Add Head -  +Lv.addheaderview (View.inflate ( This, R.layout.stick_action,NULL));//the suspended portion of the ListView entry is added to the head A  atLv.setadapter (NewArrayadapter<string> ( This, - Android. R.layout.simple_list_item_1, STRs)); -Lv.setonscrolllistener (NewAbslistview.onscrolllistener () { -  - @Override -              Public voidOnscrollstatechanged (Abslistview view,intscrollstate) { in  -             } to  + @Override -              Public voidOnscroll (Abslistview view,intFirstvisibleitem,intVisibleItemCount,intTotalitemcount) { the                 if(Firstvisibleitem >=1) { * invis.setvisibility (view.visible); $}Else {Panax Notoginseng  - invis.setvisibility (view.gone); the                 } +             } A         }); the  +}

Android Project Combat (20): A brief talk on the display effect of the ListView suspension head

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.