拥有ScrollBar组件中不能在嵌套ScrollBar组件,譬如ScrollView组件里面不能嵌套ListView或者是GridView,这个在源码里面就有这样的解释,因为这样是不科学的,存在两个可以滑动的组件的时候,将会出现滑动混淆的情况,譬如我想滑动里面的ListView的时候,那么现在外面的GridView是否选择滑动呢,系统不好判断,所以在Androdi中是不能嵌套两个ScrollBar。
混淆下拉的事件导致的后果将是只显示一两行的数据,解决方法是,先让子控件的内容全部显示出来,然后禁用子控件的滑动,如果超过了父控件的范围,则显示父控件ScrollBar滚动显示内容,自定义GridView代码:
public class MyGridView extends GridView
{
public MyGridView(android.content.Context context,
android.util.AttributeSet attrs)
{
super(context, attrs);
}
/**
* 设置不滚动
*/
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
自定义的GridView有一个回调方法onMeasure(),主要是用来绘制View显示的宽度与高度,MeasureSpec.makMeasureSpec()方法的第一个参数是决定布局空间的大小,第二个参数是布局模式
MeasureSpec.AT_MOST 的意思是子控件需要多大的空间,就扩展到多大的空间,之后在ScrollView中
添加这个组件就行了,同样有ScrollBar控件的视图控件都适用 ListView等等。
ListView分页加载
image_list.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(expendable && firstVisibleItem + visibleItemCount == totalItemCount){
expendable = false;
anim.start();
new Thread(new Runnable() {
public void run() {
JSONObject result = HttpManager.getInstance().excuteAction(
ClassHomeActivity.this,
30450,
new Pair("ClassId", classId),
new Pair("PageSize", pageSize),
new Pair("PageIndex", pageIndex++),
new Pair("AlbumId", 0),
new Pair("RefreshTime", time),
new Pair("RefreshType", 2));
if(result != null){
final JSONArray items = result.optJSONArray("List");
if(items.length() != 0){
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
for(int i = 0; i < items.length(); i++){
list.put(items.optJSONObject(i));
}
smallAdapter.notifyDataSetChanged();
bigAdapter.notifyDataSetChanged();
if(items.length() == pageSize){
expendable = true;
}else{
photo_loading.setVisibility(View.GONE);
}
}
});
}
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
anim.stop();
}
});
}
}).start();
}
}
});
listView.setOnScrollListener()事件有两个必须实现的接口,源码中对两个接口的解释:
public interface OnScrollListener {
/**
* The view is not scrolling. Note navigating the list using the trackball counts as
* being in the idle state since these transitions are not animated.
*/
public static int SCROLL_STATE_IDLE = 0;
/**
* The user is scrolling using touch, and their finger is still on the screen
*/
public static int SCROLL_STATE_TOUCH_SCROLL = 1;
/**
* The user had previously been scrolling using touch and had performed a fling. The
* animation is now coasting to a stop
*/
public static int SCROLL_STATE_FLING = 2;
/**
* Callback method to be invoked while the list view or grid view is being scrolled. If the
* view is being scrolled, this method will be called before the next frame of the scroll is
* rendered. In particular, it will be called before any calls to
* {@link Adapter#getView(int, View, ViewGroup)}.
*
* @param view The view whose scroll state is being reported
*
* @param scrollState The current scroll state. One of {@link #SCROLL_STATE_IDLE},
* {@link #SCROLL_STATE_TOUCH_SCROLL} or {@link #SCROLL_STATE_IDLE}.
*/
public void onScrollStateChanged(AbsListView view, int scrollState);
/**
* Callback method to be invoked when the list or grid has been scrolled. This will be
* called after the scroll has completed
* @param view The view whose scroll state is being reported
* @param firstVisibleItem the index of the first visible cell (ignore if
* visibleItemCount == 0)
* @param visibleItemCount the number of visible cells
* @param totalItemCount the number of items in the list adaptor
*/
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount);
}
onScrollStateChanged(),onScroll()两个方法,第一个方法的参数为回调方法被调用时,列表视图或网格视图中滚动。如果滚动视图,这个方法会被调用前的下一帧的滚动渲染。特别是,它会被调用之前调用
第二个参数是滚动的状态,0==不滚动的状态,1==用户此前一直滚动使用触摸,并进行了一击。动画现在滑行停止
2==用户此前一直滚动使用触摸,并进行了一击。动画现在滑行停止
其实上面的都是用中英文翻译翻译出来的,当屏幕停止滚动时==0,当屏幕滚动的时候,用户的手指还在屏幕上的时候==1,当用户操作,由于惯性还在滚动的时候==2
onScroll()方法的参数firstVisibableItem表示在现时屏幕第一个ListItem(部分显示的ListItem也算)整个listView
的位置,下标从0开始
VisiableItemCount表示表示在现时屏幕可以见到(部分能够见到的Item也算)总数
totalItem表示总共的ListView中Item项目的总和
判断ListView是否已经滑动到了底部的最好方法是totalItem=firstVisiableItem+visiableItemCount,此时的ListView是滑动到了底部的,但是并不是每一次滑动到了底部都会加载新的动画,expendable变量的作用是用来防止ListView往上滑动的时候也出现正在加载的动画,因为也会出发onScroll方法,所以用一个标志性的变量来保存是否加载底部动画显示正在加载的footer View。加载完毕之后刷新ListView的数据,然后设置动画隐藏