垂直滑动布局内的水平滚动

时间:2019-08-03 03:06:22

标签: android scrollview horizontal-scrolling horizontalscrollview vertical-scrolling

我有一个类似于导航抽屉的custom view类,但是它是从下到上打开的。

在我的XML中,此自定义类包装了一个水平的recyclerview。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/video_cutter_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/videoview_bg"
    android:orientation="vertical"
    tools:ignore="RtlHardcoded">

    <RelativeLayout
        android:id="@+id/layout_surface_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/layout"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:orientation="vertical">


        <com.devbrackets.android.exomedia.ui.widget.VideoView
            android:id="@+id/video_loader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />


        <ImageView
            android:id="@+id/icon_video_play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@drawable/play_button"
            android:contentDescription="@null" />

    </RelativeLayout>


    <RelativeLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="72dp"
        android:background="?attr/video_timeline_bg">


        <RelativeLayout
            android:id="@+id/timeText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/textTimeStart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginStart="7dp"
                android:layout_marginLeft="7dp"
                android:gravity="left"
                android:text="@string/timer2" />

            <TextView
                android:id="@+id/textTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:text="@string/timer2"
                android:textColor="?attr/color_accent" />

            <TextView
                android:id="@+id/textTimeEnd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginEnd="7dp"
                android:layout_marginRight="7dp"
                android:layout_toEndOf="@+id/textTime"
                android:layout_toRightOf="@+id/textTime"
                android:gravity="right"
                android:text="@string/timer2" />

        </RelativeLayout>


        <androidx.appcompat.widget.AppCompatSeekBar
            android:id="@+id/handlerTop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:max="1000"
            android:paddingTop="30dp"
            android:progressDrawable="@android:color/transparent"
            android:secondaryProgress="0"
            android:thumb="@drawable/vector_apptheme_text_select_handle_middle" />


        <View
            android:id="@+id/lineTop"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_below="@+id/handlerTop"
            android:background="@color/line_button" />


    </RelativeLayout>


    <SlidingLayer xmlns:slidingLayer="http://schemas.android.com/apk/res-auto"
        android:id="@+id/slider_panel"
        android:layout_width="match_parent"
        android:layout_height="400dip"
        android:layout_below="@+id/layout_surface_view"
        android:layout_gravity="bottom"
        android:gravity="bottom"
        slidingLayer:openLayer="false"
        slidingLayer:stickTo="bottom">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="400dip"
            android:background="@color/slider_panel_color_trans98">


            <TextView
                android:id="@+id/added_clips_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="7dp"
                android:padding="7dp"
                android:text="Selected 0 clips"
                android:textColor="?attr/text_color"
                android:textSize="@dimen/sp12"
                android:textStyle="bold" />

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/slider_iv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_gravity="top|center"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:contentDescription="@null"
                android:padding="7dp"
                android:tint="?attr/color_accent"
                app:srcCompat="@drawable/vector_slide_down" />

            <Button
                android:id="@+id/btAdd"
                style="@style/BorderlessButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:gravity="center"
                android:shadowColor="?attr/textview_shadow"
                android:shadowDx="@integer/shadowDx"
                android:shadowDy="@integer/shadowDy"
                android:shadowRadius="@integer/shadowRadius"
                android:text="@string/add"
                android:textColor="?attr/color_accent"
                android:textSize="@dimen/sp14"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btNext"
                style="@style/BorderlessButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@+id/btAdd"
                android:gravity="center"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:shadowColor="?attr/textview_shadow"
                android:shadowDx="@integer/shadowDx"
                android:shadowDy="@integer/shadowDy"
                android:shadowRadius="@integer/shadowRadius"
                android:text="@string/next"
                android:textColor="?attr/color_accent"
                android:textSize="@dimen/sp14"
                android:textStyle="bold" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/mRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btAdd"
                android:background="@color/slider_panel_color_trans98"
                android:scrollbarSize="@dimen/scrollbar_size"
                android:scrollbarStyle="outsideInset"
                android:scrollbarThumbHorizontal="?attr/color_accent"
                android:scrollbars="horizontal" />


        </RelativeLayout>

    </SlidingLayer>


</RelativeLayout>

所以我有一个水平滚动(回收视图),在垂直幻灯片内(自定义视图)。

问题是这样,请看下面的GIF文件:

horizontal scroll inside vertical slide

如您所见,我无法在recyclerview中正确滚动。

您有什么建议吗?

2 个答案:

答案 0 :(得分:2)

onInterceptTouchEvent方法内的 SlidingLayer.java 类中,为MotionEvent.ACTION_MOVE操作添加以下代码:

if (xDiff > yDiff) {
    return false;
}

它看起来像:

switch (action) {
    case MotionEvent.ACTION_MOVE:
        .
        .
        .

        if (xDiff > yDiff) {
            return false;
        }

        if (validHorizontalDrag || validVerticalDrag) {
            mIsDragging = true;
            setDrawingCacheEnabled(true);
        }

        break;

    .
    .
    .

答案 1 :(得分:1)

您可以执行以下操作:

1)MainActivity.class:

N

2)activity_main.xml:

String#gsub

3)horizo​​ntal_recycler_view_item.xml:

public class MainActivity extends AppCompatActivity {

private final String TAG = MainActivity.class.getSimpleName();
private RecyclerView mRecyclerView;

/*private float mDownMotionX = 0;
private float mDownMotionY = 0;

private final int SWIPE_SENSITIVITY = 100;

private final int SWIPE_X_SENSITIVITY = 10;
private final int SWIPE_Y_SENSITIVITY = 10;

private final int SWIPE_X_SENSITIVITY_MIN = 0;
private int SWIPE_X_SENSITIVITY_MAX = 0;
private final int SWIPE_Y_SENSITIVITY_MIN = 0;
private int SWIPE_Y_SENSITIVITY_MAX = 0;
*/

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRecyclerView = (RecyclerView) findViewById(R.id.mRecyclerView);

    List<String> data = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        data.add("I " + (i + 1));
    }
    LinearLayoutManager layoutManager
            = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    mRecyclerView.setLayoutManager(layoutManager);
    HorizontalRecyclerViewAdapter horizontalRecyclerViewAdapter = new HorizontalRecyclerViewAdapter(MainActivity.this, data);
    mRecyclerView.setAdapter(horizontalRecyclerViewAdapter);
    mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent ev) {

            mRecyclerView.getParent().requestDisallowInterceptTouchEvent(true);

            /**
             * You can adjust sensitivity by doing something similar to the code below
             */
            /*final int action = ev.getAction();

            switch (action & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN:
                    // Remember where the motion event started
                    mDownMotionX = ev.getX();
                    mDownMotionY = ev.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    // Scroll to follow the motion event
                    final float x = ev.getX();
                    final float y = ev.getY();
                    if (Math.abs(x - mDownMotionX) >= SWIPE_SENSITIVITY &&
                            mDownMotionX >= SWIPE_X_SENSITIVITY_MIN &&
                            mDownMotionX <= SWIPE_X_SENSITIVITY_MAX &&
                            Math.abs(y - mDownMotionY) <= SWIPE_Y_SENSITIVITY) {

                        mRecyclerView.getParent().requestDisallowInterceptTouchEvent(true);

                    } else{

                        mRecyclerView.getParent().requestDisallowInterceptTouchEvent(false);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_CANCEL:
                    break;
            }*/
            return false;
        }
    });

    final Display display = ((WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    final Point deviceDisplay = new Point();
    display.getSize(deviceDisplay);

    /*
    SWIPE_X_SENSITIVITY_MAX = deviceDisplay.x;
    SWIPE_Y_SENSITIVITY_MAX = mRecyclerView.getHeight();
    */

}

public class HorizontalRecyclerViewAdapter extends RecyclerView.Adapter<HorizontalRecyclerViewAdapter.MyViewHolder> {

    private final String TAG = HorizontalRecyclerViewAdapter.class.getSimpleName();
    private final Context context;
    private final List<String> data;

    public HorizontalRecyclerViewAdapter(Context context, List<String> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    @NonNull
    public HorizontalRecyclerViewAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = getLayoutInflater().inflate(R.layout.horizontal_recycler_view_item, parent, false);
        return new HorizontalRecyclerViewAdapter.MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HorizontalRecyclerViewAdapter.MyViewHolder holder, int position) {
        holder.tv.setText(data.get(position));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        final TextView tv;

        private MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
        }
    }

}

}

4)结果:

result