将recyclerView旋转90度

时间:2018-12-17 12:17:07

标签: android android-recyclerview

我有一个屏幕,其中上半部分由cameraView覆盖,下半部分是其中包含一个片段的slidePanel,该片段包含一个recyclerView。我只希望屏幕是纵向的,但是当方向更改为横向时,对象应在其位置旋转90度。我正在旋转包含Fragment并包含RecyclerView的FrameLayout。对象旋转完美,但是问题是recyclerView表现得很奇怪。当frameLayout旋转到90度RecyclerView而不是在横向模式下垂直滚动时,它将在水平滚动时滚动。

下面是我的XML,其中包含已旋转到90度的FrameLayout。

<com.sothree.slidinguppanel.SlidingUpPanelLayout
    xmlns:sothree="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/sliding_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    sothree:umanoDragView="@+id/layout_dragger"
    sothree:umanoPanelHeight="68dp"
    sothree:umanoShadowHeight="4dp"
    app:ignore="NamespaceTypo"
    android:background="@color/transparent">

    <com.otaliastudios.cameraview.CameraView
        android:id="@+id/camera"
        android:keepScreenOn="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        sothree:cameraFacing="front"
        sothree:cameraSessionType="video"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:id="@+id/main_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="false"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/layout_dragger"
                android:layout_width="match_parent"
                android:layout_height="68dp">

                <ImageView
                    android:id="@+id/up_arrow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/up_arrow"
                    android:layout_alignParentLeft="true"/>
                <ImageView
                    android:id="@+id/record"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/record"
                    android:layout_centerInParent="true"/>

            </RelativeLayout>

            <FrameLayout
                android:id="@+id/frame_video"
                android:layout_width="match_parent"
                android:layout_height="250dp">
            </FrameLayout>
        </LinearLayout>

    </LinearLayout>

</com.sothree.slidinguppanel.SlidingUpPanelLayout>

下面是我的班级,其中包含旋转动画。

 public class MirrorFragment extends Fragment {

        private LinearLayout mQueen;
        private Button mHidden, accept, decline;
        private static final String TAG = "DemoActivity";
        private DraggingPanel mDraggingPanel;
        private LinearLayout mMainLayout;
        private FrameLayout frameContainer;
        Fragment childFragment;
        private Context mContext;
        int height,width;
        int draggingPanelHeight, draggingPanelWidth;
        private ImageView iv_record, up_arrow;
        RelativeLayout.LayoutParams param;
        FrameLayout.LayoutParams frameParam;
        SlidingUpPanelLayout mLayout;
        private Animation toLandAnim, toPortAnim;
        private OrientationListener orientationListener;
        private CameraView cameraView;
        private boolean mCapturingVideo =false;
        Fragment fragment;
        private static final String VIDEO_EXTENSION = ".mp4";
        private String mNextVideoAbsolutePath;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_mirror, null);
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            mContext = getContext();
            fragment = (MirrorFragment)getFragmentManager().findFragmentByTag("mirror_frag");
            initViews(view);
    //        setWidthOfBottomContainer();

            up_arrow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                }
            });

            iv_record.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mCapturingVideo){
                        stopVideo();
                    }else{
                        captureVideo();
                    }

                }
            });

        }

        private void initViews(View view) {

            mLayout = (SlidingUpPanelLayout) view.findViewById(R.id.sliding_layout);
            mLayout.setAnchorPoint(0.5f);
            mMainLayout = view.findViewById(R.id.main_layout);
            frameContainer = view.findViewById(R.id.frame_video);
            cameraView = view.findViewById(R.id.camera);
            up_arrow = view.findViewById(R.id.up_arrow);
            iv_record = view.findViewById(R.id.record);
            cameraView.setLifecycleOwner(fragment.getViewLifecycleOwner());
            cameraView.mapGesture(Gesture.PINCH, GestureAction.ZOOM); // Pinch to zoom!
            toLandAnim= AnimationUtils.loadAnimation(mContext, R.anim.view_to_landscape);
            toPortAnim= AnimationUtils.loadAnimation(mContext, R.anim.view_to_portrait);
            orientationListener = new MirrorFragment.OrientationListener(mContext);
            childFragment = new DiscoveryFragment();
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.replace(R.id.frame_video, childFragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }

        @Override
        public void onResume() {
            super.onResume();
            cameraView.start();
        }

        @Override
        public void onPause() {
            super.onPause();
            cameraView.stop();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            cameraView.destroy();
        }

        @Override
        public void onStart() {
            orientationListener.enable();
            super.onStart();
        }

        @Override
        public void onStop() {
            super.onStop();
            orientationListener.disable();
        }

        @Override
        public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
            super.onMultiWindowModeChanged(isInMultiWindowMode);

        }

        private void getHeightWidth() {
           /* draggingPanelHeight = mLayout.getHeight();
           draggingPanelWidth = mLayout.getWidth();*/
            DisplayMetrics displayMetrics = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            height = displayMetrics.heightPixels;
            width = displayMetrics.widthPixels;
        }

        private class OrientationListener extends OrientationEventListener{
            final int ROTATION_0    = 1;
            final int ROTATION_90   = 2;
            final int ROTATION_180  = 3;
            final int ROTATION_270  = 4;

            private int rotation = 0;
            public OrientationListener(Context context) { super(context); }

            @Override public void onOrientationChanged(int orientation) {

                if( (orientation < 35 || orientation > 325) && rotation!= ROTATION_0){ // PORTRAIT
                    getHeightWidth();
                    rotation = ROTATION_0;
                    frameContainer.startAnimation(toPortAnim);
                }
                else if( orientation > 145 && orientation < 215 && rotation!=ROTATION_180){ // REVERSE PORTRAIT
                    getHeightWidth();
                    rotation = ROTATION_180;
                    frameContainer.startAnimation(toPortAnim);
                }
                else if(orientation > 55 && orientation < 125 && rotation!=ROTATION_270){ // REVERSE LANDSCAPE
                    getHeightWidth();
                    rotation = ROTATION_270;

                    //param = new RelativeLayout.LayoutParams((int)width/2,ViewGroup.LayoutParams.MATCH_PARENT);
                    frameContainer.setLayoutParams(new LinearLayout.LayoutParams((int)height/2,(int)width));

                    frameContainer.startAnimation(toLandAnim);

                }
                else if(orientation > 235 && orientation < 305 && rotation!=ROTATION_90){ //LANDSCAPE
                    getHeightWidth();
                    rotation = ROTATION_90;

                    //param = new RelativeLayout.LayoutParams((int)width/2,ViewGroup.LayoutParams.MATCH_PARENT);
                    frameContainer.setLayoutParams(new LinearLayout.LayoutParams((int)height/2,(int)width));
                    frameContainer.startAnimation(toLandAnim);
                }
            }
        }

    }

我的问题是,当recyclerView旋转到90度时,如何更改其滚动行为。请找到随附的屏幕截图以供参考。 enter image description here

enter image description here

如果您看到所附的屏幕截图,则可以看到当我旋转框架容器时,它旋转了其中的对象recylerView,但在横向时它像在纵向模式下一样滚动。

0 个答案:

没有答案