通过转发触摸事件来单独对某个view进行控制处理。
<com.example.pinterestlistview.MyLinearLayout 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:id="@+id/mll" tools:context=".MainActivity" > <ListView android:id="@+id/lv2" android:scrollbars="none" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <ListView android:id="@+id/lv1" android:scrollbars="none" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <ListView android:id="@+id/lv3" android:scrollbars="none" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </com.example.pinterestlistview.MyLinearLayout>
dispatchTouchEvent 设置把触摸事件分发给那个view
package com.example.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); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return true; } @Override public boolean dispatchTouchEvent(MotionEvent ev) { return super.dispatchTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { //每个控件,也就是每个listView占据的大小 int width=getWidth()/getChildCount(); int height = getHeight(); //获取布局里面有多少个控件 这里则是有多少个listView int count=getChildCount(); //获取触摸时焦点的位置 float eventX = event.getX(); if (eventX<width){ // 滑动左边的 listView //不知道有什么用,但是一定得加,好像是没有鼠标位置是不可以的 event.setLocation(width/2, event.getY()); //把焦点事件分发给左边的listView getChildAt(0).dispatchTouchEvent(event); return true; } else if (eventX > width && eventX < 2 * width) { //滑动中间的 listView float eventY = event.getY(); //焦点在上不部分 三个listView都滑动 if (eventY < height / 2) { event.setLocation(width / 2, event.getY()); for (int i = 0; i < count; i++) { View child = getChildAt(i); try { //三个listView都分发到焦点 child.dispatchTouchEvent(event); } catch (Exception e) { e.printStackTrace(); } } return true; } else if (eventY > height / 2) { event.setLocation(width / 2, event.getY()); try { getChildAt(1).dispatchTouchEvent(event); } catch (Exception e) { e.printStackTrace(); } return true; } }else if (eventX>2*width){ event.setLocation(width/2, event.getY()); getChildAt(2).dispatchTouchEvent(event); return true; } return true; } }
MainActivity
package com.example.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 { private ListView lv1; private ListView lv2; private ListView lv3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv1 = (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 }; class MyAdapter1 extends BaseAdapter { @Override public int getCount() { return 3000; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView iv = (ImageView) View.inflate(getApplicationContext(), R.layout.lv_item, null); int resId = (int) (Math.random() * 4); iv.setImageResource(ids[resId]); return iv; } } }
时间: 2024-10-14 06:48:31