标签:
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">之前看过网易云阅读客户端,里面的文章可以实现上下拉动实现上下翻页的效果,感觉体验效果很不错。</span>
公司新版本项目的开发中也要求实现类似的效果,不过还好项目需求里面可以提前知道需要实现上下拉动翻页的总的页数。如果像网易那种不提前知道总的页数感觉控制好LinearLayout里面的childView应该也可以达到效果。
好记性不如烂笔头,先写下我提前知道总页数实现上下拉翻页的问题吧!
首先布局仅仅是一个简单的LinearLayout包裹着
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/fenleiPullContentLayout" android:orientation="vertical"> </LinearLayout>
pullContentLayout.removeAllViews(); pullViews.clear(); for(int i=0;i<leftEntityData.size();i++){ PullToRefreshProView pullview = (PullToRefreshProView) inflater.inflate(R.layout.fenleipro_item, null); LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT, scrollHeight); pullview.setLayoutParams(param); LinearLayout pullayout = (LinearLayout) pullview.findViewById(R.id.fenleirightlayout); RightAdapter adapter = new RightAdapter(rightEntityList.get(i)); pullayout.removeAllViews(); for(int k=0;k<adapter.getCount();k++){ View view = adapter.getView(k, null, null); pullayout.addView(view,k); } pullViews.add(pullview); pullContentLayout.addView(pullview, i); if(i==0){ pullview.setHeaderRefresh(false); pullview.setOnFooterRefreshListener(new MyOnRefreshListener(i)); }else if(i==leftEntityData.size()-1){ pullview.setFooterRefresh(false); pullview.setOnHeaderRefreshListener(new MyOnRefreshListener(i)); }else{ pullview.setOnHeaderRefreshListener(new MyOnRefreshListener(i)); pullview.setOnFooterRefreshListener(new MyOnRefreshListener(i)); } }
private class MyOnRefreshListener implements OnHeaderRefreshListener,OnFooterRefreshListener{ @Override public void onFooterRefresh(PullToRefreshProView view) { } @Override public void onHeaderRefresh(PullToRefreshProView view) { } }
这里关键是在动画效果交互的实现。
上代码,上拉的动画
public class PullToRefreshUpAnimation extends Animation{ private View view1,view2; private int delt; private int topMarginView1 = 0; public PullToRefreshUpAnimation(Context context,View v1,View v2,int from,int to){ super(); view1 = v1; view2 = v2; delt = to - from; topMarginView1 = view1.getMeasuredHeight(); setDuration(450); setFillAfter(true); setInterpolator(new DecelerateInterpolator()); } public PullToRefreshUpAnimation(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub setDuration(450); setFillAfter(true); setInterpolator(new DecelerateInterpolator()); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { android.widget.LinearLayout.LayoutParams param = (android.widget.LinearLayout.LayoutParams)view2.getLayoutParams(); param.topMargin = (int) (interpolatedTime*delt); param.height = Math.abs(delt); android.widget.LinearLayout.LayoutParams param1 = (android.widget.LinearLayout.LayoutParams) view1.getLayoutParams(); param1.topMargin = (int) (topMarginView1*(interpolatedTime-1)); param1.height = topMarginView1; view1.setLayoutParams(param1); view2.setLayoutParams(param); } @Override public boolean willChangeBounds() { // TODO Auto-generated method stub return true; } }
public class PullToRefreshAnimation extends Animation{ private View view; private int delt; public PullToRefreshAnimation(Context context,View v,int from,int to){ super(); view = v; delt = to - from; setDuration(450); setFillAfter(true); setInterpolator(new DecelerateInterpolator()); } public PullToRefreshAnimation(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub setDuration(450); setFillAfter(true); setInterpolator(new DecelerateInterpolator()); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { android.widget.LinearLayout.LayoutParams param = (android.widget.LinearLayout.LayoutParams)view.getLayoutParams(); param.topMargin = (int) (interpolatedTime*delt); param.height = Math.abs(delt); param.width = android.widget.LinearLayout.LayoutParams.MATCH_PARENT; view.setLayoutParams(param); } @Override public boolean willChangeBounds() { // TODO Auto-generated method stub return true; } }
所以需要给这个动画设置animationListener,然后每次需要上啦下拉动画前把LinearLayout的height和topMargin重新设置过来,具体怎么实现看具体情况。
总体的程序设计思路就是这个样子,如果还有人能够有更简便的实现方式可以留言交流,谢谢!
PS:这里的核心实现方式其实就是控制好Linearlayout子LinearLayout的height和topMargin
使用LinearLayout和PullRefreshView实现上下翻页
标签:
原文地址:http://blog.csdn.net/fancylovejava/article/details/42497295