package text.com.gridlayoutdemo;
import android.animation.LayoutTransition;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.DragEvent;
import android.view.Gravity;
import android.view.View;
import android.widget.GridLayout;
import android.widget.TextView;
import java.util.List;
/**
* 定义出一个setitem方法动态传递标题数据
* 定义方法 setAllowDrag()控制是否可以拖拽
* Created by janzen on 2017/11/23.
*/
public class DrageGridiayout extends GridLayout {
private static final int columnCount = 4;
private boolean allowDrag;//是否可以拖拽
private Rect[] mRects;
private View dragedView;
public DrageGridiayout(Context context) {
this(context, null);
}
public DrageGridiayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DrageGridiayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
// android:animateLayoutChanges="true"
// android:columnCount="4"
this.setColumnCount(columnCount);
this.setLayoutTransition(new LayoutTransition());
}
public void setItems(List<String> items) {
for (String item : items) {
addItem(item);
}
}
private void addItem(String item) {
TextView textView = newItemView();
textView.setText(item);
this.addView(textView);
}
private TextView newItemView() {
TextView textView = new TextView(getContext());
textView.setBackgroundResource(R.drawable.selector_tv_bg);
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
int margin = 5;
layoutParams.width = getResources().getDisplayMetrics().widthPixels / 4 - 2 * margin;
layoutParams.height = GridLayout.LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(layoutParams);
layoutParams.setMargins(margin, margin, margin, margin);
textView.setGravity(Gravity.CENTER);
if (this.allowDrag) {
textView.setOnLongClickListener(olcl);
}else {
textView.setOnLongClickListener(null);
}
return textView;
}
public void setAllowDrag(boolean allowDrag) {
this.allowDrag = allowDrag;
if (this.allowDrag) {
this.setOnDragListener(odl);
}
}
View.OnLongClickListener olcl = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
dragedView = view;
view.startDrag(null, new View.DragShadowBuilder(view), null, 0);
view.setEnabled(false);
return false;
}
};
View.OnDragListener odl = new View.OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
switch (dragEvent.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
initRects();
break;
case DragEvent.ACTION_DRAG_LOCATION:
int touchIndex = getTouchIndex(dragEvent);
if (touchIndex > -1 && dragedView != null && dragedView != DrageGridiayout.this.getChildAt(touchIndex)) {
DrageGridiayout.this.removeView(dragedView);
DrageGridiayout.this.addView(dragedView, touchIndex);
}
break;
case DragEvent.ACTION_DRAG_ENDED:
if (dragedView != null)
dragedView.setEnabled(true);
break;
}
return true;
}
};
private void initRects() {
mRects = new Rect[this.getChildCount()];
for (int i = 0; i < this.getChildCount(); i++) {
View childView = this.getChildAt(i);
Rect rect = new Rect(childView.getLeft(), childView.getTop(), childView.getRight(), childView.getBottom());
mRects[i] = rect;
}
}
private int getTouchIndex(DragEvent dragEvent) {
for (int i = 0; i < mRects.length; i++) {
Rect rect = mRects[i];
if (rect.contains((int) dragEvent.getX(), (int) dragEvent.getY())) {
return i;
}
}
return -1;
}
}
自定义可拖拽GridLayout 类似于网易
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 如何通过自定义EditText来得到一个类似网易云音乐的带有清除功能的搜索框 运行效果: 先上代码,等会讲解: 自...