做安卓开发的朋友们,不知道你们会不会经常用遇到这样的情景,某个做所畏的产品设计的sb,拿着iphone来看给你看,说看苹果的这个效果多好,看那个效果多好。苹果也比安卓清晰多了,你能不能也把咱们的安卓应用也做成这种效果的。那样的用户体验更好,更cool一些。不知你们有没有遇到过这样半吊子的sb设计,反正我是遇到了。所以本文由此而生。
进入正题:首先你要实现弹性效果的view要能确定什么时候应该出现下拉的效果,什么时候出现下推的效果。在代码里的体现就是你要实现IScrollOverable接口。本文中的例子就拿GridView来做个例子。
无图无真相:
实现了IScrollOverable接口的GridView:
public class BshSOGridView extends GridView implements IScrollOverable { public BshSOGridView(Context context, AttributeSet attrs, int defStyle) { super( context, attrs, defStyle ); } public BshSOGridView(Context context, AttributeSet attrs) { super( context, attrs ); } public BshSOGridView(Context context) { super( context ); } @Override public boolean isScrollOnTop() { return 0 == getFirstVisiblePosition() ? true : false; } @Override public boolean isScrollOnBtm() { return (getCount() - 1) == getLastVisiblePosition() ? true : false; } }
调用Activity:
public class BshSOViewActivity extends Activity { BshElasticView ev; BshSOGridView gv; GridAdagper ga = new GridAdagper(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.elastic_grid ); ev = ( BshElasticView ) findViewById( R.id.ev ); //拉动幅度 ev.setFactor( 2 ); //拉动范围 ev.setMaxElastic( 0.9f ); gv = new BshSOGridView( this ); gv.setBackgroundColor( Color.WHITE ); gv.setNumColumns( 4 ); gv.setAdapter( ga ); ev.setScrollOverable( gv ); ev.irefresh = new IRefresh() { @Override public boolean refreshTop() { new Thread( new Runnable() { @Override public void run() { try { Log.d( "bsh", "refreshing" ); //在这里做刷新操作读数据神马的。这里用睡觉代替 Thread.sleep( 3000 ); } catch ( InterruptedException e ) { e.printStackTrace(); } ev.onRefreshComplete(); } } ).start(); return false; } @Override public boolean refreshBtm() { new Thread( new Runnable() { @Override public void run() { try { Log.d( "bsh", "refreshing" ); Thread.sleep( 3000 ); } catch ( InterruptedException e ) { e.printStackTrace(); } ev.onRefreshComplete(); } } ).start(); return false; } }; } class GridAdagper extends BaseAdapter { @Override
public int getCount() {
return 100; } @Override public Object getItem(int arg0) { return null; } @Override public long getItemId(int arg0) { return 0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { if ( null == arg1 ) { arg1 = new ImageView( BshSOViewActivity.this ); arg1.setBackgroundResource( R.drawable.ic_launcher ); } return arg1; } } }