自訂ViewGroup實現瀑布流效果,viewgroup瀑布

來源:互聯網
上載者:User

自訂ViewGroup實現瀑布流效果,viewgroup瀑布

今天情人節,我卻在家裡看書寫代碼,真屌絲啊哈~

我和好了4年多的女朋友也分了,心情很不好,一個人坐在屋裡好難過,這個假期的安排勢必要黃了。不過還好我是個能很好控制自己情緒的人,轉移轉移注意力,在家看看書寫寫代碼也挺好的。剛買了本程式員面試寶典,打算這個假期抽空看看。

回顧:ViewGroup的時間分發流程:

Android之View和ViewGroup事件分發

dispatchTouchEvent ----- onInterceptTouchEvent----- onTouchEvent

最外層的ViewGroup首先接收到觸摸事件,然後遍曆他的子View或者ViewGroup,將觸摸時間分發給包含觸摸位置的子View,繼續下去,直到該事件被消費(1.某個View的onTouchEvent返回了true;2.設定了監聽並返回了true。這樣該View的dispatchTouchEvent也就返回了true即事件被該View消費)onInterceptTouchEvent會攔截事件往下層傳遞,即中斷事件傳到子View,會執行自己的onTouchEvent。

下面的效果以前看到過,實現的思路挺不錯的,算是對事件分發這些知識的實戰吧。


在第一個listview裡面上下滑動,由第一個listview分發事件。

在第二個listview裡面上面滑動,三個listview均分發事件,實現一次觸摸的聯動效果。

在第二個listview裡面的下面上下滑動,由第二個listview分發事件。

在第三個listview裡面上下滑動,由第三個listview分發事件。

繼承LinearLayot,攔截觸摸事件,由自己重新分發。

public boolean onInterceptTouchEvent(MotionEvent ev) {return true;}public boolean onTouchEvent(MotionEvent event) {width = getWidth();eventX = (int) event.getX();childWidth = width / getChildCount();if (eventX < childWidth) {// 第一列的listviewevent.setLocation(childWidth/2, event.getY());getChildAt(0).dispatchTouchEvent(event);}else if (eventX >childWidth && eventX < 2*childWidth) {// 第二列的listviewevent.setLocation(childWidth/2, event.getY());if (event.getY() < getHeight()/2) {// 第二列的listview上面// 三個listview聯動for(int i = 0; i < getChildCount(); i++){getChildAt(i).dispatchTouchEvent(event);}}else {// 第二列的listview下面getChildAt(1).dispatchTouchEvent(event);}}else {//第三列listviewevent.setLocation(childWidth/2, event.getY());getChildAt(2).dispatchTouchEvent(event);}return super.onTouchEvent(event);}

布局檔案:

<com.example.day150214_pullstream.MyLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:clickable="true" ><ListView     android:id="@+id/lv1"    android:layout_height="match_parent"    android:layout_width="0dp"    android:layout_weight="1"    /><ListView     android:id="@+id/lv2"    android:layout_height="match_parent"    android:layout_width="0dp"    android:layout_weight="1"    /><ListView     android:id="@+id/lv3"    android:layout_height="match_parent"    android:layout_width="0dp"    android:layout_weight="1"    />    </com.example.day150214_pullstream.MyLayout>

MainActivity:

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initList();adapter = new SimpleAdapter(this, list, R.layout.item, new String[]{"iv"}, new int[]{R.id.iv});lv1 = (ListView) findViewById(R.id.lv1);lv2 = (ListView) findViewById(R.id.lv2);lv3 = (ListView) findViewById(R.id.lv3);lv1.setAdapter(adapter);lv2.setAdapter(adapter);lv3.setAdapter(adapter);}private void initList() {for (int i = 0; i < 20; i++) {HashMap<String, Object> map = new HashMap<String, Object>();map.put("iv", R.drawable.ic_launcher);list.add(map);}}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.