android 瀑布流簡單例子

來源:互聯網
上載者:User

main.xml

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/arc_hf_search_result"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <LinearLayout        android:id="@+id/arc_hf_search_item"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >    </LinearLayout></ScrollView>

ArcHFSearchResult.java

public class ArcHFSearchResult extends Activity {    protected static final String TAG = "ArcHFSearchResult";    private ScrollView svResult;    private LinearLayout llItem;    private String[] arrayStr;    private int pageCount = 0;    private int resultCount = 10000;    private int eachCount = 3000;    private View view;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        svResult = (ScrollView) findViewById(R.id.arc_hf_search_result);        llItem = (LinearLayout) findViewById(R.id.arc_hf_search_item);        svResult.setOnTouchListener(svListener);        view = svResult.getChildAt(0);        // 將要顯示的10000條資料        arrayStr = new String[resultCount];        for (int i = 0; i < resultCount; i++) {            arrayStr[i] = i + "";        }        // 第一次添加資料,每次添加3000條。        AddResult();    }    class svTouchListener implements OnTouchListener {        @Override        public boolean onTouch(View v, MotionEvent event) {            switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                break;            case MotionEvent.ACTION_UP:                // 如果觸發監聽事件,並且有內容,並且ScrollView已經拉到底部,載入一次資料                if (svListener != null                        && view != null                        && view.getMeasuredHeight() - 20 <= svResult                                .getScrollY() + svResult.getHeight()) {                    AddResult();                }                break;            default:                break;            }            return false;        }    }    svTouchListener svListener = new svTouchListener();    /**     * 添加結果     */    protected void AddResult() {        if (eachCount * pageCount < resultCount) {            for (int i = 0; i < eachCount; i++) {                int k = i + eachCount * pageCount;                if (k >= resultCount)                    break;                TextView tv = new TextView(this);                tv.setText("hello world" + arrayStr[k]);                llItem.addView(tv);            }            pageCount++;        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.