Waterfall streams that can be pulled and refreshed and loaded up and down
The waterfall was very popular when it first came out, and everyone thought it was amazing, so I also played it. But as a result, when it was applied to specific scenarios, we need to use the pull-up refresh function, and the open-source effect cannot be achieved. Therefore, after great guidance, we have re-implemented this effect.
First, we need to reference the waterfall Stream project. github Project address: waterfall.
Then we introduce the pull-down refresh project. github Project address: Release. We need to modify the PullToRefreshBase. java class and add two methods to determine whether the PullToRefreshBase is pulled up or pulled down.
public boolean isHeaderShown() {return getHeaderLayout().isShown();}public boolean isFooterShown() {return getFooterLayout().isShown(); }
So far, the reference of the project library has been completed. Next, we need to customize the PullToRefreshStaggeredGridView class to merge the waterfall stream and refresh component.
import android.annotation.SuppressLint;import android.annotation.TargetApi;import android.content.Context;import android.os.Build.VERSION;import android.os.Build.VERSION_CODES;import android.os.Bundle;import android.util.AttributeSet;import android.view.View;import com.etsy.android.grid.StaggeredGridView;import com.handmark.pulltorefresh.library.OverscrollHelper;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.R;@SuppressLint("NewApi")public class PullToRefreshStaggeredGridView extendsPullToRefreshBase<StaggeredGridView> {private static final OnRefreshListener<StaggeredGridView> defaultOnRefreshListener = new OnRefreshListener<StaggeredGridView>() {@Overridepublic void onRefresh(PullToRefreshBase<StaggeredGridView> refreshView) {}};public PullToRefreshStaggeredGridView(Context context) {super(context);/** * Added so that by default, Pull-to-Refresh refreshes the page */setOnRefreshListener(defaultOnRefreshListener);}public PullToRefreshStaggeredGridView(Context context, AttributeSet attrs) {super(context, attrs);/** * Added so that by default, Pull-to-Refresh refreshes the page */setOnRefreshListener(defaultOnRefreshListener);}public PullToRefreshStaggeredGridView(Context context, Mode mode) {super(context, mode);/** * Added so that by default, Pull-to-Refresh refreshes the page */setOnRefreshListener(defaultOnRefreshListener);}public PullToRefreshStaggeredGridView(Context context, Mode mode,AnimationStyle style) {super(context, mode, style);/** * Added so that by default, Pull-to-Refresh refreshes the page */setOnRefreshListener(defaultOnRefreshListener);}@Overridepublic final Orientation getPullToRefreshScrollDirection() {return Orientation.VERTICAL;}@Overrideprotected StaggeredGridView createRefreshableView(Context context,AttributeSet attrs) {StaggeredGridView gridView;if (VERSION.SDK_INT >= VERSION_CODES.GINGERBREAD) {gridView = new InternalStaggeredGridViewSDK9(context, attrs);} else {gridView = new StaggeredGridView(context, attrs);}gridView.setId(R.id.gridview);return gridView;}@Overrideprotected boolean isReadyForPullStart() {boolean result = false;View v = getRefreshableView().getChildAt(0);if (getRefreshableView().getFirstVisiblePosition() == 0) {if (v != null) {// getTop() and getBottom() are relative to the ListView,// so if getTop() is negative, it is not fully visibleboolean isTopFullyVisible = v.getTop() >= 0;result = isTopFullyVisible;}}return result;}@Overrideprotected boolean isReadyForPullEnd() {boolean result = false;int last = getRefreshableView().getChildCount() - 1;View v = getRefreshableView().getChildAt(last);int firstVisiblePosition = getRefreshableView().getFirstVisiblePosition();int visibleItemCount = getRefreshableView().getChildCount();int itemCount = getRefreshableView().getAdapter().getCount();if (firstVisiblePosition + visibleItemCount >= itemCount) {if (v != null) {boolean isLastFullyVisible = v.getBottom() <= getRefreshableView().getHeight();result = isLastFullyVisible;}}return result;}@Overrideprotected void onPtrRestoreInstanceState(Bundle savedInstanceState) {super.onPtrRestoreInstanceState(savedInstanceState);}@Overrideprotected void onPtrSaveInstanceState(Bundle saveState) {super.onPtrSaveInstanceState(saveState);}@TargetApi(9)final class InternalStaggeredGridViewSDK9 extends StaggeredGridView {// WebView doesn't always scroll back to it's edge so we add some// fuzzinessstatic final int OVERSCROLL_FUZZY_THRESHOLD = 2;// WebView seems quite reluctant to overscroll so we use the scale// factor to scale it's valuestatic final float OVERSCROLL_SCALE_FACTOR = 1.5f;public InternalStaggeredGridViewSDK9(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected boolean overScrollBy(int deltaX, int deltaY, int scrollX,int scrollY, int scrollRangeX, int scrollRangeY,int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {final boolean returnValue = super.overScrollBy(deltaX, deltaY,scrollX, scrollY, scrollRangeX, scrollRangeY,maxOverScrollX, maxOverScrollY, isTouchEvent);// Does all of the hard work...OverscrollHelper.overScrollBy(PullToRefreshStaggeredGridView.this,deltaX, scrollX, deltaY, getScrollRange(), isTouchEvent);return returnValue;}/** * Taken from the AOSP ScrollView source */private int getScrollRange() {int scrollRange = 0;if (getChildCount() > 0) {View child = getChildAt(0);scrollRange = Math.max(0, child.getHeight()- (getHeight() - getPaddingBottom() - getPaddingTop()));}return scrollRange;}}}
Then we customize the layout file and use the control we re-wrote
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFF" android:orientation="vertical" > <com.thomas.pulltorefreshstaggeredgridview.PullToRefreshStaggeredGridView android:id="@+id/pull_grid_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:background="#F0F0F0" android:cacheColorHint="#00000000" android:fadingEdge="none" android:overScrollMode="never" android:scrollbars="none" app:column_count="2" app:item_margin="8dp" /></LinearLayout>
Then we perform coding in activity
Import java. util. arrayList; import java. util. list; import android. annotation. suppressLint; import android. app. activity; import android. content. context; import android. OS. bundle; import com. etsy. android. grid. staggeredGridView; import com. handmark. pulltorefresh. library. pullToRefreshBase; import com. handmark. pulltorefresh. library. pullToRefreshBase. mode; import com. handmark. pulltorefresh. library. pullToRefreshBase. onRefreshListener; import com. nostra13.universalimageloader. cache. disc. naming. md5FileNameGenerator; import com. nostra13.universalimageloader. core. imageLoader; import com. nostra13.universalimageloader. core. imageLoaderConfiguration; import com. nostra13.universalimageloader. core. assist. queueProcessingType; @ SuppressLint ("NewApi") public class MainActivity extends Activity extends <StaggeredGridView> {private List <PhotoBean> photos; private StaggeredGridView mDongTaiGridView; private MyPhotoAdapter mAdapter; /** refresh **/private PullToRefreshStaggeredGridView mPullToRefreshStaggerdGridView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); initImageLoader (this); setContentView (R. layout. activity_main); photos = getPhotos (); mAdapter = new MyPhotoAdapter (this, photos); mPullToRefreshStaggerdGridView = (PullToRefreshStaggeredGridView) this. findViewById (R. id. pull_grid_view); mPullToRefreshStaggerdGridView. setMode (Mode. PULL_FROM_START); mPullToRefreshStaggerdGridView. setMode (Mode. BOTH); mPullToRefreshStaggerdGridView. setOnRefreshListener (this); mDongTaiGridView = mPullToRefreshStaggerdGridView. getRefreshableView (); mDongTaiGridView. setAdapter (mAdapter);} private List <PhotoBean> getPhotos () {List <PhotoBean> photos = new ArrayList <PhotoBean> (); PhotoBean = new PhotoBean ("http://img4q.duitang.com/uploads/item/201506/29/20150629140736_5JR8c.jpeg ", "Summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://img5q.duitang.com/uploads/item/201506/29/20150629141607_LNFkx.thumb.700_0.jpeg", "summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://img5q.duitang.com/uploads/item/201506/29/20150629141550_MuVm5.jpeg", "summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://img5q.duitang.com/uploads/item/201506/29/20150629141740_TUB2H.jpeg", "summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://img4q.duitang.com/uploads/item/201506/29/20150629141716_nCUrG.jpeg", "summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://cdnq.duitang.com/uploads/item/201506/29/20150629141656_5GBzS.jpeg", "summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://img4q.duitang.com/uploads/item/201506/29/20150629141634_CEida.jpeg", "summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://img5q.duitang.com/uploads/item/201506/29/20150629141236_sAHim.jpeg", "summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://img4q.duitang.com/uploads/item/201506/29/20150629141218_ezRd4.jpeg", "summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://cdnq.duitang.com/uploads/item/201506/29/20150629141200_nykzY.jpeg", "summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://img4q.duitang.com/uploads/item/201506/29/20150629141125_FCtVN.jpeg", "summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://img4q.duitang.com/uploads/item/201506/29/20150629141108_mBrUc.jpeg", "summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://cdnq.duitang.com/uploads/item/201506/29/20150629141052_cdFuA.thumb.700_0.jpeg", "summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://cdnq.duitang.com/uploads/item/201506/29/20150629141030_zv2Rt.jpeg", "summer wind"); photos. add (PhotoBean); PhotoBean = new PhotoBean ("http://img5q.duitang.com/uploads/item/201506/29/20150629140948_UMWTQ.jpeg", "summer wind"); photos. add (PhotoBean); return photos ;}@ Overridepublic void onRefresh (PullToRefreshBase <StaggeredGridView> refreshView) {// TODO Auto-generated method stubif (refreshView. isHeaderShown () {} else {} public void initImageLoader (Context context) {ImageLoaderConfiguration config = new ImageLoaderConfiguration. builder (context ). threadPriority (Thread. NORM_PRIORITY-2 ). denyCacheImageMultipleSizesInMemory (). discCacheFileNameGenerator (new Md5FileNameGenerator ()). tasksProcessingOrder (QueueProcessingType. LIFO ). writeDebugLogs () // Remove for release app. build (); ImageLoader. getInstance (). init (config );}}
Next, see:
If you still have any questions, go to my github homepage and download the complete code demo.
Click Open Link
I will further complete the project on github and hope to improve it with you.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.