国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程使用LinearLayout和PullRefreshView實現上下翻頁功能的方法

2019-12-12 02:15:04
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程使用LinearLayout和PullRefreshView實現上下翻頁功能的方法。分享給大家供大家參考,具體如下:

前看過網易云閱讀客戶端,里面的文章可以實現上下拉動實現上下翻頁的效果,感覺體驗效果很不錯。

公司新版本項目的開發中也要求實現類似的效果,不過還好項目需求里面可以提前知道需要實現上下拉動翻頁的總的頁數。如果像網易那種不提前知道總的頁數感覺控制好LinearLayout里面的childView應該也可以達到效果。

好記性不如爛筆頭,先寫下我提前知道總頁數實現上下拉翻頁的問題吧!

首先布局僅僅是一個簡單的LinearLayout包裹著

<LinearLayout android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:id="@+id/fenleiPullContentLayout"  android:orientation="vertical"></LinearLayout>

然后通過一個for循環把PullRefreshView包裹進來

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));  }}

代碼說明下:這里的PullToRefreshProView就是一個開源的下拉刷新控件,繼承的是一個LinearLayout實現的。網上有源碼;然后RightAdapter是一個BaseAdapter,通過這個adapter的getview得到每個view,然后把view添加到inflater出的PullToRefreshProView的子Linearlayoyut里面。然后給每個PullToRefreshProView設置上啦下拉的回調接口,第一個沒有上啦,最后個沒下拉。這里的MyOnRefreshListener是自己定義的下拉接口

private class MyOnRefreshListener implements OnHeaderRefreshListener,OnFooterRefreshListener{    @Override    public void onFooterRefresh(PullToRefreshProView view) {    }    @Override    public void onHeaderRefresh(PullToRefreshProView view) {    }}

然后再onFooter和onHeader里面寫下拉上拉邏輯。

這里關鍵是在動畫效果交互的實現。

上代碼,上拉的動畫

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;  }}

這兩個動畫的后果是導致最后最外層的LinearLayout包裹的每個子LinearLayout改變了自己的height和topMargin,

所以需要給這個動畫設置animationListener,然后每次需要上啦下拉動畫前把LinearLayout的height和topMargin重新設置過來,具體怎么實現看具體情況。

PS:這里的核心實現方式其實就是控制好Linearlayout子LinearLayout的height和topMargin

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android布局layout技巧總結》、《Android開發動畫技巧匯總》、《Android開發入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android資源操作技巧匯總》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兰西县| 循化| 郯城县| 宁陵县| 庄浪县| 大新县| 贡嘎县| 华安县| 黄龙县| 荔浦县| 瓮安县| 阳信县| 加查县| 万安县| 米林县| 孟州市| 建平县| 青浦区| 视频| 叶城县| 饶阳县| 佳木斯市| 禹城市| 乌拉特中旗| 阳谷县| 分宜县| 新竹县| 临沭县| 固始县| 文登市| 陵川县| 菏泽市| 于田县| 循化| 安岳县| 扶沟县| 宁津县| 兖州市| 南安市| 梧州市| 博兴县|