事件機制(經典案例+宅男福利),機制經典案例

來源:互聯網
上載者:User

事件機制(經典案例+宅男福利),機制經典案例

——————————————————如上————————————————

效果是:螢幕左中右都可以獨立的上下拖動,前提是在高度一半的下方,在螢幕上半部分向上拖動,是整體圖片向上拖動

activity_main.xml

<com.atguigu.pinterestlistview.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/mll"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <ListView        android:id="@+id/lv2"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"        android:scrollbars="none" />    <ListView        android:id="@+id/lv1"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"        android:scrollbars="none" />    <ListView        android: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="http://schemas.android.com/apk/res/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;}}


聯繫我們

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