我知道iPhone中可以使用类似pullToRefresh的功能,对于Android我们必须手动管理它。
我有一些示例有pullToRefresh,但它仅适用于ListView。
在我的情况下,我想为Scrollview实现。像Google Play中的DrawFree应用程序中提供的PullToRefresh。
请参见下图:
那么,如何实现呢?
任何实施它的例子都会非常感激。
提前致谢。
答案 0 :(得分:12)
这是在ListView,GridView,WebView,Expandable ListView中实现pull to refresh的一个很好的例子。
您可以使用此示例并根据您的观点进行更改。
答案 1 :(得分:4)
[编辑:我的解决方案与Gmail类似,如果不是您想要的,我很抱歉,无论如何我发布的代码可能对其他人有用]
我刚刚完成它,我已经在Joe Dailey编写的example之后的ListView中实现了它(非常好且简单)。然后我重新访问它以与ScrollView一起使用。
这就是我所做的:
我将onTouchListener设置为ScrollView;
我控制滚动视图是否位于顶部(scrollView.getScrollY()== 0) 然后,我使用“lastY = startY”来知道我是否向下滚动(两个变量都是活动字段)。
“act.refresh()”是运行任务以从我的服务器获取数据的方法。
在你的asynkTask的onPostExecute方法中,或者在使用hanlders的Handler中,你调用“finishRefresh()”方法;
这是onTouchListener:
class RefreshTouchListener implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
ScrollView scroll = (ScrollView) v;
if (scroll.getScrollY() == 0) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startY = event.getY();
lastY = startY;
break;
case MotionEvent.ACTION_MOVE:
if (!refreshing && event.getY() > lastY) {
lastY = event.getY();
if (event.getY() - startY <= dragLength) {
double percent = 1 - (event.getY() - startY) / dragLength;
double weight;
weight = 2 * Math.pow(percent, 0.8);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) progress.getLayoutParams();
params.weight = (float) weight;
progress.setLayoutParams(params);
progress.setIndeterminate(false);
progress.setPadding(0, 0, 0, 0);
return true;
} else {
refreshing = true;
act.refresh();
startY = 100000f;
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) progress.getLayoutParams();
params.weight = 0;
progress.setIndeterminate(true);
progress.postInvalidate();
progress.setLayoutParams(params);
}
}
case MotionEvent.ACTION_UP:
startY = 100000f;
Log.i(TAG, "action up " + event.getY());
if (!refreshing) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) progress.getLayoutParams();
params.weight = 2;
progress.setLayoutParams(params);
}
}
}
return false;
}
}
这是finishRefresh()方法:
public void finishRefresh() {
progress.setIndeterminate(false);
progress.postInvalidate();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) progress.getLayoutParams();
params.weight = 2;
progress.setLayoutParams(params);
refreshing = false;
}
这是生成布局的代码,包含progressBar和其他所有内容:
private void createProgressBarLayout() {
topMargin = -Math.round(6 * act.metrics.density);
dragLength = Math.round(act.screen_size.y / 2.5f);
LinearLayout top = new LinearLayout(this);
top.setGravity(Gravity.TOP);
top.setOrientation(LinearLayout.HORIZONTAL);
content_rel_layout = (RelativeLayout) findViewById(R.id.rel_layout_name);
content_rel_layout.addView(top);
ViewGroup.LayoutParams topParams = top.getLayoutParams();
topParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
topParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
top.setLayoutParams(topParams);
FrameLayout left = new FrameLayout(this);
progress = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
progress.setProgress(100);
progress.setIndeterminate(false);
// progress.setBackgroundResource(R.drawable.progress_bar);
FrameLayout right = new FrameLayout(this);
top.addView(left);
top.addView(progress);
top.addView(right);
LinearLayout.LayoutParams leftParams = (LinearLayout.LayoutParams) left.getLayoutParams();
leftParams.weight = 1;
leftParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
leftParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
leftParams.topMargin = topMargin;
left.setLayoutParams(leftParams);
LinearLayout.LayoutParams progressParams = (LinearLayout.LayoutParams) progress.getLayoutParams();
progressParams.weight = 2;
progressParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
progressParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
progressParams.topMargin = topMargin;
progress.setLayoutParams(progressParams);
LinearLayout.LayoutParams rightParams = (LinearLayout.LayoutParams) right.getLayoutParams();
rightParams.weight = 1;
rightParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
rightParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
rightParams.topMargin = topMargin;
right.setLayoutParams(rightParams);
ScrollView sv = (ScrollView) findViewById(R.id.prof_mon4_vert_scroll);
sv.setOnTouchListener(new RefreshTouchListener());
}
请随时问我一切疑问! 享受