如何在Android中执行拖动幻灯片视图 [英] How to do a drag slide view in Android

查看:76
本文介绍了如何在Android中执行拖动幻灯片视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够将一个列表视图向左拖动并滑出视图,同时暴露在第一个列表视图下面的另一个列表视图.我该怎么做呢?

I need to be able to drag-slide a listview to the left and out of view while exposing another listview that is underneath the first listview. How can I go about doing this?

推荐答案

您可以使用OnTouchListener并在ACTION_MOVE上调整大小或移动某些视图.记住要调用setClickable(true)以确保调用了ACTION_MOVE.

You can use a OnTouchListener and resize or move some view on ACTION_MOVE. Remember to call setClickable(true) to make sure ACTION_MOVE gets called.

下面是一个带有滑动视图的示例:

There goes an example with the sliding view at the bottom:

布局:

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/background_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        (more content)

    </LinearLayout>
    <LinearLayout
            android:id="@+id/sliding_view"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <View
                android:id="@+id/draggable_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        (more content)

    </LinearLayout>

</RelativeLayout>

设置视图:

View slidingView = ...
View draggableView = ... //placed inside slidingView, this is where you touch and drag
draggableView.setOnTouchListener(new TouchListener(slidingView));
draggableView.setClickable(true); //if not set, only ACTION_DOWN gets called, ACTION_MOVE doesn't

监听器:

private class TouchListener implements View.OnTouchListener{

        View slidingView;

        int initHeight;
        float initPos;
        ViewGroup.LayoutParams params;

        private TouchListener(View slidingView) {
            this.slidingView = slidingView;
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(params == null){
                params = slidingView.getLayoutParams();
            }

            switch (event.getActionMasked()){
                case ACTION_DOWN: //get initial state
                    initHeight = slidingView.getHeight();
                    initPos = event.getRawY();
                    break;
                case ACTION_MOVE: //do the sliding
                    float dPos = initPos - event.getRawY();
                    params.height = Math.round(initHeight + dPos);
                    slidingView.requestLayout(); //refresh layout
                    break;
            }

            return false;
        }
    }

注意:如果仍然没有调用ACTION_MOVE,请尝试调用setFocusable和setClickable之外的其他方法.当我的可拖动视图"是TextView时,我不需要这样做.

Note: If ACTION_MOVE still doesn't get called, try calling setFocusable in addition to setClickable. I didn't need to do this when my "draggable view" was a TextView though.

这篇关于如何在Android中执行拖动幻灯片视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆