gpt4 book ai didi

Java android 相对布局在滑动监听器上设置不适用于内部的 listView

转载 作者:太空宇宙 更新时间:2023-11-04 11:41:53 25 4
gpt4 key购买 nike

嗨,我在相对布局中的RelativeLayout上有一个滑动监听器,我有texview ...和ListView,当我向上滑动时,我显示一个listView,当我向下滑动时,我隐藏一个listView。 ListView 位于relativeLayout内,但是当我向下滑动(在relativeLayout中)时,listVIew永远不会隐藏。这就是我所做的:

ublic class RelativeLayoutTouchListener implements View.OnTouchListener {

static final String logTag = "ActivitySwipeDetector";
private Activity activity;
static final int MIN_DISTANCE = 100;// TODO change this runtime based on screen resolution. for 1920x1080 is to small the 100 distance
private float downX, downY, upX, upY;

// private MainActivity mMainActivity;

public RelativeLayoutTouchListener(MainActivity mainActivity) {
activity = mainActivity;
}

public void onRightToLeftSwipe() {
Log.i(logTag, "RightToLeftSwipe!");
Toast.makeText(activity, "RightToLeftSwipe", Toast.LENGTH_SHORT).show();
// activity.doSomething();
}

public void onLeftToRightSwipe() {
Log.i(logTag, "LeftToRightSwipe!");
Toast.makeText(activity, "LeftToRightSwipe", Toast.LENGTH_SHORT).show();
// activity.doSomething();
}

public void onTopToBottomSwipe() {
Log.i(logTag, "onTopToBottomSwipe!");
Toast.makeText(activity, "onTopToBottomSwipe", Toast.LENGTH_SHORT).show();
// activity.doSomething();
}

public void onBottomToTopSwipe() {
Log.i(logTag, "onBottomToTopSwipe!");
Toast.makeText(activity, "onBottomToTopSwipe", Toast.LENGTH_SHORT).show();
// activity.doSomething();
}

public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
Log.e("sdasdas", "dsfdsfdsfsdf");
downX = event.getX();
downY = event.getY();
return true;
}
case MotionEvent.ACTION_UP: {

Log.e("działa", "kikik");
upX = event.getX();
upY = event.getY();

float deltaX = downX - upX;
float deltaY = downY - upY;

Log.e("delta y " , deltaY +"");

// Log.e("delta " , deltaY +"");
// // swipe horizontal?
// if (Math.abs(deltaX) > MIN_DISTANCE) {
// // left or right
// if (deltaX < 0) {
// this.onLeftToRightSwipe();
// return true;
// }
// if (deltaX > 0) {
// this.onRightToLeftSwipe();
// return true;
// }
// } else {
// Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long horizontally, need at least " + MIN_DISTANCE);
// // return false; // We don't consume the event
// }
//
// // swipe vertical?
// if (Math.abs(deltaY) > MIN_DISTANCE) {
// // top or down
// if (deltaY < 0) {
// this.onTopToBottomSwipe();
// return true;
// }
// if (deltaY > 0) {
// this.onBottomToTopSwipe();
// return true;
// }
// } else {
// Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long vertically, need at least " + MIN_DISTANCE);
// // return false; // We don't consume the event
// }
final ViewGroup.LayoutParams params = MainActivity.relativeLayout1.getLayoutParams();
if(deltaY<0){
params.height = (int) activity.getResources().getDimension(R.dimen.dimen_entry_in_dp);
relativeLayout1.setLayoutParams(params);
MainActivity.listView1.setVisibility(View.GONE);
}else{
params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
MainActivity.relativeLayout1.setLayoutParams(params);
MainActivity.listView1.setVisibility(View.VISIBLE);
}

return false; // no swipe horizontally and no swipe vertically
}// case MotionEvent.ACTION_UP:
}
return false;
}
}

这就是我如何称呼这个监听器:

relativeLayout1.setOnTouchListener(new RelativeLayoutTouchListener(this));

最佳答案

现在我就这样做。我检查了 ListView 上的位置,但是当我将 listView 向上滑动时,它总是 listView hide ,我不知道这是否正确工作

    listView1.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
boolean enable = false;
if (listView1 != null && listView1.getChildCount() > 0) {
boolean firstItemVisible = listView1.getFirstVisiblePosition() == 0;
boolean topOfFirstItemVisible = listView1.getChildAt(0).getTop() == 0;
enable = firstItemVisible && topOfFirstItemVisible;
}
if(enable) {
Log.e("works ? ", "works");
listView1.setOnTouchListener(new RelativeLayoutTouchListener(MainActivity.this));
}
}
});

关于Java android 相对布局在滑动监听器上设置不适用于内部的 listView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42712096/

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