如何進行Gridview的實現,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
我們知道Gridview不能設置onClickListener和onLongClickListener,當GridView中出現了Blank cell,有時需要響應click事件,沒有API可以調用。
AbsListView中的pointToPositon方法可以返回某個點對應的adapter中的數據position,當返回-1時,說明該點不在可見點item上,為空白區域。利用這個方法在dispatchTouchEvent中設置回調,可以解決這個問題。
以下是我實現的可以增加了onClickListener 和onLongClickListener的Gridview,有需要的可以參考一下:
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.ViewConfiguration; import android.widget.GridView; public class ClickableGridView extends GridView { public ClickableGridView(Context context){ super(context); } public ClickableGridView(Context context, AttributeSet attrs) { super(context, attrs); } public ClickableGridView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } private OnNoItemClickListener clickListener; private OnNoItemLongClickListener longClickListener; private boolean mHasPerformedLongPress = false; private boolean isPressed; private CheckForLongPress checkForLongPress; public interface OnNoItemClickListener { public void onNoItemClick(); } public interface OnNoItemLongClickListener{ public void onNoItemLongClick(); } public void setOnNoItemClickListener(OnNoItemClickListener listener) { this.clickListener = listener; } public void setOnNoItemLongClickListener(OnNoItemLongClickListener longClickListener) { this.longClickListener = longClickListener; } @Override public boolean dispatchTouchEvent(MotionEvent event) { // The pointToPosition() method returns -1 if the touch event // occurs outside of a child View. if (pointToPosition((int) event.getX(), (int) event.getY()) == -1) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: isPressed = true; mHasPerformedLongPress = false; if(checkForLongPress == null){ checkForLongPress = new CheckForLongPress(); } postDelayed(checkForLongPress, ViewConfiguration.getLongPressTimeout()); break; case MotionEvent.ACTION_UP: if(!mHasPerformedLongPress){ removeCallbacks(checkForLongPress); if(clickListener != null){ clickListener.onNoItemClick(); } }else{ mHasPerformedLongPress = false; } isPressed = false; break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_MOVE: default: removeCallbacks(checkForLongPress); isPressed = false; break; } } return super.dispatchTouchEvent(event); } private final class CheckForLongPress implements Runnable { @Override public void run() { if (isPressed){ if (longClickListener != null) { longClickListener.onNoItemLongClick(); mHasPerformedLongPress = true; } } } } }
關于如何進行Gridview的實現問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創新互聯行業資訊頻道了解更多相關知識。
另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
文章標題:如何進行Gridview的實現-創新互聯
瀏覽路徑:http://www.yijiale78.com/article18/csipdp.html
成都網站建設公司_創新互聯,為您提供外貿建站、微信小程序、做網站、網站內鏈、手機網站建設、靜態網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯