当我在 Recyclerview 中将视频播放到 VideoView 时,当我滚动时,它会在 x 轴上旋转

When i play video to VideoView inside a Recyclerview this rotate in x axis when i scroll

我有一个问题。我在 recyclerview 中放了一个 videoview,当我在我的 phone 中查看它时,视频效果非常好,有时它会在 x 轴上旋转 260°向下滚动,但我也有 imageview,它工作正常。

抱歉,我是 android 的新人,我在互联网上搜索信息,但我找不到答案

卡片xml代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView android:id="@+id/cvManana"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    card_view:cardCornerRadius="4dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools"
    app:ignore="NamespaceTypo">
    <LinearLayout android:background="@drawable/textlines"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:paddingTop="6dp"
        android:paddingBottom="6dp"
        android:orientation="vertical"
        android:id="@+id/post"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/imgPerfil"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginRight="8dp"
                android:scaleType="centerCrop"
                android:src="@mipmap/ic_load_img" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/tv_fechaPost"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="hace 44 minutos sep. 22º 16"
                        android:textColor="@color/defecto_text_fecha" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/tv_nickname"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingRight="12dp"
                        android:text="User"
                        android:textColor="@color/defecto_main_text_nick"
                        android:textSize="16dp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:id="@+id/contentTextStory"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tv_postTimeLine"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:text="historias"
                    android:textColor="@color/defecto_color_text_tabla"
                    android:textSize="17dp" />

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/defecto_color_text_tabla"
                android:gravity="center_horizontal"
                android:id="@+id/contentFiles">
                <ImageView
                    android:id="@+id/image_timeline"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:cropToPadding="false"
                    android:scaleType="fitStart"
                    card_view:srcCompat="@drawable/campo_manana" />

                <VideoView
                    android:id="@+id/video_timeline"
                    android:layout_width="match_parent"
                    android:layout_height="450dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    android:layout_centerInParent="true"
                    app:layout_constraintTop_toTopOf="parent"
                    android:layout_weight="1" />

            </LinearLayout>
            <LinearLayout
                android:paddingTop="4dp"
                android:paddingBottom="7dp"
                android:layout_gravity="left"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <LinearLayout
                    android:gravity="right"
                    android:layout_weight="1.0"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <ImageButton
                        android:id="@+id/bt_reply"
                        android:layout_gravity="center_vertical"
                        android:scaleType="centerCrop"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:background="@mipmap/ic_reply_gray"/>
                    <TextView
                        android:id="@+id/countReply"
                        android:text="(3)"
                        android:textSize="12dp"
                        android:layout_gravity="center"
                        android:textColor="#999"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                </LinearLayout>
                <LinearLayout
                    android:layout_weight="1.0"
                    android:layout_width="match_parent"
                    android:gravity="right"
                    android:layout_height="wrap_content">
                    <ImageButton
                        android:id="@+id/bt_share"
                        android:scaleType="centerCrop"
                        android:layout_gravity="center_vertical"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:background="@mipmap/ic_share_gray"/>
                    <TextView
                        android:id="@+id/countShare"
                        android:text="(3)"
                        android:textSize="12dp"
                        android:layout_gravity="center"
                        android:textColor="#999"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                </LinearLayout>
                <LinearLayout
                    android:layout_weight="1.0"
                    android:layout_width="match_parent"
                    android:gravity="right"
                    android:layout_height="wrap_content">
                    <ImageButton
                        android:layout_gravity="center_vertical"
                        android:id="@+id/bt_like"
                        android:scaleType="centerCrop"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:background="@mipmap/ic_like_gray"/>
                    <TextView
                        android:id="@+id/countLike"
                        android:text="(3)"
                        android:textSize="12dp"
                        android:layout_gravity="center"
                        android:textColor="#999"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                </LinearLayout>
                <LinearLayout
                    android:layout_weight="1.0"
                    android:layout_width="match_parent"
                    android:gravity="right"
                    android:layout_height="wrap_content">
                    <ImageButton
                        android:id="@+id/bt_dislike"
                        android:layout_gravity="center_vertical"
                        android:scaleType="centerCrop"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:background="@mipmap/ic_nolike_gray"/>
                    <TextView
                        android:id="@+id/countNoLike"
                        android:text="(3)"
                        android:textSize="12dp"
                        android:layout_gravity="center"
                        android:textColor="#999"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</android.support.v7.widget.CardView>

Java recyclerview 代码

postViewHolder.imgTimeLine.setVisibility(View.VISIBLE);
postViewHolder.videoTm.setVisibility(View.VISIBLE);
if(!fileson.getBoolean("err")){ // si tiene archivos adjuntos
    switch (fileson.getString("type")){
        case "Pic":
            imageTimeline imgTm=new imageTimeline(fileson.getString("src"), postViewHolder.imgTimeLine);
            imgTm.execute();
            postViewHolder.videoTm.setVisibility(View.GONE);
        break;
        case "vid":
            postViewHolder.videoTm.setVideoURI(Uri.parse(fileson.getString("src")));
            MediaController mediaController = new MediaController(ctx);
            postViewHolder.videoTm.setMediaController(mediaController);
            postViewHolder.videoTm.requestFocus();
            mediaController.setAnchorView(postViewHolder.itemView);
            //postViewHolder.videoTm.start();
            postViewHolder.imgTimeLine.setVisibility(View.GONE);
        break;
        default:
            postViewHolder.imgTimeLine.setVisibility(View.GONE);
            postViewHolder.videoTm.setVisibility(View.GONE);
        break;
    }
}else{
    postViewHolder.imgTimeLine.setVisibility(View.GONE);
    postViewHolder.videoTm.setVisibility(View.GONE);
}

可视化

编辑 ******************

我更新了 gradle 并使用了 androidx 资源,这改变了我的代码中的所有内容,但我可以解决所有问题.. 除了一。代码可以在我的设备上正常安装,但是当我尝试 运行 它时,在 run console 中显示此错误

home.java代码

// imports header
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
// end imports header

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    fnFiles.createFoldersNecesary(this);
    sockets.getmSocket();
    sockets.getnSocket();
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    sql.abrir();
    final JSONObject rows = sql.readLogin();
    sql.cerrar();
    try {
        if(rows.getString("nick")!=""){
            rowss=rows;
            startDesign(rows);
        } else {
            Intent intent = new Intent(home.this, login.class);
            startActivity(intent);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    FloatingActionButton fab=findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(home.this, newHistoryPop.class));
        }
    });

    DrawerLayout drawer = findViewById(R.id.drawer_layout);

    //ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this, );

    //ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
     //       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    //drawer.setDrawerListener((DrawerLayout.DrawerListener) toggle);
    //toggle.syncState();

    NavigationView navigationView = findViewById(R.id.nav_view_left);
    navigationView.setNavigationItemSelectedListener(this);
    
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

}

为了尝试找到解决您问题的方法,我最终重新创建了您的项目。仍然不确定为什么您的 videoView 不工作。我认为这要么与使用支持库有关,要么与 recyclerView 中的 hiding/showing 视图逻辑有关。

  • 我建议远离支持库,即 android.support.v7.widget.CardView 因为它们不再受支持 并且已知有错误并迁移到 androidx androidx.cardview.widget.CardView.

这是我的 onBindViewHolder 在我的 recyclerView 适配器中

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    final CardView cd = holder.card;
    final LinearLayout postCard = holder.postCard;
    final ImageView imageView = holder.imageTime;
    final VideoView videoTime = holder.videoTime;

    // Hide both image and video views
    imageView.setVisibility(View.GONE);
    videoTime.setVisibility(View.GONE);

    videoTime.stopPlayback(); // stop play back if view has been reused

    /*
    Demo media Urls contains mp4 and jpg
    List<String> mediaUrls = Arrays.asList("https://funnymemes.funnyjunk.com/movies/Meme_042a09_6211432.mp4",
                                     "https://memeworld.funnyjunk.com/pictures/Avaricious+preparatory+mitigated+kingfisher_c80ce6_7780034.jpg",
                                     "https://funnymemes.funnyjunk.com/movies/Meme_7a7f88_6211432.mp4");
     */


    // returns string used to distinguish the type of media (mp4,jpg)
    String mediaType = mediaUrls.get(position).substring(mediaUrls.get(position).length() - 3); // return last three chars from the url


    switch (mediaType) {
        case "mp4":
            videoTime.setVisibility(View.VISIBLE);
            Uri video = Uri.parse(mediaUrls.get(position));
            videoTime.setVideoURI(video);
            videoTime.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.setLooping(true);
                    videoTime.start();
                }
            });
            break;
        case "jpg":
            imageView.setVisibility(View.VISIBLE);
            // use picasso for async request for images
            Picasso.get()
                    .load(mediaUrls.get(position))
                    .error(R.drawable.ic_image)
                    .into(imageView);
            break;
        default:
            Log.e("Unknown Media Type ",mediaType);
            break;
    }
}

然后不要忘记将库添加到项目中 Gradle。

implementation 'com.squareup.picasso:picasso:2.71828'
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation 'androidx.cardview:cardview:1.0.0'

Here is a link to RecyclerView

Link to Git Repo