——————————————————效果图如上————————————————
效果是:屏幕左中右都可以独立的上下拖动,前提是在高度一半的下方,在屏幕上半部分向上拖动,是整体图片向上拖动
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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取listview 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,R.drawable.girl4 }; 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); // 0<=Math.random() * 5<5 int 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,事件将会传给孩子 */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { //导致ontouch事件执行 return true; } @Override public boolean onTouchEvent(MotionEvent event) { // 屏幕的三分之一 int width = getWidth() / getChildCount(); int height = getHeight(); int count = getChildCount(); //得到x坐标值 float eventX = event.getX(); // 滑动左边的 listView if (eventX < width) { //设置有效区间 event.setLocation(width / 2, event.getY()); //分发事件给listview getChildAt(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; } }
时间: 2024-10-12 02:54:29