亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android編程使用LinearLayout和PullRefreshView實(shí)現(xiàn)上下翻頁(yè)功能的方法

 更新時(shí)間:2017年08月15日 11:53:06   作者:fancylovejava  
這篇文章主要介紹了Android編程使用LinearLayout和PullRefreshView實(shí)現(xiàn)上下翻頁(yè)功能的方法,涉及Android界面布局與邏輯處理相關(guān)操作技巧,需要的朋友可以參考下

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

前看過(guò)網(wǎng)易云閱讀客戶端,里面的文章可以實(shí)現(xiàn)上下拉動(dòng)實(shí)現(xiàn)上下翻頁(yè)的效果,感覺(jué)體驗(yàn)效果很不錯(cuò)。

公司新版本項(xiàng)目的開發(fā)中也要求實(shí)現(xiàn)類似的效果,不過(guò)還好項(xiàng)目需求里面可以提前知道需要實(shí)現(xiàn)上下拉動(dòng)翻頁(yè)的總的頁(yè)數(shù)。如果像網(wǎng)易那種不提前知道總的頁(yè)數(shù)感覺(jué)控制好LinearLayout里面的childView應(yīng)該也可以達(dá)到效果。

好記性不如爛筆頭,先寫下我提前知道總頁(yè)數(shù)實(shí)現(xiàn)上下拉翻頁(yè)的問(wèn)題吧!

首先布局僅僅是一個(gè)簡(jiǎn)單的LinearLayout包裹著

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

然后通過(guò)一個(gè)for循環(huán)把PullRefreshView包裹進(jìn)來(lái)

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

代碼說(shuō)明下:這里的PullToRefreshProView就是一個(gè)開源的下拉刷新控件,繼承的是一個(gè)LinearLayout實(shí)現(xiàn)的。網(wǎng)上有源碼;然后RightAdapter是一個(gè)BaseAdapter,通過(guò)這個(gè)adapter的getview得到每個(gè)view,然后把view添加到inflater出的PullToRefreshProView的子Linearlayoyut里面。然后給每個(gè)PullToRefreshProView設(shè)置上啦下拉的回調(diào)接口,第一個(gè)沒(méi)有上啦,最后個(gè)沒(méi)下拉。這里的MyOnRefreshListener是自己定義的下拉接口

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

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

這里關(guān)鍵是在動(dòng)畫效果交互的實(shí)現(xiàn)。

上代碼,上拉的動(dòng)畫

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

下拉動(dòng)畫

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

這兩個(gè)動(dòng)畫的后果是導(dǎo)致最后最外層的LinearLayout包裹的每個(gè)子LinearLayout改變了自己的height和topMargin,

所以需要給這個(gè)動(dòng)畫設(shè)置animationListener,然后每次需要上啦下拉動(dòng)畫前把LinearLayout的height和topMargin重新設(shè)置過(guò)來(lái),具體怎么實(shí)現(xiàn)看具體情況。

PS:這里的核心實(shí)現(xiàn)方式其實(shí)就是控制好Linearlayout子LinearLayout的height和topMargin

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android布局layout技巧總結(jié)》、《Android開發(fā)動(dòng)畫技巧匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論