给listview添加标题并且随着手势的滑动标题可以悬停在屏幕顶部,效果图如下:
效果实现起来也不是很复杂,需要自定义listview,答题的思路就是自定义的listview顶部有个textview的title,适配器的顶部也有一个,当满足一定条件的时候标题变化,
/***
* 带标题的listview
*
* @author zhang
*
*/
public class TitledListView extends ListView {
private View mTitle;
TextView title_text;
public static boolean FLAG_VIEW;// 隐藏title标识,true:gone ; false:visibile(如果要不显示标题,改值一定要在刷新适配器值前调用)
public TitledListView(Context context) {
super(context);
}
public TitledListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TitledListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mTitle != null) {
measureChild(mTitle, widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (mTitle != null) {
mTitle.layout(0, 0, mTitle.getMeasuredWidth(), mTitle.getMeasuredHeight());
}
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (FLAG_VIEW) {
mTitle.setVisibility(View.GONE);
} else {
if (mTitle != null) {
drawChild(canvas, mTitle, getDrawingTime());
}
}
}
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
LayoutInflater inflater = LayoutInflater.from(getContext());
mTitle = inflater.inflate(R.layout.title, this, false);
title_text = (TextView) mTitle.findViewById(R.id.titled_text);
}
public void moveTitle(String title) {
View bottomChild = getChildAt(0);
if (bottomChild != null) {
int bottom = bottomChild.getBottom();
int height = mTitle.getMeasuredHeight();
int y = 0;
if (bottom < height) {
y = bottom - height;
}
if (title != null) {
title_text.setText(title);
}
mTitle.layout(0, y, mTitle.getMeasuredWidth(), mTitle.getMeasuredHeight() + y);
}
}
public void updateTitle(String title) {
if (title != null) {
title_text.setText(title);
}
mTitle.layout(0, 0, mTitle.getMeasuredWidth(), mTitle.getMeasuredHeight());
}
public String getTitle() {
return title_text.getText().toString();
}
}
主页面布局:
<LinearLayout
android:id="@+id/ll_group_hoster"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_white"
android:orientation="vertical" >
<com.jingxinlawyer.lawchat.widget.TitledListView
android:id="@+id/lv_group_hoster"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000" >
</com.jingxinlawyer.lawchat.widget.TitledListView>
</LinearLayout>
适配器布局:
<?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="match_parent"
android:background="@drawable/selector_rl_item"
android:descendantFocusability="beforeDescendants"
android:orientation="vertical" >
<TextView
android:id="@+id/catalog2"
android:layout_width="fill_parent"
android:layout_height="32dp"
android:background="@color/bg_gray"
android:gravity="center_vertical"
android:paddingLeft="@dimen/pdingLeftRight"
android:textColor="@color/text_black" />
<View style="@style/line" />
<RelativeLayout
android:id="@+id/rl_member"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:padding="10dp" >
<ImageView
android:id="@+id/ivGroupHeader2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:contentDescription="@string/description"
android:scaleType="fitXY" />
<TextView
android:id="@+id/tvGroupName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/ivGroupHeader2"
android:ellipsize="end"
android:maxLength="12"
android:text="name"
android:singleLine="true"
android:textColor="@color/text_black"
android:textSize="@dimen/text_name" />
<TextView
android:id="@+id/tv_kq_no2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_toRightOf="@id/tvGroupName2"
android:singleLine="true"
android:layout_toLeftOf="@+id/tvGroupTime2"
android:textColor="@color/text_gray"
android:textSize="@dimen/text_name" />
<LinearLayout
android:id="@+id/llGroupIntroduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/tvGroupName2"
android:layout_below="@id/tvGroupName2"
android:gravity="center" >
<LinearLayout
android:id="@+id/tvGroupSexAge2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:background="@drawable/shape_sex"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="3dp"
android:paddingRight="3dp" >
<ImageView
android:id="@+id/iv_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/description"
android:src="@drawable/qz_nan" />
<TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:singleLine="true"
android:textColor="@color/text_white"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:id="@+id/tvGroupHoster2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape_near_lawyer_item"
android:maxLength="12"
android:padding="2dp"
android:singleLine="true"
android:text="律师"
android:textColor="@color/text_pink"
android:textSize="12sp" />
<TextView
android:id="@+id/tvGroupWork2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:background="@drawable/shape_near_lawyer_item"
android:maxLength="12"
android:padding="2dp"
android:singleLine="true"
android:text="IT"
android:textColor="@color/text_pink"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/llUserIntroduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/ivGroupHeader2"
android:layout_alignLeft="@id/tvGroupName2"
android:layout_below="@id/tvGroupName2"
android:layout_marginRight="30dp"
android:gravity="center"
android:visibility="gone" >
<TextView
android:id="@+id/tvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textColor="@color/text_gray"
android:textSize="@dimen/annotion_text" />
</LinearLayout>
<TextView
android:id="@+id/tvGroupTime2"
style="@style/user_comment_text"
android:layout_alignBottom="@id/tvGroupName2"
android:layout_alignParentRight="true"
android:singleLine="true" />
<TextView
android:id="@+id/tvGroupManger2"
android:layout_width="35dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/tvGroupTime2"
android:layout_marginTop="2dp"
android:background="@drawable/selector_btn1"
android:gravity="center"
android:singleLine="true"
android:text="管理"
android:textColor="@color/text_white"
android:visibility="gone" />
<TextView
android:id="@+id/tvAddfriend"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/tvGroupTime2"
android:background="@drawable/selector_btn1"
android:gravity="center"
android:singleLine="true"
android:text="添加"
android:textColor="@color/text_white"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
主页面listview的滚动监听判断是否需要替换标题:
OnScrollListener onScroll = new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (listUser.size() == 1) {
((TitledListView) view).updateTitle(listUser.get(
firstVisibleItem).getStrTitle());
}
if (listUser != null && listUser.size() > 1) {
// 第一项与第二项标题不同,说明标题需要移动
if (!TextUtils
.equals(listUser
.get(firstVisibleItem)
.getStrTitle(), listUser.get(firstVisibleItem + 1)
.getStrTitle())) {
((TitledListView) view).moveTitle(listUser.get(
firstVisibleItem).getStrTitle());
} else {
((TitledListView) view).updateTitle(listUser.get(
firstVisibleItem).getStrTitle());
}
}
}
};
适配器判断标题的显示:
vh.tvTitle.setText(grouper.getStrTitle());
if (position == 0) {
vh.tvTitle.setVisibility(View.VISIBLE);
} else if (position < getCount()
&& !TextUtils.equals(listMember.get(position)
.getStrTitle(), listMember.get(position - 1)
.getStrTitle())) {
vh.tvTitle.setVisibility(View.VISIBLE);
} else {
vh.tvTitle.setVisibility(View.GONE);
}