前言
根据启舰大大 的博客所学习的滑动删除。
scrollBy子控件在现有位置的基础上一次次移动
scrollBy(int x, int y):子控件往左(上)移动
- x(y) :为正时,
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main"
>
<RelativeLayout
android:layout_centerInParent="true"
android:background="@color/colorAccent"
android:id="@+id/redSection"
android:layout_width="300dp"
android:layout_height="300dp"
>
<RelativeLayout
android:layout_centerInParent="true"
android:id="@+id/greensection"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#00ff00"
>
<TextView
android:layout_centerInParent="true"
android:id="@+id/txt"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:background="@color/colorPrimaryDark"
android:text="scrollBy"
android:textSize="20dp"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
点击哪部分,哪部分调用scrollby
final View root = findViewById(R.id.activity_main);
final View redSection = findViewById(R.id.redSection);
final View greenSection = findViewById(R.id.greensection);
final View txt = findViewById(R.id.txt);
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txt.scrollBy(10, 10);
}
});
greenSection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
greenSection.scrollBy(10, 10);
}
});
redSection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
redSection.scrollBy(10, 10);
}
});
root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
root.scrollBy(10, 10);
}
});
效果如下:
scrollyBy效果图
scrollTo:子控件直接移动到目标位置
将上述代码改为scrollTo
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txt.scrollTo(10, 10);
}
});
greenSection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
greenSection.scrollTo(10, 10);
}
});
redSection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
redSection.scrollTo(10, 10);
}
});
root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
root.scrollTo(10, 10);
}
});
效果如下:
scrollTo效果做个简易的小左滑,后续会逐渐完善的
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:id="@+id/itemroot"
>
<TextView
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="ITEM"
/>
<TextView
android:id="@+id/del"
android:layout_width="60dp"
android:layout_height="100dp"
android:background="@color/colorPrimaryDark"
android:gravity="center"
android:text="删除"
/>
</LinearLayout>
itemRoot = findViewById(R.id.itemroot);
del = findViewById(R.id.del);
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "删除", Toast.LENGTH_SHORT).show();
}
});
itemRoot.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int scrollX = itemRoot.getScrollX();
int width = del.getMeasuredWidth();
if (event.getAction() == MotionEvent.ACTION_MOVE) {
//getScrollX()返回的就是子控件相对于初始位置偏移量
int newScrollX = (int) (scrollX + lastX - event.getX());
if (newScrollX < 0)
newScrollX = 0;
else if (newScrollX > width) {
newScrollX = width;
}
itemRoot.scrollTo(newScrollX, 0);
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
if (scrollX > width / 2) {
//偏移大于一半距离
itemRoot.scrollTo(width, 0);
}
else
//偏移小于一半距离
itemRoot.scrollTo(0, 0);
}
lastX = (int) event.getX();
return true;
}
});
网友评论