PullToRefreshScrollView嵌套ListView时,下拉刷新ListView自动滑动到底部

PullToRefreshScrollView大家应该很熟悉,这个android的开源下拉刷新,上拉加载更多的开源框架。当PullToRefreshScrollView嵌套了ListView并且ListView上面还有其他控件时,由于我们处理了ListView数据无法完全显示的问题。解决如下:

public static void setListViewHeightBasedOnChildren(ListView listView) {
		BaseAdapter listAdapter = (BaseAdapter) listView.getAdapter();
		if (listAdapter == null) {
			return;
		}

		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		int divideHeight = (int) (0.5 * (listAdapter.getCount() - 1));
		params.height = totalHeight + divideHeight;
		listView.setLayoutParams(params);
	}

也就是计算ListView高度并设置它的高度。

我们在下拉刷新的时候,ListView会自动下滑导致ListView上面的控件无法正常显示。这时,我们只需要让上面的控件获得焦点即可:(在我的项目里面是PullToRefreshScrollView嵌套了ViewPage和ListView,所以只需要让ViewPage获取焦点即可)

vpBanner.requestFocus();
vpBanner.setFocusable(true);
vpBanner.setFocusableInTouchMode(true);





你可能感兴趣的:(PullToRefreshScrollView嵌套ListView时,下拉刷新ListView自动滑动到底部)