最近電商類項目有個需求挺頭疼,要求用GridView去顯示商品,滑動到底部下拉能加載更多商品,向下滑動過程中需要隱藏掉自定義的Action布局,向上滑動能顯示出來,整體效果類似淘寶APP搜索結(jié)果頁那樣。
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),石城企業(yè)網(wǎng)站建設(shè),石城品牌網(wǎng)站建設(shè),網(wǎng)站定制,石城網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,石城網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
起初覺得挺簡單的,但是后來才發(fā)現(xiàn)還是得轉(zhuǎn)一點(diǎn)腦子。最開始我想用PullToRefreshGridView,但是后來發(fā)現(xiàn)GridView沒有添加headview的方法,只能采用PullToRefreshScrollView內(nèi)嵌套GridView的方法,Scrollview里多放一個空白布局當(dāng)GridView的headview,高度和Action布局一樣就行。這時還有一個問題,ScrollView和GridView會產(chǎn)生滑動沖突,還好網(wǎng)上很容易找到解決辦法,我這里采用自定義GridView,最后就是監(jiān)聽方法了,最為關(guān)鍵的點(diǎn),還是直接上代碼吧:
自定義GridView:
package com.example.ztestscrollview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View.MeasureSpec;
import android.widget.GridView;
public class MyGridView extends GridView{
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
主界面:
package com.example.ztestscrollview;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
public class MainActivity extends Activity
{
int num = 10;
private GridAdapter adapter;
private ScrollView scrollView;
private PullToRefreshScrollView ptr_scrollview;
private View actionLayout;
private MyGridView myGridview;
@Override
protected void onCreate(Bundle savedInstanceState)
{
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(new OnRefreshListener
{
@Override
public void onRefresh(PullToRefreshBase
{
num += 10;
adapter.notifyDataSetChanged();
ptr_scrollview.onRefreshComplete();
}
});
myGridview = (MyGridView) findViewById(R.id.myGridView1);
adapter = new GridAdapter();
myGridview.setAdapter(adapter);
scrollView = ptr_scrollview.getRefreshableView();
scrollView.setOnTouchListener(new OnTouchListener()
{
// 判斷是否第一個滑動值
boolean isFirst = true;
// Scrollview滑動的ScrollY值
private int ScrollY_Move;
// 第一個滑動的ScrollY值
private int First_ScrollY_Move;
// 手指抬起時ScrollY值
private int ScrollY_Up;
// ScrollY_Move和First_ScrollY_Move的差值
private int Cha;
@Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
//ACTIN_DOWN沒用,不用監(jiān)聽
case MotionEvent.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;
// 當(dāng)ScrollY_Move>First_ScrollY_Move時證明滑動方向是向下;
// 加上判斷差值的目的:當(dāng)Actionbar顯示的時候,手指按住往下滑過一段距離還未抬起時,Actionbar也能隱藏
if (First_ScrollY_Move < ScrollY_Move || Cha > 200)
{
// 隱藏Actionbar
actionLayout.setVisibility(View.GONE);
}
// 當(dāng)ScrollY_Move // 加上判斷差值的目的:當(dāng)Actionbar顯示的時候,手指按住往上滑過一段距離還未抬起時,Actionbar也能顯示 // 判斷ScrollY_Move < 150目的:當(dāng)Scrollview滑動接近頂端時必須顯示Actionbar else if (First_ScrollY_Move > ScrollY_Move || Cha < -200 || ScrollY_Move < 150) { actionLayout.setVisibility(View.VISIBLE); } break; case MotionEvent.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; } return false; } }); } class GridAdapter extends BaseAdapter { @Override public View getView(int position, View convertView, ViewGroup parent) { View inflate = getLayoutInflater().inflate(R.layout.griditem, null); return inflate; } @Override public int getCount() { // TODO Auto-generated method stub return num; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 布局: xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > android:id="@+id/ptr_scrollview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" > android:id="@+id/headview" android:layout_width="match_parent" android:layout_height="120dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > android:id="@+id/myGridView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/headview" android:numColumns="2" > android:id="@+id/actionlayout" android:layout_width="match_parent" android:layout_height="120dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="#cdcdcd" > android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="ActionLayout" />
網(wǎng)站標(biāo)題:PullToRefreshScrollView+GridView,實(shí)現(xiàn)向下滑動中隱藏自定義Action布局
網(wǎng)頁URL:http://weahome.cn/article/jpidhd.html