本篇文章給大家分享的是有關Android中怎么通過自定義控件實現(xiàn)下拉刷新效果,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、微信小程序開發(fā)、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了天山免費建站歡迎大家使用!
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if (pullText.getTop() == 0) { viewHeight = pullText.getMeasuredHeight(); pullText.layout(l, 0, r, b); myList.layout(l, 0, r, b); pullText.offsetTopAndBottom(-viewHeight); } else { pullText.layout(l, pullText.getTop(), r, pullText.getBottom()); myList.layout(l, myList.getTop(), r, myList.getBottom()); } }
上面的代碼段中,pullText即是TV,myList是LV。這樣在下拉LV的時候,TV就會跟著往下走,所以就會出現(xiàn)在屏幕中實現(xiàn)我們想要的效果。
/** * 這是拖拽效果的主要邏輯 */ private class DragHelperCallback extends ViewDragHelper.Callback { @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { int childIndex = 1; if (changedView == myList) { childIndex = 2; } onViewPosChanged(childIndex, top); } @Override public boolean tryCaptureView(View child, int pointerId) { return true; } @Override public int getViewVerticalDragRange(View child) { return 1; } @Override public void onViewReleased(View releasedChild, float xvel, float yvel) { refreshOrNot(releasedChild, yvel); } @Override public int clampViewPositionVertical(View child, int top, int dy) { int finalTop = top; if (child == pullText) { if (top > 0) { finalTop = 0; } } else if (child == myList) { if (top < 0) { finalTop = 0; } if(top >= viewHeight){ pullText.setText("松開刷新"); }else{ pullText.setText("下拉刷新"); } } return child.getTop() + (finalTop - child.getTop()) / 2; } }
上面的代碼段中,主要是在clampViewPositionVertical中判斷滑動的位置,作用的子view。其他就不多說了,大致和之前的博客相同。主要說說onViewReleased吧。在此函數(shù)中是在用戶手勢抬起時響應的,所以我們在此實現(xiàn)下拉后的刷新。我們先定義一個接口,以便在刷新的時候調(diào)用。
public interface pulltorefreshNotifier { public void onPull(); }
public void setpulltorefreshNotifier(pulltorefreshNotifier pullNotifier) { this.pullNotifier = pullNotifier; }
private void refreshOrNot(View releasedChild, float yvel) { int finalTop = 0; if (releasedChild == pullText) { // 拖動第一個view松手 if (yvel < -50) { finalTop = 0; } else { finalTop = viewHeight; } } else { // 拖動第二個view松手 if (yvel > viewHeight - 5 || releasedChild.getTop() >= viewHeight) { finalTop = viewHeight; if (null != pullNotifier) { pullNotifier.onPull(); } pullText.setText("正在刷新"); } } if (VDH.smoothSlideViewTo(myList, 0, finalTop)) { ViewCompat.postInvalidateOnAnimation(this); } }
拖動第二個view時,也就是LV時,我們判斷一下是否需要刷新,需要刷新則執(zhí)行onPull();
然后我們來看一下主要的Activity:
package com.maxi.pulltorefreshtest; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import com.maxi.pulltorefreshtest.adapter.ProjectAdapter; import com.maxi.pulltorefreshtest.widget.MyListView; import com.maxi.pulltorefreshtest.widget.PullToRefreshGroup; import com.maxi.pulltorefreshtest.widget.PullToRefreshGroup.pulltorefreshNotifier; public class MainActivity extends Activity { private PullToRefreshGroup pullListgroup; private boolean isDown = false; private MyListView myList; private ProjectAdapter pa; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findView(); init(); } private void findView() { pullListgroup = (PullToRefreshGroup) findViewById(R.id.pulltorefresh); myList = pullListgroup.returnMylist(); } private void init() { pulltorefreshNotifier pullNotifier = new pulltorefreshNotifier() { @Override public void onPull() { // TODO Auto-generated method stub downLoad(); } }; pullListgroup.setpulltorefreshNotifier(pullNotifier); pa = new ProjectAdapter(this); myList.setAdapter(pa); pa.notifyDataSetChanged(); } private void downLoad() { if (!isDown) { isDown = true; new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(2000); handler.sendEmptyMessage(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } } @SuppressLint("HandlerLeak") private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); switch (msg.what) { case 1: pullListgroup.refreshComplete(); isDown = false; break; default: break; } } }; }
我們在他刷新的時候執(zhí)行downLoad();刷新數(shù)據(jù)。為了達到效果可以看出我讓線程暫停2s。然后調(diào)用refreshComplete();
public void refreshComplete() { if (VDH.smoothSlideViewTo(myList, 0, 0)) { ViewCompat.postInvalidateOnAnimation(this); } }
實現(xiàn)刷新好后讓TV繼續(xù)返回屏幕上方。
上段代碼中我們發(fā)現(xiàn)MyListView是重寫的ListView,主要是處理手勢事件的。
package com.maxi.pulltorefreshtest.widget; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.ListView; public class MyListView extends ListView { boolean allowDragBottom = true; float downY = 0; boolean needConsumeTouch = true; public MyListView(Context context){ super(context); } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { downY = ev.getRawY(); needConsumeTouch = true; if (getMyScrollY() == 0) { allowDragBottom = true; } else { allowDragBottom = false; } } else if (ev.getAction() == MotionEvent.ACTION_MOVE) { if (!needConsumeTouch) { getParent().requestDisallowInterceptTouchEvent(false); return false; } else if (allowDragBottom) { if (downY - ev.getRawY() < -2) { needConsumeTouch = false; getParent().requestDisallowInterceptTouchEvent(false); return false; } } } getParent().requestDisallowInterceptTouchEvent(needConsumeTouch); return super.dispatchTouchEvent(ev); } public int getMyScrollY() { View c = getChildAt(0); if (c == null) { return 0; } int firstVisiblePosition = getFirstVisiblePosition(); int top = c.getTop(); return -top + firstVisiblePosition * c.getHeight(); } }
以上就是Android中怎么通過自定義控件實現(xiàn)下拉刷新效果,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁名稱:Android中怎么通過自定義控件實現(xiàn)下拉刷新效果
標題URL:http://bm7419.com/article14/pciide.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、網(wǎng)站內(nèi)鏈、網(wǎng)站維護、小程序開發(fā)、網(wǎng)站營銷、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)