Hi there,
just a trick that I find on StackOverflow and adapt (to my case) for ListView instead of ScrollView.
It could be nice to know if you are scrolling up and down over a list to be notified and then do whatever you want.
Here I am gonna explain how to this easily..
There is a method in ListView
ListView
that reports the shifting of scrolls. It is called onScrollChanged(). However, it is declared protected, so you must create a wrapper class to gain access. For the usage of the method, please check android docs.
First, create an interface to expose the protected method
- public interface OnScrollListener {
- void onScrollUpAndDownChanged(int x, int y, int oldx, int oldy);
- }
Then, create your wrapper and extend ListView.
- public class MyCustomListView extends ListView {
- private OnScrollListener onScrollListener = null;
- public MyCustomListView(Context context) {
- super(context);
- }
- public MyCustomListView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- public MyCustomListView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public void setOnScrollUpAndDownListener(OnScrollListener onScrollListener) {
- this.onScrollListener = onScrollListener;
- }
- @Override
- protected void onScrollChanged(int x, int y, int oldx, int oldy) {
- super.onScrollChanged(x, y, oldx, oldy);
- if (onScrollListener != null) {
- onScrollListener.onScrollUpAndDownChanged(x, y, oldx, oldy);
- }
- }
- }
Finally, include it in your XML layout like a custom view, and attach listeners like usual.
- <your mycustomlistview />
- myinstance.setOnScrollUpAndDownListener(new OnScrollListener() {...});
Hope this help you to enhance android possibilities like always :)
Cheers
My references:
没有评论:
发表评论