gpt4 book ai didi

android - 在 View 上拖动时选择叠加

转载 作者:行者123 更新时间:2023-11-30 02:14:47 25 4
gpt4 key购买 nike

我正在尝试构建一个类似于日历议程 View (用户可以通过拖动选择小时的垂直列表)的 View ,但我卡在了用户开始按下屏幕并拖动的部分他将手指放在小时行上以选择它们。如何在代表小时的 View 上覆盖代表选择的内容,并在用户拖动选择时扩展/缩小它?

我当前的界面由一个垂直方向的 LinearLayout 组成,其中包含空的 TextViews 作为占位符,代表一天中的几个小时。

我的屏幕是这样的:

enter image description here

每个绿色列都是 LinearLayout 和代表插槽的 TextViews。我想让用户开始按下其中一个插槽并向下拖动以进行选择,并且在拖动过程中有一个覆盖层可以缩小/扩展以反射(reflect)选择。

最佳答案

我已经设法通过扩展 LinearLayout 并像这样连接到绘图机制来解决它:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class AvailabilityCalendarColumnLayout extends LinearLayout{

private Drawable overlay;

public AvailabilityCalendarColumnLayout(Context context) {
super(context);
setWillNotDraw(false);
}

public AvailabilityCalendarColumnLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
setWillNotDraw(false);
}

public AvailabilityCalendarColumnLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setWillNotDraw(false);
}

@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if(overlay != null) {
overlay.draw(canvas);
}
}

public boolean startSelectionOverlay(View startingView) {
if(startingView == null) {
return false;
}
overlay = getResources().getDrawable(R.drawable.overlay);
float[] l = new float[2];
l[0] = startingView.getX();
l[1] = startingView.getY();
int w = startingView.getWidth();
int h = startingView.getHeight();
overlay.setBounds((int) l[0], (int) l[1], (int) l[0] + w, (int) l[1] + h);
invalidate();
return true;
}

private void extendSelectionOverlay(View endView) {
if(endView == null) {
return;
}
float[] l = new float[2];
l[0] = endView.getX();
l[1] = endView.getY();
int w = endView.getWidth();
int h = endView.getHeight();

Rect r = overlay.getBounds();
r.bottom = (int)l[1] + h;
overlay.setBounds(r);
invalidate();
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = MotionEventCompat.getActionMasked(ev);
float[] pos = new float[2];
pos[0] = ev.getX();
pos[1] = ev.getY();

if(action == MotionEvent.ACTION_DOWN && overlay == null) {
View view = getChildViewUnderPosition(pos);
if(view != null) {
startSelectionOverlay(view);
}
}

if(action == MotionEvent.ACTION_MOVE && overlay != null) {
View view = getChildViewUnderPosition(pos);
extendSelectionOverlay(view);
}

if(action == MotionEvent.ACTION_UP && overlay != null) {
endSelectionOverlay();
invalidate();
}
return true;
}

private View getChildViewUnderPosition(float[] pos) {
int num = getChildCount();
View v;
for(int x = 0; x < num; x++) {
v = getChildAt(x);
if(v.getX() <= pos[0] && (v.getX() + v.getWidth()) >= pos[0] && v.getY() <= pos[1] && (v.getY() + v.getHeight()) >= pos[1] && !v.isSelected()) {
return v;
}
}

return null;
}

private void endSelectionOverlay() {
overlay = null;
invalidate();
}
}

关于android - 在 View 上拖动时选择叠加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29526364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com