Skip to content

Commit

Permalink
DragLayout增加maxScrollTime(xml)和touchScrollable属性
Browse files Browse the repository at this point in the history
  • Loading branch information
Mosect committed Aug 29, 2019
1 parent 54579de commit 67a5df2
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 19 deletions.
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,15 @@ Android拖拽布局,包括以下布局:
# 使用:
项目已上传jcenter,可以直接使用maven或gradle引用本库。

## Maven
```
<dependency>
<groupId>com.mosect</groupId>
<artifactId>DragLayout</artifactId>
<version>1.0.3</version>
<type>pom</type>
</dependency>
```

## Gradle
```
implementation 'com.mosect:DragLayout:1.0.3'
implementation 'com.mosect:ViewUtils:1.0.5'
implementation 'com.mosect:DragLayout:1.0.4'
```

# 更新记录
## 1.0.4
* 增加maxScrollTime(xml)
* 增加touchScrollable属性,表示是否可以触摸滑动视图
## 1.0.3
* 修复上次滑动未完成,下次直接打开周边出现瞬间闪屏的问题
* DragLayout直接继承ViewGroup,不继承FrameLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class ListItem implements Serializable, Cloneable {

private String title;
private String info;
private boolean touchScrollable; // 是否可以触摸滑动

@Override
public ListItem clone() {
Expand All @@ -31,4 +32,12 @@ public String getInfo() {
public void setInfo(String info) {
this.info = info;
}

public boolean isTouchScrollable() {
return touchScrollable;
}

public void setTouchScrollable(boolean touchScrollable) {
this.touchScrollable = touchScrollable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void onClick(View v) {
ListItem item = new ListItem();
item.setTitle(getString(R.string.title) + (i + 1));
item.setInfo(getString(R.string.info));
item.setTouchScrollable(i % 2 == 0);
items.add(item);
}
adapter2.addAll(items);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public class ListAdapter extends RecyclerView.Adapter<ItemHolder> {

private List<ListItem> data = new ArrayList<>();
protected List<ListItem> data = new ArrayList<>();

@NonNull
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void onClick(View v) {
public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
super.onBindViewHolder(holder, position);
DragLayout dragLayout = holder.itemView.findViewById(R.id.ly_drag);
dragLayout.setTouchScrollable(data.get(position).isTouchScrollable());
dragLayout.closeEdge(false); // 关闭周边
}
}
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ publish {
userOrg = 'mosect' // bintray.com用户名
groupId = 'com.mosect' // jcenter上的路径,bintray上创建Package时填写的Version control项
artifactId = 'DragLayout' // 项目名称,bintray上创建Package时填写的Name项
publishVersion = '1.0.3' // 版本号
desc = 'Android拖拽控件' // 描述,不重要
publishVersion = '1.0.4' // 版本号
desc = 'DragLayout增加maxScrollTime(xml)和touchScrollable属性' // 描述,不重要
website = 'http://www.mosect.com' // 网站,最好有,不重要
}

Expand Down
52 changes: 48 additions & 4 deletions library/src/main/java/com/mosect/draglayout/lib/DragLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public class DragLayout extends ViewGroup {
* 的速度,此速度默认为0,即关闭最大时间限制
*/
private int maxScrollTime;
/**
* 是否开启触摸滑动,默认开启
*/
private boolean touchScrollable;
private LinkedList<Runnable> afterLayoutRunnableList;
private boolean attached = false; // 是否添加到窗口系统

Expand Down Expand Up @@ -109,11 +113,14 @@ public DragLayout(Context context, AttributeSet attrs, int defStyleAttr, int def
private void init(AttributeSet attrs) {
scrollVelocity = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
DEFAULT_SCROLL_VELOCITY_DP, getContext().getResources().getDisplayMetrics());
touchScrollable = true;
if (null != attrs) {
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.DragLayout);
overScroll = ta.getInt(R.styleable.DragLayout_overScroll, OVER_SCROLL_ALL);
scrollVelocity = ta.getDimensionPixelSize(
R.styleable.DragLayout_scrollVelocity, scrollVelocity);
maxScrollTime = ta.getInt(R.styleable.DragLayout_maxScrollTime, 0);
touchScrollable = ta.getBoolean(R.styleable.DragLayout_touchScrollable, touchScrollable);
ta.recycle();
}
gestureHelper = GestureHelper.createDefault(getContext());
Expand Down Expand Up @@ -248,7 +255,9 @@ public boolean onTouchEvent(MotionEvent event) {
dy = getVerticalLayerScrollMax();
}
}
layerScrollTo(getLayerScrollX(), dy);
if (touchScrollable) {
layerScrollTo(getLayerScrollX(), dy);
}

// 处理停止触摸情况
if (event.getAction() == MotionEvent.ACTION_UP ||
Expand Down Expand Up @@ -300,7 +309,9 @@ public boolean onTouchEvent(MotionEvent event) {
dx = getHorizontalLayerScrollMax();
}
}
layerScrollTo(dx, getScrollY());
if (touchScrollable) {
layerScrollTo(dx, getScrollY());
}

// 处理停止触摸情况
if (event.getAction() == MotionEvent.ACTION_UP ||
Expand Down Expand Up @@ -490,7 +501,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
*/
protected void onLayoutChildren() {
// 布局子View
System.out.println("onLayoutChildren==================");
// System.out.println("onLayoutChildren==================");
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child.getVisibility() == GONE) continue;
Expand Down Expand Up @@ -542,7 +553,7 @@ protected void onLayoutChildren() {

// 根据重力倾向,计算出视图位置
Gravity.apply(gravity, widthSpace, heightSpace, containerRect, outRect);
System.out.println(String.format("Gravity.apply:%s,%s", containerRect, outRect));
// System.out.println(String.format("Gravity.apply:%s,%s", containerRect, outRect));
outRect.left += lp.leftMargin;
outRect.top += lp.topMargin;
outRect.right -= lp.rightMargin;
Expand Down Expand Up @@ -1202,14 +1213,47 @@ private void releaseTouch() {
}
}

/**
* 获取最大滑动时间
*
* @return 最大滑动时间
* @see #setMaxScrollTime(int)
*/
public int getMaxScrollTime() {
return maxScrollTime;
}

/**
* 设置最大的滑动时间:如果smooth到目标位置需要的时间大于此时间,则以maxScrollTime时间到达目标
* 的速度滑动;如果此值小于等于0,不生效
*
* @param maxScrollTime 最大滑动时间
* @see #getMaxScrollTime()
*/
public void setMaxScrollTime(int maxScrollTime) {
this.maxScrollTime = maxScrollTime;
}

/**
* 设置是否触摸滑动
*
* @param touchScrollable 是否开启触摸滑动:true,触摸导致视图进行滑动;false,触摸不会导致视图滑动
* @see #isTouchScrollable()
*/
public void setTouchScrollable(boolean touchScrollable) {
this.touchScrollable = touchScrollable;
}

/**
* 判断是否开启触摸滑动
*
* @return true,触摸导致视图进行滑动;false,触摸不会导致视图滑动;默认为true
* @see #setTouchScrollable(boolean)
*/
public boolean isTouchScrollable() {
return touchScrollable;
}

public static class LayoutParams extends MarginLayoutParams {

/**
Expand Down
6 changes: 6 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DragLayout">
<!--是否可以滑动超过四周-->
<attr name="overScroll" format="flags">
<flag name="none" value="0" />
<flag name="left" value="1" />
Expand All @@ -9,7 +10,12 @@
<flag name="bottom" value="8" />
<flag name="all" value="15" />
</attr>
<!--滑动速度(smooth)-->
<attr name="scrollVelocity" format="dimension|reference" />
<!--最大的滑动时间-->
<attr name="maxScrollTime" format="integer|reference" />
<!--是否开启触摸滑动-->
<attr name="touchScrollable" format="boolean|reference" />
</declare-styleable>
<declare-styleable name="DragLayout_Layout">
<attr name="layout_layer" format="enum">
Expand Down

0 comments on commit 67a5df2

Please sign in to comment.