刷机上设置的Java android RelativeLayout Listener不能与listView一起使用

时间:2017-03-10 06:52:48

标签: java android android-layout listview

嗨我在RelativeLayout上有相对布局的刷卡监听器我有texview ...和ListView,当我向上滑动时我显示了一个listView,当我向下滑动时我隐藏了一个listView。列表View在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));

1 个答案:

答案 0 :(得分:0)

现在我这样做。我检查了ListView上的可能性,但是当我将listView刷到Up并且它总是列出View隐藏时,我不知道这是否有效

    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));
            }
        }
    });