view拖扯代码:
学习笔记
public class MyGroup extends GridLayout {
public MyGroup(Context context) {
this(context, null);
init();
}
public MyGroup(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init();
}
public MyGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setColumnCount(4);
setLayoutTransition(new LayoutTransition());
}
public void addItem(List<String> strList) {
for (String str : strList) {
setTextItems(str);
}
}
//添加GridLayout里面的textView
int mMag = 10;
private View mDragView;
public void setTextItems(String str) {
TextView textView = new TextView(getContext());
textView.setText(str);//添加文字
textView.setBackgroundResource(R.drawable.shape);
int tv_width = getResources().getDisplayMetrics().widthPixels / getColumnCount() - mMag * 2;//获取屏幕
宽/列数
textView.setWidth(tv_width);
textView.setGravity(Gravity.CENTER);//文字居中
// textView.setSingleLine();//将文字设置成一行
//跑马灯效果
textView.setFocusable(true);
textView.setFocusableInTouchMode(true);
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.setMarqueeRepeatLimit(-1);
//控件和控件间隔10dp
GridLayout.LayoutParams params = new LayoutParams();
params.setMargins(mMag, mMag, mMag, mMag);
textView.setLayoutParams(params);
addView(textView);
textView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mDragView = v;
//参数1 :一个封装了参数的对象,参数二,用于创建手指一动的视图
// 参数三,当在一个activity中进行拖扯的时候,可以随意的传递一个对象,在监听方法中可以获取这
个对象
//参数四:控制拖放的标志,没有标志填0.
v.startDrag(null, new DragShadowBuilder(v), null, 0);
return true;
}
});
}
public void setIsDragabled(boolean falg) {
if (falg) {
//设置监听器
setOnDragListener(dragListener);
} else {
setOnDragListener(null);
}
}
private OnDragListener dragListener = new OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
initRects();
break;
//实时就监听a标签是否处于b标签的范围内
case DragEvent.ACTION_DRAG_LOCATION:
int index = getIndex(event);
if (index > -1 && mDragView != null && mDragView != getChildAt(index)) {
removeView(mDragView);
addView(mDragView, index);
}
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DROP:
break;
}
return true;
}
};
//i 被脱拉拽到的那个标签对应的索引值
private int getIndex(DragEvent event) {
for (int i = 0; i < mRectArr.length; i++) {
float x = event.getX();
float y = event.getY();
if (mRectArr[i].contains((int) x, (int) y)) {
return i;
}
}
//在其他范围
return -1;
}
private Rect[] mRectArr;
private void initRects() {
int childCount = getChildCount();
mRectArr = new Rect[childCount];
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);//根据每个条目的i
Rect rect = new Rect(view.getLeft(), view.getTop()
, view.getRight(), view.getBottom());
mRectArr[i] = rect;
}
}
}
了解的知识点:
1 setLayoutTransition(new LayoutTransition()); View的方法,这个方法的作用是:构建一个LayoutTransition
对象,这个对象会监听ViewGroup layout中的事件,并且会随之运行默认动画为每一种类型layout变化的事
件。
2 一般我们进行拖动都是给要拖动的view设置长点击事件。
3 拖动的核心方法是: v.startDrag(null, new DragShadowBuilder(v), null, 0);
startDrag有四个参数,含义参考如下:
https://blog.csdn.net/Hubert_bing/article/details/79499654
网友评论