您好,登錄后才能下訂單哦!
最近電商類項目有個需求挺頭疼,要求用GridView去顯示商品,滑動到底部下拉能加載更多商品,向下滑動過程中需要隱藏掉自定義的Action布局,向上滑動能顯示出來,整體效果類似淘寶APP搜索結果頁那樣。
起初覺得挺簡單的,但是后來才發現還是得轉一點腦子。最開始我想用PullToRefreshGridView,但是后來發現GridView沒有添加headview的方法,只能采用PullToRefreshScrollView內嵌套GridView的方法,Scrollview里多放一個空白布局當GridView的headview,高度和Action布局一樣就行。這時還有一個問題,ScrollView和GridView會產生滑動沖突,還好網上很容易找到解決辦法,我這里采用自定義GridView,最后就是監聽方法了,最為關鍵的點,還是直接上代碼吧:
自定義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<ScrollView>()
{
@Override
public void onRefresh(PullToRefreshBase<ScrollView> refreshView)
{
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沒用,不用監聽
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;
// 當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
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;
}
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/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"
android:layout_height="match_parent"
android:layout_below="@+id/headview"
android:numColumns="2" >
</com.example.ztestscrollview.MyGridView>
</RelativeLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
<RelativeLayout
android:id="@+id/actionlayout"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#cdcdcd" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="ActionLayout" />
</RelativeLayout>
</RelativeLayout>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。