当我将视频播放到 Recyclerview 内的 VideoView 时,当我滚动时,它会在 x 轴上旋转
Posted
技术标签:
【中文标题】当我将视频播放到 Recyclerview 内的 VideoView 时,当我滚动时,它会在 x 轴上旋转【英文标题】:When i play video to VideoView inside a Recyclerview this rotate in x axis when i scroll 【发布时间】:2020-06-12 04:39:27 【问题描述】:我有一个问题。我将 videoview 放在 recyclerview 中,当我在手机中查看它时,视频效果非常罕见,有时当我向下滚动时它会在 x 轴上旋转 260°,但我也有 imageview 并且它可以工作很好。
对不起,我是android新手,我在网上搜索信息,但我找不到答案
卡片xml代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView android:id="@+id/cvManana"
android:layout_
android:layout_
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_
android:layout_>
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/imgPerfil"
android:layout_
android:layout_
android:layout_gravity="center_horizontal"
android:layout_marginRight="8dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_load_img" />
<LinearLayout
android:layout_
android:layout_
android:layout_gravity="left"
android:orientation="vertical">
<LinearLayout
android:layout_
android:layout_>
<TextView
android:id="@+id/tv_fechaPost"
android:layout_
android:layout_
android:text="hace 44 minutos sep. 22º 16"
android:textColor="@color/defecto_text_fecha" />
</LinearLayout>
<LinearLayout
android:layout_
android:layout_>
<TextView
android:id="@+id/tv_nickname"
android:layout_
android:layout_
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_
android:layout_>
<LinearLayout
android:id="@+id/contentTextStory"
android:layout_
android:layout_
android:layout_marginBottom="15dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_postTimeLine"
android:layout_
android:layout_
android:layout_gravity="left"
android:text="historias"
android:textColor="@color/defecto_color_text_tabla"
android:textSize="17dp" />
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
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_
android:layout_
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitStart"
card_view:srcCompat="@drawable/campo_manana" />
<VideoView
android:id="@+id/video_timeline"
android:layout_
android:layout_
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_
android:layout_>
<LinearLayout
android:gravity="right"
android:layout_weight="1.0"
android:layout_
android:layout_>
<ImageButton
android:id="@+id/bt_reply"
android:layout_gravity="center_vertical"
android:scaleType="centerCrop"
android:layout_
android:layout_
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_
android:layout_ />
</LinearLayout>
<LinearLayout
android:layout_weight="1.0"
android:layout_
android:gravity="right"
android:layout_>
<ImageButton
android:id="@+id/bt_share"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:layout_
android:layout_
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_
android:layout_ />
</LinearLayout>
<LinearLayout
android:layout_weight="1.0"
android:layout_
android:gravity="right"
android:layout_>
<ImageButton
android:layout_gravity="center_vertical"
android:id="@+id/bt_like"
android:scaleType="centerCrop"
android:layout_
android:layout_
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_
android:layout_ />
</LinearLayout>
<LinearLayout
android:layout_weight="1.0"
android:layout_
android:gravity="right"
android:layout_>
<ImageButton
android:id="@+id/bt_dislike"
android:layout_gravity="center_vertical"
android:scaleType="centerCrop"
android:layout_
android:layout_
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_
android:layout_ />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
recyclerview的Java代码
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();
【问题讨论】:
貌似还有一些支持库试试 Refactor -> Migrate to AndroidX... 【参考方案1】:为了解决您的问题,我最终重新创建了您的项目。仍然不确定为什么您的 videoView 不起作用。我认为这要么与使用支持库有关,要么与您在 recyclerView 中隐藏/显示视图的逻辑有关。
我建议远离支持库,即android.support.v7.widget.CardView
因为它们不再受支持
并且已知有问题并迁移到 androidx
androidx.cardview.widget.CardView
。
这是我的 recyclerView 适配器中的onBindViewHolder
@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;
然后不要忘记将库添加到 Project 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
【讨论】:
告诉我进展如何@SonickSeven 我可以用 androidx 更改所有代码,但出现了新的错误,我在 *** 中更新了我原来的错误,看看它 我创建了一个新项目,因为很多事情都是错误的,但只能从另一个项目中导入代码(简单)。使用 Androidx 可以正常工作,我的朋友。非常感谢以上是关于当我将视频播放到 Recyclerview 内的 VideoView 时,当我滚动时,它会在 x 轴上旋转的主要内容,如果未能解决你的问题,请参考以下文章
android: ScrollView 内的 RecyclerView
(更新版)Android VideoPlayer 在滚动列表实现item视频播放(ListView控件和RecyclerView)
当我将视图留给前一个孩子时,viewflipper 中的视频视图仍在播放
关于打开一个新活动以响应对片段内的 RecyclerView 项目的点击