安卓中級教程(5):ScrollView與refreshable之間的設置
設置向下拉動更新。
package com.mycompany.Scroll_test; import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*; /**
* 刷新控制view
*
* @author Nono
*
*/
public class RefreshableView extends LinearLayout { private static final String TAG = "LILITH";
private Scroller scroller;
private View refreshView;
private ImageView refreshIndicatorView;
private int refreshTargetTop = -60;
private ProgressBar bar;
private TextView downTextView;
private TextView timeTextView; private RefreshListener refreshListener; private String downTextString;
private String releaseTextString; private Long refreshTime = null;
private int lastX;
private int lastY;
// 拉動標記
private boolean isDragging = false;
// 是否可刷新標記
private boolean isRefreshEnabled = true;
// 在刷新中標記
private boolean isRefreshing = false; private Context mContext;
public RefreshableView(Context context) {
super(context);
mContext = context; }
public RefreshableView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init(); }
private void init() {
// TODO Auto-generated method stub
//滑動對象,
scroller = new Scroller(mContext); //刷新視圖頂端的的view
refreshView = LayoutInflater.from(mContext).inflate(R.layout.refresh_top_item, null);
//指示器view
refreshIndicatorView = (ImageView) refreshView.findViewById(R.id.indicator);
//刷新bar
bar = (ProgressBar) refreshView.findViewById(R.id.progress);
//下拉顯示text
downTextView = (TextView) refreshView.findViewById(R.id.refresh_hint);
//下來顯示時間
timeTextView = (TextView) refreshView.findViewById(R.id.refresh_time); LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, -refreshTargetTop);
lp.topMargin = refreshTargetTop;
lp.gravity = Gravity.CENTER;
addView(refreshView, lp);
downTextString = mContext.getResources().getString(R.string.refresh_down_text);
releaseTextString = mContext.getResources().getString(R.string.refresh_release_text);
} /**
* 刷新
* @param time
*/
private void setRefreshText(String time) {
// TODO Auto-generated method stub
//timeTextView.setText(time);
} @Override
public boolean onTouchEvent(MotionEvent event) { int y= (int) event.getRawY(); switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//記錄下y坐標
lastY = y;
break; case MotionEvent.ACTION_MOVE:
Log.i(TAG, "ACTION_MOVE");
//y移動坐標
int m = y - lastY;
if(((m < 6) && (m > -1)) || (!isDragging )){
doMovement(m);
}
//記錄下此刻y坐標
this.lastY = y;
break; case MotionEvent.ACTION_UP:
Log.i(TAG, "ACTION_UP"); fling(); break;
}
return true;
} /**
* up事件處理
*/
private void fling() {
// TODO Auto-generated method stub
LinearLayout.LayoutParams lp = (LayoutParams) refreshView.getLayoutParams();
Log.i(TAG, "fling()" + lp.topMargin);
if(lp.topMargin > 0){//拉到了觸發可刷新事件
refresh();
}else{
returnInitState();
}
} private void returnInitState() {
// TODO Auto-generated method stub
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)this.refreshView.getLayoutParams();
int i = lp.topMargin;
scroller.startScroll(0, i, 0, refreshTargetTop);
invalidate();
}
private void refresh() {
// TODO Auto-generated method stub
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)this.refreshView.getLayoutParams();
int i = lp.topMargin;
refreshIndicatorView.setVisibility(View.GONE);
bar.setVisibility(View.VISIBLE);
timeTextView.setVisibility(View.GONE);
downTextView.setVisibility(View.GONE);
scroller.startScroll(0, i, 0, 0-i);
invalidate();
if(refreshListener !=null){
refreshListener.onRefresh(this);
isRefreshing = true;
}
} /**
*
*/
@Override
public void computeScroll() {
// TODO Auto-generated method stub
if(scroller.computeScrollOffset()){
int i = this.scroller.getCurrY();
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)this.refreshView.getLayoutParams();
int k = Math.max(i, refreshTargetTop);
lp.topMargin = k;
this.refreshView.setLayoutParams(lp);
this.refreshView.invalidate();
invalidate();
}
}
/**
* 下拉move事件處理
* @param moveY
*/
private void doMovement(int moveY) {
// TODO Auto-generated method stub
LinearLayout.LayoutParams lp = (LayoutParams) refreshView.getLayoutParams();
if(moveY > 0){
//獲取view的上邊距
float f1 =lp.topMargin;
float f2 = moveY * 0.3F;
int i = (int)(f1+f2);
//修改上邊距
lp.topMargin = i;
//修改後刷新
refreshView.setLayoutParams(lp);
refreshView.invalidate();
invalidate();
}
timeTextView.setVisibility(View.VISIBLE);
if(refreshTime!= null){
setRefreshTime(refreshTime);
}
downTextView.setVisibility(View.VISIBLE); refreshIndicatorView.setVisibility(View.VISIBLE);
bar.setVisibility(View.GONE);
if(lp.topMargin > 0){
downTextView.setText(R.string.refresh_release_text);
refreshIndicatorView.setImageResource(R.drawable.refresh_arrow_up);
}else{
downTextView.setText(R.string.refresh_down_text);
refreshIndicatorView.setImageResource(R.drawable.refresh_arrow_down);
} } public void setRefreshEnabled(boolean b) {
this.isRefreshEnabled = b;
} public void setRefreshListener(RefreshListener listener) {
this.refreshListener = listener;
} /**
* 刷新時間
* @param refreshTime2
*/
private void setRefreshTime(Long time) {
// TODO Auto-generated method stub } /**
* 結束刷新事件
*/
public void finishRefresh(){
Log.i(TAG, "執行了=====finishRefresh");
LinearLayout.LayoutParams lp= (LinearLayout.LayoutParams)this.refreshView.getLayoutParams();
int i = lp.topMargin;
refreshIndicatorView.setVisibility(View.VISIBLE);
timeTextView.setVisibility(View.VISIBLE);
scroller.startScroll(0, i, 0, refreshTargetTop);
invalidate();
isRefreshing = false;
} /*該方法一般和ontouchEvent 一起用
* (non-Javadoc)
* @see android.view.ViewGroup#onInterceptTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
// TODO Auto-generated method stub
int action = e.getAction();
int y= (int) e.getRawY();
switch (action) {
case MotionEvent.ACTION_DOWN:
lastY = y;
break; case MotionEvent.ACTION_MOVE:
//y移動坐標
int m = y - lastY; //記錄下此刻y坐標
this.lastY = y;
if(m > 6 && canScroll()){
return true;
}
break;
case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_CANCEL: break;
}
return false;
}
private boolean canScroll() {
// TODO Auto-generated method stub
View childView;
if(getChildCount()>1){
childView = this.getChildAt(1);
if(childView instanceof ListView){
int top =((ListView)childView).getChildAt(0).getTop();
int pad =((ListView)childView).getListPaddingTop();
if((Math.abs(top-pad)) < 3&&
((ListView) childView).getFirstVisiblePosition() == 0){
return true;
}else{
return false;
}
}else if(childView instanceof ScrollView){
if(((ScrollView)childView).getScrollY() == 0){
return true;
}else{
return false;
}
} }
return false;
}
/**
* 刷新監聽接口
* @author Nono
*
*/
public interface RefreshListener{
public void onRefresh(RefreshableView view);
}}
安卓中級教程(5):ScrollView與refreshable之間的設置的更多相关文章
- 安卓中級教程(3):ScrollView
以上是scrollview的圖例,可見srollview是一種滑動功能的控件,亦是非常常見的控件. 一般寫法如下: package com.mycompany.viewscroller; import ...
- 安卓中級教程(9):pathbutton中的animation.java研究(2)
src/geniuz/myPathbutton/composerLayout.java package geniuz.myPathbutton; import com.nineoldandroids. ...
- 安卓中級教程(1):@InjectView
package com.mycompany.hungry; import android.annotation.SuppressLint; import android.app.Activity; i ...
- 安卓中級教程(4):ScrollView與ListView之間的高度問題
在scrollView中加插ListView是一個大難題.其中一個難題是Listview的高度難以計算,輸出效果往往強差人意,就讓我們看看當中的問題 . <LinearLayout xmlns: ...
- 安卓中級教程(10):@InjectView
package com.example.android.db01; import android.app.Activity; import android.content.ContentValues; ...
- 安卓中級教程(8):pathbutton中的animation.java研究(1)
src/geniuz/myPathbutton/myAnimations.java package geniuz.myPathbutton; import java.util.ArrayList; i ...
- 安卓中級教程(6):annotation的基本用法
package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...
- 安卓中級教程(11):深入研究餓了麼的各個java檔運作關係(1)
package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...
- 安卓中級教程(7):annotation中的 public @interface的用法
package com.example.ele_me.util; import java.lang.annotation.Retention; import java.lang.annotation. ...
随机推荐
- winform窗体弹出时,光标默认显示在指定的输入框内
private void Form1_Paint(object sender, PaintEventArgs e) { this.textBox1.SelectAll(); this.textBox1 ...
- git 教程(13)--创建与合并分支
在版本回退里,你已经知道,每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在Git里,这个分支叫主分支,即master分支.HEAD严格来说不是指向提交,而 ...
- spring quartz分布式任务计划
spring quartz分布式任务计划 环境: 通过maven管理的spring mvc工程,且已经成功连接数据库. 数据库表结构 /*Table structure for table `qrtz ...
- CSS 多类选择器
写的代码多了,就会发现,自己越来越无知了,总以为html css很简单,已经掌握的很熟练了,其实我还差的很多. 平时没有用过css的这种写法 .a.b{display:block;} 上网一查才 ...
- jquery获取不到append出来的新元素的解决办法
jquery获取不到append出来的新元素的解决办法 $('body').on("click","property",function(){}) jq1.7+ ...
- Python爬虫Scrapy框架入门(1)
也许是很少接触python的原因,我觉得是Scrapy框架和以往Java框架很不一样:它真的是个框架. 从表层来看,与Java框架引入jar包.配置xml或.property文件不同,Scrapy的模 ...
- 8-04流程控制语句BEGIN ..END
流程控制语句: 是用来控制程序流程的语句. 常用的流程控制语句的分类: 顺序结构:BEGIN...END 分支结构: IF ..ELSE 或CASE ..END 循环结构:WHILE 顺序结构 语法 ...
- Install MySQL on CentOS 7
原文:https://devops.profitbricks.com/tutorials/install-mysql-on-centos-7/ 1.下载mysql 在mysql官网选择适合的mysql ...
- Jmeter之Web端HTTP性能测试(九)
之前有跟大家讲过通过Badboy来录制脚本,这里就不多说了,需要的可以参考 Jmeter之Badboy录制脚本及简化脚本http请求(三) 这边就不用项目的链接了,直接采用http://www.cnb ...
- linux磁盘读写性能优化
在LINUX系统中,如果有大量读请求,默认的请求队列或许应付不过来,我们可以 动态调整请求队列数来提高效率,默认的请求队列数存放在/sys/block/xvda/queue/nr_requests 文 ...