Last active
August 29, 2015 14:04
-
-
Save AvatarQing/403f287b3c583c8bdde6 to your computer and use it in GitHub Desktop.
自定义的PinnedHeaderViewListView,PinnedHeaderView的触摸事件尚未处理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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" | |
tools:context="${relativePackage}.${activityClass}" > | |
<com.example.pinnedheaderlistviewtest.PinnedHeaderListView | |
android:id="@android:id/list" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<color name="header_bg">#88EE2C2C</color> | |
<color name="header_static_bg">#88FF7F00</color> | |
<color name="header_pinned_bg">#88EEAEEE</color> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@color/header_bg" | |
android:orientation="vertical" > | |
<LinearLayout | |
android:id="@+id/header_static_container" | |
android:layout_width="match_parent" | |
android:layout_height="100dp" | |
android:background="@color/header_static_bg" | |
android:orientation="vertical" > | |
<TextView | |
android:id="@+id/header_static_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/header_static_text" | |
android:textAppearance="?android:attr/textAppearanceMedium" /> | |
</LinearLayout> | |
<include layout="@layout/pinned_header" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.pinnedheaderlistviewtest; | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.ArrayAdapter; | |
import com.example.pinnedheaderlistviewtest.PinnedHeaderListView.PinnedHeaderCallback; | |
public class MainActivity extends Activity { | |
private PinnedHeaderListView mList = null; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
findViews(); | |
initListView(); | |
} | |
private void findViews() { | |
mList = (PinnedHeaderListView) findViewById(android.R.id.list); | |
} | |
private void initListView() { | |
// 设置适配器 | |
List<String> dataList = new ArrayList<String>(); | |
for (int i = 0; i < 100; i++) { | |
dataList.add("挖屎 No." + i); | |
} | |
ArrayAdapter<String> adatper = new ArrayAdapter<String>( | |
getApplicationContext(), android.R.layout.simple_list_item_1, | |
android.R.id.text1, dataList); | |
mList.setAdapter(adatper); | |
mList.setPinnedHeaderCallback(new PinnedHeaderCallback() { | |
@Override | |
public View getPinnedHeader() { | |
View view = LayoutInflater.from(getApplicationContext()) | |
.inflate(R.layout.pinned_header, mList, false); | |
return view; | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/header_fixed_container" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@color/header_pinned_bg" | |
android:orientation="vertical" > | |
<TextView | |
android:id="@+id/header_fixed_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/header_fixed_text" | |
android:textAppearance="?android:attr/textAppearanceMedium" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.pinnedheaderlistviewtest; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.AbsListView; | |
import android.widget.AbsListView.OnScrollListener; | |
import android.widget.ListView; | |
public class PinnedHeaderListView extends ListView implements OnScrollListener { | |
public interface PinnedHeaderCallback { | |
public View getPinnedHeader(); | |
} | |
private static final String TAG = PinnedHeaderListView.class | |
.getSimpleName(); | |
private View mHeaderView = null; | |
private View mHeaderStaticContent = null; | |
private View mPinnedHeader = null; | |
private OnScrollListener mOnScrollListener = null; | |
private PinnedHeaderCallback mPinnedHeaderCallback = null; | |
public PinnedHeaderListView(Context context) { | |
this(context, null); | |
} | |
public PinnedHeaderListView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
if (mPinnedHeader != null) { | |
measureChild(mPinnedHeader, widthMeasureSpec, heightMeasureSpec); | |
} | |
Log.d(TAG, "onMeasure()"); | |
} | |
@Override | |
protected void onLayout(boolean changed, int l, int t, int r, int b) { | |
super.onLayout(changed, l, t, r, b); | |
Log.d(TAG, "onLayout()"); | |
} | |
@Override | |
protected void dispatchDraw(Canvas canvas) { | |
super.dispatchDraw(canvas); | |
if (mPinnedHeader != null) { | |
drawChild(canvas, mPinnedHeader, getDrawingTime()); | |
} | |
Log.d(TAG, "dispatchDraw()"); | |
} | |
@Override | |
public void setOnScrollListener(OnScrollListener l) { | |
if (l == this) { | |
super.setOnScrollListener(l); | |
} else { | |
mOnScrollListener = l; | |
} | |
} | |
@Override | |
public void onScroll(AbsListView view, int firstVisibleItem, | |
int visibleItemCount, int totalItemCount) { | |
refreshPinnedHeader(); | |
if (mOnScrollListener != null) { | |
mOnScrollListener.onScroll(view, firstVisibleItem, | |
visibleItemCount, totalItemCount); | |
} | |
} | |
@Override | |
public void onScrollStateChanged(AbsListView view, int scrollState) { | |
if (mOnScrollListener != null) { | |
mOnScrollListener.onScrollStateChanged(view, scrollState); | |
} | |
} | |
public void setPinnedHeaderCallback(PinnedHeaderCallback callback) { | |
mPinnedHeaderCallback = callback; | |
if (callback == null) { | |
mPinnedHeader = null; | |
} else { | |
mPinnedHeader = mPinnedHeaderCallback.getPinnedHeader(); | |
requestLayout(); | |
postInvalidate(); | |
} | |
} | |
private void init() { | |
addHeaderView(); | |
setOnScrollListener(this); | |
} | |
private void addHeaderView() { | |
mHeaderView = LayoutInflater.from(getContext()).inflate( | |
R.layout.header, this, false); | |
mHeaderStaticContent = mHeaderView | |
.findViewById(R.id.header_static_container); | |
addHeaderView(mHeaderView); | |
} | |
private void refreshPinnedHeader() { | |
if (mPinnedHeader == null) { | |
return; | |
} | |
// 静止区域 | |
int scrollYOfStaticHeader = Math.abs(mHeaderView.getTop()); | |
Log.d(TAG, "scrollYOfStaticHeader:" + scrollYOfStaticHeader); | |
// 静止区域原本应该滚到屏幕上部之外 | |
if (scrollYOfStaticHeader > 0 | |
&& scrollYOfStaticHeader < mHeaderStaticContent | |
.getMeasuredHeight()) { | |
mHeaderStaticContent.scrollTo(0, -scrollYOfStaticHeader); | |
} | |
// 固定头 | |
int distanceToTopOfPinnedHeader = mHeaderView.getBottom() | |
- mPinnedHeader.getMeasuredHeight(); | |
if (distanceToTopOfPinnedHeader <= 0) { | |
mPinnedHeader.layout(0, 0, mPinnedHeader.getMeasuredWidth(), | |
mPinnedHeader.getMeasuredHeight()); | |
} else { | |
mPinnedHeader.layout(0, 0, 0, 0); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string name="app_name">PinnedHeaderListViewTest</string> | |
<string name="header_fixed_text">Fixed Header!</string> | |
<string name="header_static_text">Static Header!</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment