我知道在iPhone中可以使用类似pullToRefresh的功能,而对于Android,我们必须手动对其进行管理。
我有一个具有pullToRefresh的示例,但它仅适用于ListView。
就我而言,我想为Scrollview实现。 Google Play的DrawFree应用中提供了PullToRefresh之类的功能。
请参见下图:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9VVlZmQi5wbmcifQ==” alt =“在此处输入图像描述”>
那么,如何实现呢?
这是在ListView,GridView,WebView,Expandable ListView中实现拉动刷新的绝佳示例。
您可以使用此示例,并根据您的视图进行更改。
[[编辑:我的解决方案类似于Gmail,很抱歉,如果它不是您想要的,反正我发布了可能对其他人有用的代码]
我刚刚完成,已经按照Joe Dailey编写的example在ListView中实现了(非常好,很简单)。然后,我再次将其与ScrollView一起使用。
这是我所做的:
我将onTouchListener设置为ScrollView;
我控制scrollView是否在顶部(scrollView.getScrollY()== 0)然后,我使用“ lastY = startY”来知道是向下滚动还是向上滚动(变量都是活动字段)。
“ act.refresh()”是运行任务以从服务器获取数据的方法。
在asynkTask的onPostExecute方法中,或者在处理程序中,如果您使用处理程序,则调用“ 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;
}
这是生成带有进度条和其他所有内容的布局的代码:
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());
}
请随时向我提出任何疑问!享受