<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context="com.hobby.contactsdemo.MainActivity">
<ListView
android:id="@+id/activity_main_lv_contacts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="none"
android:divider="#DDDDDD"
android:dividerHeight="1dp"
></ListView>
<LinearLayout
android:id="@+id/activity_main_lv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3355ff"
android:orientation="horizontal">
<TextView
android:id="@+id/activity_main_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#ffffff"
android:padding="8dp"
android:text="A"/>
</LinearLayout>
</RelativeLayout>
/**
* 当前界面第一项对应的分组section
*/
int section = getSectionForPosition(firstVisibleItem);
/**
* 下一个分组对应的首个position
*/
int nextSectionPosition = getPositionForSection(section + 1);
if (firstVisibleItem != lastFirstVisibleItem) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) lvTitle.getLayoutParams();
params.topMargin = 0;
lvTitle.setLayoutParams(params);
tvTitle.setText(String.valueOf(alphabet.charAt(section)));
}
if (nextSectionPosition == firstVisibleItem + 1) {
View childView = view.getChildAt(0);
if (childView != null) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) lvTitle.getLayoutParams();
int titleHeight = lvTitle.getHeight();
int bottom = childView.getBottom();
if (bottom < titleHeight) {
float disPush = bottom - titleHeight;
params.topMargin = (int) disPush;
lvTitle.setLayoutParams(params);
} else {
if (params.topMargin != 0) {
params.topMargin = 0;
lvTitle.setLayoutParams(params);
}
}
}
}
lastFirstVisibleItem = firstVisibleItem;
42.09 KB, 下载次数: 34
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |