public class HVListView extends ListView { private GestureDetector mGesture; public LinearLayout mListHead; private int mOffset = 0; private int screenWidth; public HVListView(Context context) { super(context); mGesture = new GestureDetector(context, mOnGesture); } public HVListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mGesture = new GestureDetector(context, mOnGesture); } public HVListView(Context context, AttributeSet attrs) { super(context, attrs); mGesture = new GestureDetector(context, mOnGesture); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { super.dispatchTouchEvent(ev); return mGesture.onTouchEvent(ev); } private OnGestureListener mOnGesture = new GestureDetector.SimpleOnGestureListener() { @Override public boolean onDown(MotionEvent e) { return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { synchronized (HVListView.this) { if (Math.abs(distanceX) > Math.abs(distanceY)) { int moveX = (int) distanceX; int curX = mListHead.getScrollX(); // int scrollWidth = getWidth(); int scrollWidth = (int)( 890 * getContext().getResources().getDisplayMetrics().density); int dx = moveX; if (curX + moveX < 0) dx = 0; if (curX + moveX + getScreenWidth() > scrollWidth) dx = scrollWidth - getScreenWidth() - curX; mOffset += dx; for (int i = 0, j = getChildCount(); i < j; i++) { View child = ((ViewGroup) getChildAt(i)).getChildAt(1); if (child.getScrollX() != mOffset) child.scrollTo(mOffset, 0); } mListHead.scrollBy(dx, 0); } } requestLayout(); return true; } }; public int getScreenWidth() { if (screenWidth == 0) { screenWidth = getContext().getResources().getDisplayMetrics().widthPixels; if (getChildAt(0) != null) { screenWidth -= ((ViewGroup) getChildAt(0)).getChildAt(0).getMeasuredWidth(); } else if (mListHead != null) { screenWidth -= mListHead.getChildAt(0).getMeasuredWidth(); } } return screenWidth; } public int getHeadScrollX() { return mListHead.getScrollX(); } public LinearLayout getListHead() { return mListHead; } public void setListHead(LinearLayout listHead) { this.mListHead = listHead; } }
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F6F6F6" android:orientation="vertical" android:paddingBottom="2dip" tools:context=".MainActivity" > <!-- 和分公司运营数据界面查不多 --> <include android:id="@+id/title_include_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/toptitle_include" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/city_hall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="20dip" android:text="所有城区的分公司" android:textColor="@color/black" android:textSize="20sp" /> <TextView android:id="@+id/currentTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="20dip" android:layout_marginTop="20dip" android:text="显示默认服务器时间" android:textColor="@color/black" android:textSize="14sp" /> <View android:id="@+id/view_one" android:layout_width="fill_parent" android:layout_height="1dip" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:background="#B4B6B7" /> <LinearLayout android:id="@+id/headLayout" android:layout_width="1050dip" android:layout_height="35dip" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:orientation="horizontal" > <!-- 这里第一列的宽度进行固定 --> <View android:layout_width="1dip" android:layout_height="fill_parent" android:background="#B4B6B7" /> <TextView style="@style/TextStyleBack" android:layout_width="158dp" android:layout_height="fill_parent" android:text="营业厅名称" /> <View android:layout_width="1dip" android:layout_height="fill_parent" android:background="#B4B6B7" /> <LinearLayout android:id="@+id/headView" android:layout_width="880dip" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView style="@style/TextStyleBack" android:layout_width="99dp" android:layout_height="fill_parent" android:text="取号人数" /> <View android:layout_width="1dip" android:layout_height="fill_parent" android:background="#B4B6B7" /> <TextView style="@style/TextStyleBack" android:layout_width="99dp" android:layout_height="fill_parent" android:text="服务人数" /> <View android:layout_width="1dip" android:layout_height="fill_parent" android:background="#B4B6B7" /> <TextView style="@style/TextStyleBack" android:layout_width="99dp" android:layout_height="fill_parent" android:text="弃号人数" /> <View android:layout_width="1dip" android:layout_height="fill_parent" android:background="#B4B6B7" /> <TextView style="@style/TextStyleBack" android:layout_width="99dp" android:layout_height="fill_parent" android:text="等待人数" /> <View android:layout_width="1dip" android:layout_height="fill_parent" android:background="#B4B6B7" /> <TextView style="@style/TextStyleBack" android:layout_width="119dp" android:layout_height="fill_parent" android:text="平均服务时间" /> <View android:layout_width="1dip" android:layout_height="fill_parent" android:background="#B4B6B7" /> <TextView style="@style/TextStyleBack" android:layout_width="119dp" android:layout_height="fill_parent" android:text="平均等待时间" /> <View android:layout_width="1dip" android:layout_height="fill_parent" android:background="#B4B6B7" /> <TextView style="@style/TextStyleBack" android:layout_width="119dp" android:layout_height="fill_parent" android:text="台席开放率" /> <View android:layout_width="1dip" android:layout_height="fill_parent" android:background="#B4B6B7" /> <TextView style="@style/TextStyleBack" android:layout_width="119dp" android:layout_height="fill_parent" android:text="台席开放数" /> <View android:layout_width="1dip" android:layout_height="fill_parent" android:background="#B4B6B7" /> </LinearLayout> </LinearLayout> <View android:id="@+id/view_two" android:layout_width="fill_parent" android:layout_height="1dip" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:background="#B4B6B7" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.ibm.moa.servicemanager.view.HVListView android:id="@+id/hallListView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:layout_weight="1" android:cacheColorHint="@color/transparent" android:divider="#B4B6B7" android:dividerHeight="1dip" android:fadingEdge="none" /> <!-- android:scrollbars="none" --> <!-- <View android:id="@+id/bottom_line" android:background="#B4B6B7" android:layout_width="fill_parent" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:layout_height="1dip" android:visibility="visible" /> --> <include android:id="@+id/loadMore" android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/loadmore_footer_view" android:visibility="gone" /> </LinearLayout> </LinearLayout> <include android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/tishi" /> </FrameLayout>
View child = ((ViewGroup) convertView).getChildAt(1);
int headScrollX = hallListView.getHeadScrollX();
if (child.getScrollX() != headScrollX) {
child.scrollTo(hallListView.getHeadScrollX(), 0);
}