1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android 自定义控件linearlayout 自定义控件(瀑布流 LinearLayout)

android 自定义控件linearlayout 自定义控件(瀑布流 LinearLayout)

时间:2020-09-06 06:26:22

相关推荐

android 自定义控件linearlayout 自定义控件(瀑布流 LinearLayout)

通过转发触摸事件来单独对某个view进行控制处理。

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/mll"

tools:context=".MainActivity" >

android:id="@+id/lv2"

android:scrollbars="none"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1" />

android:id="@+id/lv1"

android:scrollbars="none"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1" />

android:id="@+id/lv3"

android:scrollbars="none"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1" />

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

//不知道有什么用,但是一定得加,好像是没有鼠标位置是不可以的

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;

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。