用戶要求一個特效,做了一個Demo,分享一下。
1.羅列代碼
<LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:layout_width ="match_parent" android:layout_height ="match_parent" android:orientation ="vertical" > <View
android:id ="@+id/placeholder_top" android:layout_width ="match_parent" android:layout_height ="200dp" android:background ="@android:color/transparent" /> <View
android:id ="@+id/placeholder_sticky" android:layout_width ="match_parent" android:layout_height ="50dp" />
</LinearLayout >
<FrameLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:layout_width ="match_parent" android:layout_height ="match_parent" > <View
android:id ="@+id/top" android:layout_width ="match_parent" android:layout_height ="200dp" android:background ="@drawable/ic_launcher" /> <ListView
android:id ="@+id/listView" android:layout_width ="match_parent" android:layout_height ="wrap_content" > </ListView > <TextView
android:id ="@+id/sticky" android:layout_width ="match_parent" android:layout_height ="50dp" android:background ="#00ffff" android:gravity ="center" android:textColor ="#ff0000" android:text ="Sticky" android:textSize ="20sp" />
</FrameLayout >
package com.example.android.listviewscrolltrick;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.listviewscrolltrick.R;
public class MainActivity extends Activity {private TextView mStickyView;
private View mPlaceHolderSticky;
private ListView mListView;
private View mTopView;
@SuppressWarnings (
"deprecation" )
@Override protected void onCreate (Bundle savedInstanceState) {requestWindowFeature(Window.FEATURE_NO_TITLE);
super .onCreate(savedInstanceState);setContentView(R.layout.activity_main);mStickyView = (TextView) findViewById(R.id.sticky);mListView = (ListView) findViewById(R.id.listView);mTopView = findViewById(R.id.top);LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);View headView = inflater.inflate(R.layout.layout_headview,
null );mPlaceHolderSticky = headView.findViewById(R.id.placeholder_sticky);mListView.addHeaderView(headView);mListView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint (
"NewApi" )
@SuppressWarnings (
"deprecation" )
@Override public void onGlobalLayout () {onScrollChanged();ViewTreeObserver obs = mListView.getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {obs.removeOnGlobalLayoutListener(
this );}
else {obs.removeGlobalOnLayoutListener(
this );}}});mListView.setOnScrollListener(
new AbsListView.OnScrollListener() {
@Override public void onScrollStateChanged (AbsListView view,
int scrollState) {}
@Override public void onScroll (AbsListView view,
int firstVisibleItem,
int visibleItemCount,
int totalItemCount) {onScrollChanged();}});List<String> dataSource =
new ArrayList<String>();
for (
int j =
0 ; j <
20 ; j++) {dataSource.add(
"item-----" + j);}ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(
this ,android.R.layout.simple_list_item_1, dataSource);mListView.setAdapter(arrayAdapter);}
private void onScrollChanged () {View v = mListView.getChildAt(
0 );
int top = (v ==
null ) ?
0 : v.getTop();
if (mListView.getFirstVisiblePosition() ==
0 ) {mStickyView.setTranslationY(Math.max(mPlaceHolderSticky.getTop()+ top,
0 ));mTopView.setTranslationY(top);}}
}
2.大致原理講解
3.結合源代碼對原理進行深入講解
初始階段位置的調整 對ListView加載完成進行監聽 注意 :要取消監聽
mListView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint (
"NewApi" )
@SuppressWarnings (
"deprecation" )
@Override public void onGlobalLayout () {onScrollChanged();ViewTreeObserver obs = mListView.getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {obs.removeOnGlobalLayoutListener(
this );}
else {obs.removeGlobalOnLayoutListener(
this );}}});
根據ListView中headView中的對應部分 ,設置mTopView/mStickView位置
private void onScrollChanged () {View v = mListView.getChildAt(
0 );
int top = (v ==
null ) ?
0 : v.getTop();
if (mListView.getFirstVisiblePosition() ==
0 ) {mStickyView.setTranslationY(Math.max(mPlaceHolderSticky.getTop()+ top,
0 ));mTopView.setTranslationY(top);}}
由于mStickyView要一直顯示在可見范圍內,所以mStickyView.setTranslationY一定不能為負數.
監聽ListView滑動,調整mTopView/mStickView的位置(同上)
mListView.setOnScrollListener(
new AbsListView.OnScrollListener() {
@Override public void onScrollStateChanged (AbsListView view,
int scrollState) {}
@Override public void onScroll (AbsListView view,
int firstVisibleItem,
int visibleItemCount,
int totalItemCount) {onScrollChanged();}});
3.Demo下載
http://download.csdn.net/detail/guchuanhang/9515429
總結
以上是生活随笔 為你收集整理的仿蜻蜓FM专辑页面滑动特效 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。