先上效果图,未做美化的效果,不是很清晰的话,各位请见谅,这是在CSDN的第二篇博客,排版可能不是很好。
以下就是主要代码了,注释都比较详细,有不是很清楚或错误的地方大家一起交流下,am_faith@163.com
要实现这个效果主要要了解android的事件处理,如果有过相关经验的人看起来应该不是很难,表中的数据都是随机数据。
package com.am.fixtableeffect;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;
/*
* 该容器控件将onTouch事件进行拦截, *
* 了解事件处理的会比较清楚该控件的作用,网上也有不少资料介绍这个
* */
public class TestInterceptScrollContainer extends LinearLayout {
public TestInterceptScrollContainer(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public TestInterceptScrollContainer(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
//onInterceptTouchEvent是用于拦截手势事件的,每个手势事件都会先调用onInterceptTouchEvent,在
//ViewGroup里面定义,目的是在系统向该ViewGroup及其各个childView触发onTouchEvent()之前对相
//关事件进行一次拦截,想了解更多的可以去看看。
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
return true;//返回ture,以后每次事件分发下来,将不会传递给子view了,将由父view处理
}
}
接着是自定义HorizontalScrollView
package com.am.fixtableeffect;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;
/*
* 继承HorizontalScrollView
* 重载了 onScrollChanged(滚动条变化),监听每次的变化通知给 观察(此变化的)观察者
* 可使用 AddOnScrollChangedListener 来订阅本控件的 滚动条变化
* */
public class TestHScrollView extends HorizontalScrollView {
ScrollViewObserver mScrollViewObserver = new ScrollViewObserver();
public TestHScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public TestHScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public TestHScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(ev);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
/*
* 当滚动条移动后,引发 滚动事件。通知给观察者,观察者会传达给其他的。
*/
if (mScrollViewObserver != null ) {
mScrollViewObserver.NotifyOnScrollChanged(l, t, oldl, oldt);
}
super.onScrollChanged(l, t, oldl, oldt);
}
/*
* 监听该自定义 滚动条变化事件
* */
public void AddOnScrollChangedListener(OnScrollChangedListener listener) {
mScrollViewObserver.AddOnScrollChangedListener(listener);
}
/*
* 取消监听自定义 滚动条变化事件
* */
public void RemoveOnScrollChangedListener(OnScrollChangedListener listener) {
mScrollViewObserver.RemoveOnScrollChangedListener(listener);
}
/*
* 滚动事件监听
*/
public static interface OnScrollChangedListener {
public void onScrollChanged(int l, int t, int oldl, int oldt);
}
public static class ScrollViewObserver {
List<OnScrollChangedListener> mList;
public ScrollViewObserver() {
super();
mList = new ArrayList<OnScrollChangedListener>();
}
public void AddOnScrollChangedListener(OnScrollChangedListener listener) {
mList.add(listener);
}
public void RemoveOnScrollChangedListener(
OnScrollChangedListener listener) {
mList.remove(listener);
}
public void NotifyOnScrollChanged(int l, int t, int oldl, int oldt) {
if (mList == null || mList.size() == 0) {
return;
}
for (int i = 0; i < mList.size(); i++) {
if (mList.get(i) != null) {
mList.get(i).onScrollChanged(l, t, oldl, oldt);
}
}
}
}
}
下面是基于自定义布局和视图的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<TextView
android:id="@+id/id"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="@string/id"
android:textAppearance="?android:attr/textAppearanceMedium" />
<com.am.fixtableeffect.TestInterceptScrollContainer
android:id="@+id/scroollContainter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/id"
android:focusable="false" >
<com.am.fixtableeffect.TestHScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:scrollbars="none" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:orientation="horizontal" >
<TextView
android:id="@+id/tablebody1"
style="@style/app_login_item_style"
android:layout_width="60dp"
android:gravity="center"
android:layout_gravity="center"
android:text ="@string/tablebody1" />
<TextView
android:id="@+id/tablebody2"
style="@style/app_login_item_style"
android:layout_width="60dp"
android:gravity="center"
android:layout_gravity="center"
android:text="@string/tablebody2"/>
<TextView
android:id="@+id/tablebody3"
style="@style/app_login_item_style"
android:layout_width="60dp"
android:gravity="center"
android:layout_gravity="center"
android:text ="@string/tablebody3" />
<TextView
android:id="@+id/tablebody4"
style="@style/app_login_item_style"
android:layout_width="60dp"
android:gravity="center"
android:layout_gravity="center"
android:text ="@string/tablebody4" />
<TextView
android:id="@+id/tablebody5"
style="@style/app_login_item_style"
android:layout_width="60dp"
android:gravity="center"
android:layout_gravity="center"
android:text ="@string/tablebody5" />
</LinearLayout>
</com.am.fixtableeffect.TestHScrollView>
</com.am.fixtableeffect.TestInterceptScrollContainer>
</RelativeLayout>
主布局文件
<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=".TestActivity" >
<!-- listview_head 为如上的布局文件 -->
<include
android:id="@+id/listview_head"
layout="@layout/item_test" />
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/listview_head"
android:cacheColorHint="@android:color/transparent"
android:divider="@null" >
</ListView>
</RelativeLayout>
好了,如上就是这些,后续会有一些蓝牙与下位机的一些经验分享给大家,如有不正确的地方请大家指教,有相关问题可以联系我am_faith@163.com