事件机制(经典案例+宅男福利)

——————————————————效果图如上————————————————

效果是:屏幕左中右都可以独立的上下拖动,前提是在高度一半的下方,,在屏幕上半部分向上拖动,是整体图片向上拖动

activity_main.xml

<com.atguigu.pinterestlistview.MyLinearLayout xmlns:android=""xmlns:tools=""android:id="@+id/mll"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><ListViewandroid:id="@+id/lv2"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:scrollbars="none" /><ListViewandroid:id="@+id/lv1"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:scrollbars="none" /><ListViewandroid:id="@+id/lv3"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:scrollbars="none" /></com.atguigu.pinterestlistview.MyLinearLayout>lv_item.xml

<ImageView xmlns:android=""android:id="@+id/iv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:adjustViewBounds="true"android:src="@drawable/default1" />MainActivity.java

package com.atguigu.pinterestlistview;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;public class MainActivity extends Activity {/* * 定义三个listview */private ListView lv1;private ListView lv2;private ListView lv3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取listviewlv1 = (ListView) findViewById(R.id.lv1);lv2 = (ListView) findViewById(R.id.lv2);lv3 = (ListView) findViewById(R.id.lv3);//设置适配器try {lv1.setAdapter(new MyAdapter1());lv2.setAdapter(new MyAdapter1());lv3.setAdapter(new MyAdapter1());} catch (Exception e) {e.printStackTrace();}}//准备需要的图片资源private int ids[] = new int[] { R.drawable.default1, R.drawable.girl1,R.drawable.girl2, R.drawable.girl3,R.drawable.girl4 };class MyAdapter1 extends BaseAdapter {@Overridepublic int getCount() {return 3000;}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ImageView iv = (ImageView) View.inflate(getApplicationContext(),R.layout.lv_item, null);// 0<=Math.random() * 5<5int resId = (int) (Math.random() * 5);iv.setImageResource(ids[resId]);return iv;}}}MyLinearLayout.java

package com.atguigu.pinterestlistview;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.LinearLayout;public class MyLinearLayout extends LinearLayout {/** * 第一步执行构造器 */public MyLinearLayout(Context context, AttributeSet attrs) {super(context, attrs);}/** * onInterceptTouchEvent();拦截触摸事件 1.如果返回的是true,将会触发当前View的onTouchEvent(); * 2.如果返回的是false,事件将会传给孩子 */@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {//导致ontouch事件执行return true;}@Overridepublic boolean onTouchEvent(MotionEvent event) {// 屏幕的三分之一int width = getWidth() / getChildCount();int height = getHeight();int count = getChildCount();//得到x坐标值float eventX = event.getX();// 滑动左边的 listViewif (eventX < width) {//设置有效区间event.setLocation(width / 2, event.getY());//分发事件给listviewgetChildAt(0).dispatchTouchEvent(event);//返回true,说明list处理事件return true;} else if (eventX > width && eventX < 2 * width) { // 滑动中间的 listView//得到中间listview的点击y值float eventY = event.getY();//如果在屏幕的1/2之上(屏幕上半部分)if (eventY < height / 2) {event.setLocation(width / 2, event.getY());for (int i = 0; i < count; i++) {//获取三个listview视图View child = getChildAt(i);try {child.dispatchTouchEvent(event);} catch (Exception e) {e.printStackTrace();}}//返回true,说明list处理事件return true;} else if (eventY > height / 2) {event.setLocation(width / 2, event.getY());getChildAt(1).dispatchTouchEvent(event);return true;}} else if (eventX > 2 * width) {event.setLocation(width / 2, event.getY());getChildAt(2).dispatchTouchEvent(event);return true;}return true;}}

偶尔会想,如果人生真如一场电子游戏,

事件机制(经典案例+宅男福利)

相关文章:

你感兴趣的文章:

标签云: