主要是对OnScrllListener的使用介绍,有两个方法来监听滑动的状态,onScroll()和onScrollStateChanged()。
new AbsListView.OnScrollListener() { /** *正在滚动时回调,回调2-3次,手指没抛则回调2次。scrollState = 2的这次不回调 *回调顺序如下 *第1次:scrollState = SCROLL_STATE_TOUCH_SCROLL(1) 正在滚动 *第2次:scrollState = SCROLL_STATE_FLING(2) 手指做了抛的动作(手指离开屏幕前,用力滑了一下) *第3次:scrollState = SCROLL_STATE_IDLE(0) 停止滚动 */ @Override public void onScrollStateChanged(AbsListView view, int scrollState) { //当停止滚动时 if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { //do something } } /** * 滚动时一直回调,直到停止滚动时才停止回调。单击时回调一次。 *firstVisibleItem:当前能看见的第一个列表项ID(从0开始) *visibleItemCount:当前能看见的列表项个数(小半个也算) *totalItemCount:列表项共数 */ @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //否滚到最后一行 if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) { //do something } } };下面用Listview的加载更多实例来说明。
重写的Listview,实现了滑动监听。
public class LoaderListView extends ListView implements AbsListView.OnScrollListener { private Context mContext; private boolean isCanScroll = false; private List list = new ArrayList(); /** * 最后可见条目的索引 */ private int lastVisibleIndex = 0; private View footView; private MainActivity.CurAdapter adapter; private Handler loaderHander = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); loaderMore(); } }; public LoaderListView(Context context) { super(context); this.mContext = context; init(); } public LoaderListView(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; init(); } public LoaderListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.mContext = context; init(); } private void init() { footView = LayoutInflater.from(mContext).inflate(R.layout.item_foot, null); addFooterView(footView); setOnScrollListener(this); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (isCanScroll && OnScrollListener.SCROLL_STATE_IDLE == scrollState && lastVisibleIndex < list.size()) { loaderHander.sendEmptyMessageDelayed(0, 2000); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { lastVisibleIndex = firstVisibleItem + visibleItemCount - 1; if (firstVisibleItem + 15 < totalItemCount) { isCanScroll = false; } else { if (lastVisibleIndex == list.size()) { removeFooterView(footView); } else { isCanScroll = true; } } } /** * 加载更多 */ private void loaderMore() { if (lastVisibleIndex + 10 < list.size()) { lastVisibleIndex += 10; } else { lastVisibleIndex = list.size(); } adapter.setCountLength(lastVisibleIndex); } public List getList() { return list; } public void setList(List list) { this.list = list; } public MainActivity.CurAdapter getCurAdapter() { return adapter; } public void setCurAdapter(MainActivity.CurAdapter adapter) { this.adapter = adapter; } }活动中,主要是传入集合和适配器。
public class MainActivity extends Activity { private MainActivity mContext; private int lastPress = 0; private boolean delState = false; private List<String> curList = new ArrayList<>(); private LoaderListView curListView; private CurAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { mContext = this; for (int i = 0; i < 100; i++) { curList.add("第" + i + ":行数据"); } curListView = (LoaderListView) findViewById(R.id.lv_contents); adapter = new CurAdapter(); curListView.setAdapter(adapter); curListView.setList(curList); curListView.setCurAdapter(adapter); } public class CurAdapter extends BaseAdapter { private int count = 0; public CurAdapter() { if (0 == count) { if (15 > curList.size()) { count = curList.size(); } else { count = 15; } } } public void setCountLength(int count) { this.count = count; notifyDataSetChanged(); } @Override public int getCount() { return count; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (null == convertView) { convertView = LayoutInflater.from(mContext).inflate(R.layout.item_content, null); } ((TextView) convertView.findViewById(R.id.tv_content)).setText(curList.get(position)); return convertView; } } }活动布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <cn.com.myapplication.LoaderListView android:id="@+id/lv_contents" android:layout_width="match_parent" android:layout_height="match_parent"></cn.com.myapplication.LoaderListView> </RelativeLayout>子项布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#FFFFFF" android:layout_height="40dp"> <RelativeLayout android:id="@+id/rela_content" android:layout_width="match_parent" android:visibility="visible" android:layout_height="40dp"> <TextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="match_parent" android:text="content" android:textColor="#001122" android:gravity="center" /> </RelativeLayout> </RelativeLayout>
在适配器中,对子项数量的处理,是针对那种提前就把数据都取完了的情况:
public CurAdapter() { if (0 == count) { if (15 > curList.size()) { count = curList.size(); } else { count = 15; } } } public void setCountLength(int count) { this.count = count; notifyDataSetChanged(); }如果加载更多的数据需要去网络上请求的话,那么最好是在loaderMore()中,开启去请求,然后发送handler刷新数据。
/** * 加载更多 */ private void loaderMore() { if (lastVisibleIndex + 10 < list.size()) { lastVisibleIndex += 10; } else { lastVisibleIndex = list.size(); } adapter.setCountLength(lastVisibleIndex); }