Custom ViewGroup for waterfall flow effect, viewgroup waterfall

Source: Internet
Author: User
Tags gety

Custom ViewGroup for waterfall flow effect, viewgroup waterfall

Today's Valentine's Day, I read books and write code at home ~

I have been away with my girlfriend for more than four years, and I am in a bad mood. It is so sad to sit alone in the house that the holiday schedule will be yellow. But fortunately, I am a person who can control my emotions well. It's also good to shift my attention and read and write code at home. I just bought a programmer's interview book and plan to take a look at it this holiday.

Review: The time distribution process of ViewGroup:

View and ViewGroup event distribution for Android

DispatchTouchEvent ----- onInterceptTouchEvent ----- onTouchEvent

The ViewGroup on the outermost layer first receives the touch event, then traverses its child View or ViewGroup, distributes the touch time to the Child View containing the touch location, and continues, until the event is consumed (1. the onTouchEvent of a View returns true; 2. set the listener and return true. In this way, the dispatchTouchEvent of the View will return true, that is, the event is consumed by the View.) onInterceptTouchEvent will intercept the event to be passed to the next layer, that is, the interruption event will be passed to the subview and the onTouchEvent.

The following results have been seen before, and the Implementation idea is quite good. It is a practical practice for event distribution.


Slide up and down in the first listview and distribute the event by the first listview.

Slide on the second listview, and all three listview distribute events to achieve one-touch interaction.

Slide up and down under the second listview and distribute the event by the second listview.

Slide up and down in the third listview and distribute the event by the third listview.

Inherit LinearLayot, intercept touch events, and resend by yourself.

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 In the first column. setLocation (childWidth/2, event. getY (); getChildAt (0 ). dispatchTouchEvent (event);} else if (eventX> childWidth & eventX <2 * childWidth) {// listviewevent in the second column. setLocation (childWidth/2, event. getY (); if (event. getY () <getHeight ()/2) {// The Top Of The listview in the second column // three listview links for (int I = 0; I <getChildCount (); I ++) {getChildAt (I ). dispatchTouchEvent (event) ;}} getChildAt (1) Under the listview in the second column of else ). dispatchTouchEvent (event) ;}} else {// The third column listviewevent. setLocation (childWidth/2, event. getY (); getChildAt (2 ). dispatchTouchEvent (event);} return super. onTouchEvent (event );}

Layout file:

<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);}}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.