listview 有个滑动监听 OnScrollListener ,这个监听有两个方法,
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(AbsListView view, int scrollState)方法,SCROLL_STATE_IDLE 意思是静止, SCROLL_STATE_TOUCH_SCROLL 是触摸滑动,SCROLL_STATE_FLING 是滑动。
通过对 scrollState 判断,可以实现一些简单的操作。要做一个功能,listview使劲上滑了一下,分段加载,最后一条也加载出来了,这时候可以在 SCROLL_STATE_IDLE状态下判断 int position = listview.getLastVisiblePosition();判断是否是集合中最后一个对象,如果不是,就继续分批加载。
onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) 方法是在listview滑动时调用的,firstVisibleItem是第一个显示的item的position,很有用。比如想要根据item滑动的位置和对象所在集合的索引位置监听一些别的控件的显示,可以用此方法。
一个典型的功能是listview上面悬浮一个textview控件,显示一些文字信息,要根据listview滑动到的位置,动态显示一些特定的内容。
if (firstVisibleItem >= (appinfos.size() /2)) {
tv.setText("系统程序:" + appinfos.size() /2 + "个");
} else {
tv.setText("用户程序:" + (appinfos.size() /2-appinfos.size() /2) + "个");
}
listview 多条目显示需要在ada原先四个方法的基础上再多重写两个方法
private static final int MESSAGE_TYPE_RECV_TXT = 0;
private static final int MESSAGE_TYPE_SENT_TXT = 1;
public int getViewTypeCount() {
return 12;
}
/**
* 获取item类型
*/
public int getItemViewType(int position) {
EMMessage message = getItem(position);
if (message == null) {
return -1;
}
if (message.getType() == EMMessage.Type.TXT) {
return message.direct == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_TXT : MESSAGE_TYPE_SENT_TXT;
}
return -1;// invalid
}
getViewTypeCount()返回的item的类型的个数,getItemViewType(int position)返回的是代表的item的具体类型,需要说明的一点,代表类型的数字一定要从0开始排起,依次为0 ,1,2,3等等
在getView()方法里面根据message.getType()来确定用inflater.inflate(R.layout.hx_row_received_message, null)不同的layout。比较典型的用法就是聊天列表界面,包括自己的和对方的聊天信息,聊天信息又有 文字 语音 图片 视频 等等 ,有兴趣的可以搜一下
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(AbsListView view, int scrollState)方法,SCROLL_STATE_IDLE 意思是静止, SCROLL_STATE_TOUCH_SCROLL 是触摸滑动,SCROLL_STATE_FLING 是滑动。
通过对 scrollState 判断,可以实现一些简单的操作。要做一个功能,listview使劲上滑了一下,分段加载,最后一条也加载出来了,这时候可以在 SCROLL_STATE_IDLE状态下判断 int position = listview.getLastVisiblePosition();判断是否是集合中最后一个对象,如果不是,就继续分批加载。
onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) 方法是在listview滑动时调用的,firstVisibleItem是第一个显示的item的position,很有用。比如想要根据item滑动的位置和对象所在集合的索引位置监听一些别的控件的显示,可以用此方法。
一个典型的功能是listview上面悬浮一个textview控件,显示一些文字信息,要根据listview滑动到的位置,动态显示一些特定的内容。
if (firstVisibleItem >= (appinfos.size() /2)) {
tv.setText("系统程序:" + appinfos.size() /2 + "个");
} else {
tv.setText("用户程序:" + (appinfos.size() /2-appinfos.size() /2) + "个");
}
listview 多条目显示需要在ada原先四个方法的基础上再多重写两个方法
private static final int MESSAGE_TYPE_RECV_TXT = 0;
private static final int MESSAGE_TYPE_SENT_TXT = 1;
public int getViewTypeCount() {
return 12;
}
/**
* 获取item类型
*/
public int getItemViewType(int position) {
EMMessage message = getItem(position);
if (message == null) {
return -1;
}
if (message.getType() == EMMessage.Type.TXT) {
return message.direct == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_TXT : MESSAGE_TYPE_SENT_TXT;
}
return -1;// invalid
}
getViewTypeCount()返回的item的类型的个数,getItemViewType(int position)返回的是代表的item的具体类型,需要说明的一点,代表类型的数字一定要从0开始排起,依次为0 ,1,2,3等等
在getView()方法里面根据message.getType()来确定用inflater.inflate(R.layout.hx_row_received_message, null)不同的layout。比较典型的用法就是聊天列表界面,包括自己的和对方的聊天信息,聊天信息又有 文字 语音 图片 视频 等等 ,有兴趣的可以搜一下