【移動應用開發(fā)技術】PullToRefreshScrollView+GridView實現向下滑動中隱藏自定義Action布局_第1頁
【移動應用開發(fā)技術】PullToRefreshScrollView+GridView實現向下滑動中隱藏自定義Action布局_第2頁
【移動應用開發(fā)技術】PullToRefreshScrollView+GridView實現向下滑動中隱藏自定義Action布局_第3頁
【移動應用開發(fā)技術】PullToRefreshScrollView+GridView實現向下滑動中隱藏自定義Action布局_第4頁
【移動應用開發(fā)技術】PullToRefreshScrollView+GridView實現向下滑動中隱藏自定義Action布局_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

【移動應用開發(fā)技術】PullToRefreshScrollView+GridView,實現向下滑動中隱藏自定義Action布局

最近電商類項目有個需求挺頭疼,要求用GridView去顯示商品,滑動到底部下拉能加載更多商品,向下滑動過程中需要隱藏掉自定義的Action布局,向上滑動能顯示出來,整體效果類似淘寶APP搜索結果頁那樣。

起初覺得挺簡單的,但是后來才發(fā)現還是得轉一點腦子。最開始我想用PullToRefreshGridView,但是后來發(fā)現GridView沒有添加headview的方法,只能采用PullToRefreshScrollView內嵌套GridView的方法,Scrollview里多放一個空白布局當GridView的headview,高度和Action布局一樣就行。這時還有一個問題,ScrollView和GridView會產生滑動沖突,還好網上很容易找到解決辦法,我這里采用自定義GridView,最后就是監(jiān)聽方法了,最為關鍵的點,還是直接上代碼吧:自定義GridView:packagecom.example.ztestscrollview;importandroid.content.Context;importandroid.util.AttributeSet;importandroid.view.View.MeasureSpec;importandroid.widget.GridView;publicclassMyGridViewextendsGridView{publicMyGridView(Contextcontext,AttributeSetattrs){

super(context,attrs);}publicMyGridView(Contextcontext){

super(context);}publicMyGridView(Contextcontext,AttributeSetattrs,intdefStyle){

super(context,attrs,defStyle);}@Override

publicvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){

intexpandSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,

MeasureSpec.AT_MOST);

super.onMeasure(widthMeasureSpec,expandSpec);}

}主界面:packagecom.example.ztestscrollview;importandroid.app.Activity;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.Menu;importandroid.view.MotionEvent;importandroid.view.View;importandroid.view.View.OnTouchListener;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.ScrollView;importcom.handmark.pulltorefresh.library.PullToRefreshBase;importcom.handmark.pulltorefresh.library.PullToRefreshBase.Mode;importcom.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;importcom.handmark.pulltorefresh.library.PullToRefreshScrollView;publicclassMainActivityextendsActivity{

intnum=10;

privateGridAdapteradapter;

privateScrollViewscrollView;

privatePullToRefreshScrollViewptr_scrollview;

privateViewactionLayout;

privateMyGridViewmyGridview;

@Override

protectedvoidonCreate(BundlesavedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

actionLayout=findViewById(R.id.actionlayout);

ptr_scrollview=(PullToRefreshScrollView)findViewById(R.id.ptr_scrollview);

ptr_scrollview.setMode(Mode.PULL_UP_TO_REFRESH);

ptr_scrollview.setOnRefreshListener(newOnRefreshListener<ScrollView>()

{

@Override

publicvoidonRefresh(PullToRefreshBase<ScrollView>refreshView)

{

num+=10;

adapter.notifyDataSetChanged();

ptr_scrollview.onRefreshComplete();

}

});

myGridview=(MyGridView)findViewById(R.id.myGridView1);

adapter=newGridAdapter();

myGridview.setAdapter(adapter);

scrollView=ptr_scrollview.getRefreshableView();

scrollView.setOnTouchListener(newOnTouchListener()

{

//判斷是否第一個滑動值

booleanisFirst=true;

//Scrollview滑動的ScrollY值

privateintScrollY_Move;

//第一個滑動的ScrollY值

privateintFirst_ScrollY_Move;

//手指抬起時ScrollY值

privateintScrollY_Up;

//ScrollY_Move和First_ScrollY_Move的差值

privateintCha;

@Override

publicbooleanonTouch(Viewv,MotionEventevent)

{

switch(event.getAction())

{

//ACTIN_DOWN沒用,不用監(jiān)聽

caseMotionEvent.ACTION_MOVE:

if(isFirst)

{

//記錄下第一個滑動的ScrollY值

First_ScrollY_Move=v.getScrollY();

isFirst=false;

}

//記錄下Scrollview滑動的ScrollY值

ScrollY_Move=v.getScrollY();

//計算出ScrollY_Move和First_ScrollY_Move的差值

Cha=ScrollY_Move-First_ScrollY_Move;

//當ScrollY_Move>First_ScrollY_Move時證明滑動方向是向下;

//加上判斷差值的目的:當Actionbar顯示的時候,手指按住往下滑過一段距離還未抬起時,Actionbar也能隱藏

if(First_ScrollY_Move<ScrollY_Move||Cha>200)

{

//隱藏Actionbar

actionLayout.setVisibility(View.GONE);

}

//當ScrollY_Move<First_ScrollY_Move時證明滑動方向是向上;

//加上判斷差值的目的:當Actionbar顯示的時候,手指按住往上滑過一段距離還未抬起時,Actionbar也能顯示

//判斷ScrollY_Move<150目的:當Scrollview滑動接近頂端時必須顯示Actionbar

elseif(First_ScrollY_Move>ScrollY_Move||Cha<-200

||ScrollY_Move<150)

{

actionLayout.setVisibility(View.VISIBLE);

}

break;

caseMotionEvent.ACTION_UP:

ScrollY_Up=v.getScrollY();//記錄下手指抬起時ScrollY值

isFirst=true;//將isFirst還原為初始化

if(ScrollY_Up==0)

{

actionLayout.setVisibility(View.VISIBLE);

}

//Log.e("ACTION_UP>","scrollY_up:"+scrollY_up+"

"

//+"eventY_up:"+eventY_up);

break;

default:

break;

}

returnfalse;

}

});

}

classGridAdapterextendsBaseAdapter

{

@Override

publicViewgetView(intposition,ViewconvertView,ViewGroupparent)

{

Viewinflate=getLayoutInflater().inflate(R.layout.griditem,null);

returninflate;

}

@Override

publicintgetCount()

{

//TODOAuto-generatedmethodstub

returnnum;

}

@Override

publicObjectgetItem(intposition)

{

//TODOAuto-generatedmethodstub

returnnull;

}

@Override

publiclonggetItemId(intposition)

{

//TODOAuto-generatedmethodstub

return0;

}

}

@Override

publicbooleanonCreateOptionsMenu(Menumenu)

{

//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.

getMenuInflater().inflate(R.menu.main,menu);

returntrue;

}}布局:<RelativeLayoutxmlns:android="/apk/res/android"

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<com.handmark.pulltorefresh.library.PullToRefreshScrollView

android:id="@+id/ptr_scrollview"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true">

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:descendantFocusability="blocksDescendants">

<RelativeLayout

android:id="@+id/headview"

android:layout_width="match_parent"

android:layout_height="120dp"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true">

</RelativeLayout>

<com.example.ztestscrollview.MyGridView

android:id="@+id/myGridView1"

android:layout_width="match_parent"

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論