我們還是來看一款示例:(蘑菇街)
看起來很像我們的gridview吧,不過又不像,因為item大小不固定的,看起來是不是别有一番風味,确實如此.就如我們的方角圖形,斯通見慣後也就出現了圓角.下面我簡單介紹下實作方法.
第一種:
我們在配置檔案中定義好列數.如上圖也就是3列.我們需要定義三個LinearLayout,然後把擷取到的圖檔add裡面就ok了.
main.xml
[java] view plain copy
- <?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檔案中,如果想從網上拉取資料,自己寫額外部分.
[java] view plain copy
- @Override
- public 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);
- }
[java] view plain copy
- 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;
- }
- }
[java] view plain copy
- 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() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MainActivity.this,
- "您點選了" + v.getTag() + "個Item", Toast.LENGTH_SHORT)
- .show();
- }
- });
- }
注釋已經很明确,相信大家都看的明白,我就不過多解釋了.
因為瀑布流不是一個規則的試圖,是以我們不可能用listview那種“底部加一個按鈕試圖,點選加載更多,這樣看起來很難看”。是以我們最好滑動到低端自動加載.
我們這裡用到的自定義ScrollView,因為我們要實作下滑分頁,這裡要判斷是否要進行分頁等操作.
LazyScrollView.java (這個法很實用哦.)
[java] view plain copy
- 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() {
- @Override
- public void handleMessage(Message msg) {
- // process incoming messages here
- super.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() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- switch (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;
- }
- };
- public void getView() {
- this.view = getChildAt(0);
- if (view != null) {
- init();
- }
- }
- public interface OnScrollListener {
- void onBottom();
- void onTop();
- void onScroll();
- }
- private OnScrollListener onScrollListener;
- public void setOnScrollListener(OnScrollListener onScrollListener) {
- this.onScrollListener = onScrollListener;
- }
我們還需要一個類, 異步加載實作 ,我想有開發經驗的朋友一定用過好多次了,這裡就不展示代碼了,想看的朋友,可以點選下載下傳( 如果認為還不錯的話,請您一定要表示一下哦. )
對了,忘記一點,我們還需要對MainActivity 中的lazyScrollView實作OnScrollListener接口,對滑動到底部進行監聽.
效果圖:
下面我介紹另外一種做法:(相對上面更靈活)
我們動态添加列.
配置檔案就不貼了,和上面那例子一樣,隻不過裡面值包含一個LinearLayout布局.
在這裡我們動态添加列布局.
[java] view plain copy
- 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_container
- for (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);
- }
- }
[java] view plain copy
- 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;
- }
- 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 。
由于比較繁忙,我将源碼上傳網上,如有需要,自行下載下傳,如有問題,請留言.(記得下載下傳圖檔導入項目裡面)
圖檔下載下傳
示例二源碼(第一種方式)
示例一源碼(第二種方式)
哈哈,如果對您又幫助的話,記得贊一個哦