自定义ViewGroup实现瀑布流效果
来源:互联网 发布:linux下安装php环境 编辑:程序博客网 时间:2024/06/10 05:07
今天情人节,我却在家里看书写代码,真屌丝啊哈~
回顾: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);}}
3 0
- 自定义ViewGroup实现瀑布流效果
- 自定义ViewGroup实现瀑布流样式
- 自定义UICollectionViewLayout—实现瀑布流效果
- 自定义实现带文字标题的瀑布流效果
- 自定义ViewGroup实现进度动态更新效果
- 自定义ViewGroup实现标签云效果。
- 自定义ViewGroup实现ViewPager的滑动效果
- 自定义ViewGroup实现弹性滑动效果
- 自定义ViewGroup实现随机泡泡效果
- 自定义viewGroup实现点击动画效果
- 自定义 ViewGroup 实现子 View 层叠效果
- 自定义ViewGroup实现左滑效果
- android 瀑布流实现效果
- 瀑布流效果怎么实现
- staggeredGridView 实现瀑布流效果
- RecycleView 实现瀑布流效果
- JavaScript实现瀑布流效果
- RecyclerView 实现瀑布流效果
- hdu 3065 病毒侵袭持续中
- ui
- java设计模式之---工厂模式
- 南洋理工OJ——24 素数距离问题
- Python:在指定目录下查找满足条件的文件
- 自定义ViewGroup实现瀑布流效果
- iOS 使用levelDB储存本地数据
- C++面向对象方法求解约瑟夫环问题
- 数据库连接池
- ui.layout
- ui.ImageView及其子类
- Java多个线程之间处理共享数据的方式
- 算法导论:动态规划
- ZOJ Problem Set - 3811 Untrusted Patrol