使用RecyclerView和CardView在Android Studio中分离不同的视图
我在我的应用程序中将FirebaseRecycleServiceAdapter与CardView一起使用,希望在一张卡上加载视频,在另一张卡上加载图像,但我不知道如何将它们分开。每次我在CardView布局中添加视频视图时,它都会被添加到同一张卡的ImageView顶部,并且会在所有RecyclerView中重复。就像我的帖子数量一次又一次地出现一样。我想VideoView应该在不同的帖子上播放视频,ImageView在不同的帖子上显示图像,我搜索了很多选项,但没有找到任何有用的,请帮助 我正在使用Android Studio CardView xml文件:使用RecyclerView和CardView在Android Studio中分离不同的视图,android,android-layout,firebase,android-recyclerview,android-cardview,Android,Android Layout,Firebase,Android Recyclerview,Android Cardview,我在我的应用程序中将FirebaseRecycleServiceAdapter与CardView一起使用,希望在一张卡上加载视频,在另一张卡上加载图像,但我不知道如何将它们分开。每次我在CardView布局中添加视频视图时,它都会被添加到同一张卡的ImageView顶部,并且会在所有RecyclerView中重复。就像我的帖子数量一次又一次地出现一样。我想VideoView应该在不同的帖子上播放视频,ImageView在不同的帖子上显示图像,我搜索了很多选项,但没有找到任何有用的,请帮助 我正在
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<ImageView
android:id="@+id/po_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/ti_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/de_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="@+id/so_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="@color/SoColor"/>
</LinearLayout>
</android.support.v7.widget.CardView>
FirebaseRecyclerAdapter <post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(
post.class,
R.layout.post_row_recycle_home,
postViewHolder.class,
mDatabaseReference
) {
@Override
protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
viewHolder.setTi(model.getTi());
viewHolder.setde(model.getDe());
}
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class postViewHolder extends RecyclerViewPager.ViewHolder{
View mView;
public postViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTi(String ti){
TextView post_ti = (TextView)mView.findViewById(R.id.ti_cardView);
post_ti.setText(ti);
}
public void setde(String de){
TextView post_de = (TextView)mView.findViewById(R.id.de_cardView);
post_de.setText(de);
}
FirebaseRecyclerView适配器类:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<ImageView
android:id="@+id/po_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/ti_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/de_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="@+id/so_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="@color/SoColor"/>
</LinearLayout>
</android.support.v7.widget.CardView>
FirebaseRecyclerAdapter <post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(
post.class,
R.layout.post_row_recycle_home,
postViewHolder.class,
mDatabaseReference
) {
@Override
protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
viewHolder.setTi(model.getTi());
viewHolder.setde(model.getDe());
}
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class postViewHolder extends RecyclerViewPager.ViewHolder{
View mView;
public postViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTi(String ti){
TextView post_ti = (TextView)mView.findViewById(R.id.ti_cardView);
post_ti.setText(ti);
}
public void setde(String de){
TextView post_de = (TextView)mView.findViewById(R.id.de_cardView);
post_de.setText(de);
}
FirebaseRecyclerAdapter FirebaseRecyclerAdapter=新的FirebaseRecyclerAdapter(
课后,
R.layout.post\u row\u recycle\u home,
postViewHolder.class,
mDatabaseReference
) {
@凌驾
受保护的void populateViewHolder(postViewHolder、post模型、int位置){
setTi(model.getTi());
viewHolder.setde(model.getDe());
}
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
公共静态类postViewHolder扩展了RecycleServicewPager.ViewHolder{
视图视图;
公共postViewHolder(查看项目视图){
超级(项目视图);
mView=项目视图;
}
公共无效设置ti(字符串ti){
TextView post_ti=(TextView)mView.findViewById(R.id.ti_cardView);
后ti.setText(ti);
}
公共无效集合de(字符串de){
TextView post_de=(TextView)mView.findViewById(R.id.de_cardView);
邮政编码SETEXT(de);
}
首先设置两个视图的可见性,然后根据其视频url或图像url设置可见性。操纵每个视图的可见性
使用RecyclerView时,不要将VideoPlayer和ImageView放在同一个xml布局中,您可以创建两个不同的布局,并使用
getItemViewType()
选择要充气的正确布局
关于如何在同一个RecyclerView上使用不同布局的问题已经得到了回答
示例代码:
image_item_layout.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:visibility="gone"
android:id="@+id/po_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<...>
</LinearLayout>
</android.support.v7.widget.CardView>
video_item_layout.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:visibility="gone"
android:id="@+id/youtube"
android:layout_width="match_parent"
android:layout_height="
<...>
</LinearLayout>
</android.support.v7.widget.CardView>
image\u item\u layout.xml
您已将imageview和videoview放在同一布局中。因此,listview中的每个项目都包含这两个项目。我建议您最好的解决方案是实用地一次隐藏其中一个项目,然后根据您的数据显示另一个项目。@ZohaibHassan可以使用FirebaseRecyclerAdapter实现吗?可以使用Recy实现clerView。但我不确定FirebaseRecyclerAdapter。先生,我正在使用Firebase作为数据库,并且在其中使用FirebaseRecyclerAdapter,我可以将其与之一起使用吗?是的,你可以。我编辑了答案,并添加了一个关于将其与FireBaseSir一起使用的链接。我已经查看了这篇文章,但是Firebase更新中是否使用了itemViewType()它的功能是一样的吗?当我将visibility设置为gone并为youtube添加视频id时,我的应用程序显示youtube播放器崩溃的错误。崩溃日志是什么?我肯定会在我的问题中很快发布它。先生,不要叫我兄弟先生。我们是来互相帮助的。没有错误日志兄弟,但是如果我有视频id,youtube播放器也不可见然后在对话框中显示youtube的错误已停止。