




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
【移動應用開發(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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度智能制造企業(yè)生產管理人才招聘與智能制造協(xié)議
- 二零二五年度立體停車設備研發(fā)與委托運營管理合同
- 二零二五年度航空航天就業(yè)勞動合同
- 二零二五年度叉車安全風險評估與整改合同
- 圍城深度解讀與評析征文
- 新產品市場推廣策略及執(zhí)行方案
- 工業(yè)自動化控制系統(tǒng)設計與維護服務協(xié)議
- 《天文觀測與天體物理學習計劃》
- 行業(yè)市場深度調研分析
- 互聯網+三農營銷模式創(chuàng)新案例集
- 2025年黑龍江農墾職業(yè)學院單招職業(yè)傾向性測試題庫完整
- 2025年黑龍江旅游職業(yè)技術學院單招職業(yè)傾向性測試題庫附答案
- 《多彩的節(jié)日民俗》(教學設計)浙教版四年級下冊綜合實踐活動
- 2025年健康咨詢管理服務合同范文
- 光學鏡片透光率測量基準
- 歷史-貴州省貴陽市2025年高三年級適應性考試(一)(貴陽一模)試題和答案
- 2025年01月2025全國婦聯所屬在京事業(yè)單位公開招聘93人筆試歷年典型考題(歷年真題考點)解題思路附帶答案詳解
- 輻射安全管理測試題含答案
- 2025年北京社會管理職業(yè)學院高職單招高職單招英語2016-2024年參考題庫含答案解析
- 信息系統(tǒng)項目計劃書
- 2025學生管理工作計劃怎么寫
評論
0/150
提交評論