首先我們還是來看一款樣本:(蘑菇街)
看起來很像我們的gridview吧,不過又不像,因為item大小不固定的,看起來是不是別有一番風味,確實如此.就如我們的方角圖形,斯通見慣後也就出現了圓角.下面我簡單介紹下實現方法.
第一種:
我們在設定檔中定義好列數.如也就是3列.我們需要定義三個LinearLayout,然後把擷取到的圖片add裡面就ok了.
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/background_light" android:orientation="vertical" > <include android:id="@+id/progressbar" layout="@layout/loading" /> <com.jj.waterfall.LazyScrollView android:id="@+id/lazyscrollview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:scrollbars="@null" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/background_light" android:orientation="horizontal" android:padding="2dp" > <LinearLayout android:id="@+id/layout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/layout02" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/layout03" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > </LinearLayout> </LinearLayout> </com.jj.waterfall.LazyScrollView> <TextView android:id="@+id/loadtext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/loading_bg" android:gravity="center" android:padding="10dp" android:text="Loading..." android:textColor="@android:color/background_dark" /></LinearLayout>
在這裡因為圖片很多就把圖片放在assets檔案中,如果想從網上拉取資料,自己寫額外部分.
@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);InitView();assetManager = this.getAssets();// 擷取顯示圖片寬度Image_width = (getWindowManager().getDefaultDisplay().getWidth() - 4) / 3;try {image_filenames = Arrays.asList(assetManager.list("images"));// 擷取圖片名稱} catch (IOException e) {e.printStackTrace();}addImage(current_page, count);}
/*** * 載入更多 * * @param current_page * 當前頁數 * @param count * 每頁顯示個數 */private void addImage(int current_page, int count) {for (int x = current_page * count; x < (current_page + 1) * count&& x < image_filenames.size(); x++) {addBitMapToImage(image_filenames.get(x), y, x);y++;if (y >= 3)y = 0;}}
/*** * 添加imageview 到layout * * @param imagePath 圖片name * @param j 列 * @param i 行 */public void addBitMapToImage(String imagePath, int j, int i) {ImageView imageView = getImageview();asyncTask = new ImageDownLoadAsyncTask(this, imagePath, imageView,Image_width);asyncTask.setProgressbar(progressbar);asyncTask.setLoadtext(loadtext);asyncTask.execute();imageView.setTag(i);if (j == 0) {layout01.addView(imageView);} else if (j == 1) {layout02.addView(imageView);} else if (j == 2) {layout03.addView(imageView);}imageView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this,"您點擊了" + v.getTag() + "個Item", Toast.LENGTH_SHORT).show();}});}
注釋已經很明確,相信大家都看的明白,我就不過多解釋了.
因為瀑布流不是一個規則的試圖,所以我們不可能用listview那種“底部加一個按鈕試圖,點擊載入更多,這樣看起來很難看”。因此我們最好滑動到低端自動載入.
我們這裡用到的自訂ScrollView,因為我們要實現下滑分頁,這裡要判斷是否要進行分頁等操作.
LazyScrollView.java (這個法很實用哦.)
/*** * 自訂ScrollView * * @author zhangjia * */public class LazyScrollView extends ScrollView {private static final String tag = "LazyScrollView";private Handler handler;private View view;public LazyScrollView(Context context) {super(context);}public LazyScrollView(Context context, AttributeSet attrs) {super(context, attrs);}public LazyScrollView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}// 這個獲得總的高度public int computeVerticalScrollRange() {return super.computeHorizontalScrollRange();}public int computeVerticalScrollOffset() {return super.computeVerticalScrollOffset();}/*** * 初始化 */private void init() {this.setOnTouchListener(onTouchListener);handler = new Handler() {@Overridepublic void handleMessage(Message msg) {// process incoming messages heresuper.handleMessage(msg);switch (msg.what) {case 1:if (view.getMeasuredHeight() <= getScrollY() + getHeight()) {if (onScrollListener != null) {onScrollListener.onBottom();}} else if (getScrollY() == 0) {if (onScrollListener != null) {onScrollListener.onTop();}} else {if (onScrollListener != null) {onScrollListener.onScroll();}}break;default:break;}}};}OnTouchListener onTouchListener = new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {case MotionEvent.ACTION_DOWN:break;case MotionEvent.ACTION_UP:if (view != null && onScrollListener != null) {handler.sendMessageDelayed(handler.obtainMessage(1), 200);}break;default:break;}return false;}};/** * 獲得參考的View,主要是為了獲得它的MeasuredHeight,然後和捲軸的ScrollY+getHeight作比較。 */public void getView() {this.view = getChildAt(0);if (view != null) {init();}}/** * 定義介面 * * @author admin * */public interface OnScrollListener {void onBottom();void onTop();void onScroll();}private OnScrollListener onScrollListener;public void setOnScrollListener(OnScrollListener onScrollListener) {this.onScrollListener = onScrollListener;}
我們還需要一個類,非同步載入實現,我想有開發經驗的朋友一定用過好多次了,這裡就不展示代碼了,想看的朋友,可以點擊下載(如果認為還不錯的話,請您一定要表示一下哦.)
對了,忘記一點,我們還需要對MainActivity 中的lazyScrollView實現OnScrollListener介面,對滑動到底部進行監聽.
:
/**************************************************************************/
下面我介紹另外一種做法:(相對上面更靈活)
我們動態添加列.
設定檔就不貼了,和上面那例子一樣,只不過裡面值包含一個LinearLayout布局.
在這裡我們動態添加列布局.
/*** * init view */public void initView() {setContentView(R.layout.main);lazyScrollView = (LazyScrollView) findViewById(R.id.waterfall_scroll);lazyScrollView.getView();lazyScrollView.setOnScrollListener(this);waterfall_container = (LinearLayout) findViewById(R.id.waterfall_container);progressbar = (LinearLayout) findViewById(R.id.progressbar);loadtext = (TextView) findViewById(R.id.loadtext);item_width = getWindowManager().getDefaultDisplay().getWidth() / column;linearLayouts = new ArrayList<LinearLayout>();// 添加列到waterfall_containerfor (int i = 0; i < column; i++) {LinearLayout layout = new LinearLayout(this);LinearLayout.LayoutParams itemParam = new LinearLayout.LayoutParams(item_width, LayoutParams.WRAP_CONTENT);layout.setOrientation(LinearLayout.VERTICAL);layout.setLayoutParams(itemParam);linearLayouts.add(layout);waterfall_container.addView(layout);}}
/*** * 擷取imageview * * @param imageName * @return */public ImageView getImageview(String imageName) {BitmapFactory.Options options = getBitmapBounds(imageName);// 建立顯示圖片的對象ImageView imageView = new ImageView(this);LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);imageView.setLayoutParams(layoutParams);//imageView.setMinimumHeight(options.outHeight);imageView.setMinimumWidth(options.outWidth);imageView.setPadding(2, 0, 2, 2);imageView.setBackgroundResource(R.drawable.image_border);if (options != null)options = null;return imageView;}/*** * * 擷取相應圖片的 BitmapFactory.Options */public BitmapFactory.Options getBitmapBounds(String imageName) {int h, w;BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;// 只返回bitmap的大小,可以減少記憶體使用量,防止OOM.InputStream is = null;try {is = assetManager.open(file + "/" + imageName);} catch (IOException e) {e.printStackTrace();}BitmapFactory.decodeStream(is, null, options);return options;}
在這裡我稍微修改了下,為要顯示的iamgeview添加一個邊框,這樣看起來效果不錯,我們動態滑動的同時, 然後圖片陸續的填充邊框.蘑菇街就是這種效果哦.
:
顯示成4列,因此圖片有點小,仔細看的話,你應該可以看到有好多邊框,然後圖片陸續的填充邊框.這種效果感覺對上面那個使用者體驗更友好些.
最後簡單總結下:針對瀑布流最好使用第二種方法,這種可擴充性比較大,哪天老大說四列太醜了,改成三列,那麼我們只需要把column改成3就ok了,簡單吧。
注意:由於圖片量太多,佔用空間太大,因此我將圖片上傳到網上,擷取源碼的同學下載該檔案放到項目的assets檔案夾下,然後運行就ok了.
如果有不足之處,請留言指出,
想要源碼請留郵箱.Thanks for you 。
由於比較繁忙,我將源碼上傳網上,如有需要,自行下載,如有問題,請留言.(記得下載圖片匯入項目裡面)
圖片下載
樣本二源碼(第一種方式)
樣本一源碼(第二種方式)
哈哈,如果對您又協助的話,記得贊一個哦.