Android 具有两个Viewholder的RecyclerView中的视图重叠
我在我的回收视图中使用了2个视图持有者A和B,其中A位于第4个位置。但问题是,在每四个位置,A都是重叠的B。B的第四个视图隐藏在A下面。我如何跳过B的每四个位置,并在那里显示A 这是我的密码Android 具有两个Viewholder的RecyclerView中的视图重叠,android,android-recyclerview,android-viewholder,Android,Android Recyclerview,Android Viewholder,我在我的回收视图中使用了2个视图持有者A和B,其中A位于第4个位置。但问题是,在每四个位置,A都是重叠的B。B的第四个视图隐藏在A下面。我如何跳过B的每四个位置,并在那里显示A 这是我的密码 public class A extends RecyclerView.ViewHolder { ......Code } public class B extends RecyclerView.ViewHolder { ......Code } @Override public int getIte
public class A extends RecyclerView.ViewHolder {
......Code
}
public class B extends RecyclerView.ViewHolder {
......Code
}
@Override
public int getItemViewType(int position) {
return (position % 4 == 3) ? A : B;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == A) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.view_A, parent, false);
return new A(view);
} else {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.view_B, parent, false);
return new B(v);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder.getItemViewType() == A) {
.....Code
} else {
......Code
}
}
View_B.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoBt xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/videoLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="@color/colorPrimary"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/videoListImage"
android:layout_width="fill_parent"
android:layout_height="210dp"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/videoHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorSecondary"
android:textStyle="bold"
android:padding="12dp"
android:textSize="18sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
View_A.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/adLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="@color/colorPrimary"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.facebook.ads.MediaView
android:id="@+id/adMedia"
android:layout_width="fill_parent"
android:layout_height="210dp"
android:scaleType="fitXY" />
<ImageView
android:id="@+id/adImage"
android:layout_width="fill_parent"
android:layout_height="210dp"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/adIcon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8sp"
android:layout_marginStart="8sp"
android:layout_marginTop="8sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/adHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="18dp"
android:layout_marginRight="8dp"
android:padding="4dp"
android:textColor="@color/colorSecondary"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/mAdChoice"
android:layout_width="14dp"
android:layout_height="14dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="3dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/adSocialContext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:textColor="@color/colorSecondary"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/adBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:ellipsize="end"
android:maxLines="2"
android:paddingBottom="10dp"
android:paddingEnd="0dp"
android:paddingLeft="10dp"
android:paddingRight="0dp"
android:paddingStart="10dp"
android:textColor="@color/colorSecondary"
android:textSize="13sp" />
<StyledButton
android:id="@+id/adCallToAction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.6"
android:background="?android:attr/selectableItemBackground"
android:textColor="@color/colorSecondary" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
替换返回(位置%4==3)?A:B代码>带返回(位置+1)%4==0?A:B代码>并检查
例:
类适配器扩展了RecyclerView.Adapter{
INTA=1;
int B=2;
@凌驾
public RecyclerView.ViewHolder onCreateViewHolder(视图组父级,int-viewType){
如果(视图类型==A){
返回新的HolderA(newtextview(parent.getContext());
}否则{
返回newholderb(newtextView(parent.getContext());
}
}
@凌驾
BindViewHolder上的公共无效(RecyclerView.ViewHolder,int位置){
if(holder.getItemViewType()==A){
((HolderA)holder.textView.setText(“A”);
}否则{
((HolderB)holder.textView.setText(“B”);
}
}
@凌驾
public int getItemCount(){
返回50;
}
@凌驾
public int getItemViewType(int位置){
返回(位置+1)%4==0?A:B;
}
类HolderA扩展了RecyclerView.ViewHolder{
文本视图文本视图;
公共控股公司(查看项目视图){
超级(项目视图);
textView=(textView)项目视图;
textView.setBackgroundColor(Color.GREEN);
设置填充(10,10,10,10);
}
}
类HolderB扩展了RecyclerView.ViewHolder{
文本视图文本视图;
公共持有者B(查看项目视图){
超级(项目视图);
textView=(textView)项目视图;
设置填充(10,10,10,10);
}
}
}
为viewA和viewB的父行布局添加边距尝试此链接这些链接可能会帮助您解决这些问题有没有任何方法可以让视图在第四位置显示两次?因此,视图上的一个将是A,第二个将是B。它仍然相同,A是重叠的BI,代码完全相同,除了getItemCount是listData.size();它们都是卡片视图,A是ad,B是来自api的数据。我将更新代码。检查问题
class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
int A = 1;
int B = 2;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == A){
return new HolderA(new TextView(parent.getContext()));
}else {
return new HolderB(new TextView(parent.getContext()));
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder.getItemViewType() == A){
((HolderA) holder).textView.setText("A");
}else{
((HolderB) holder).textView.setText("B");
}
}
@Override
public int getItemCount() {
return 50;
}
@Override
public int getItemViewType(int position) {
return (position+1) % 4 == 0 ? A: B;
}
class HolderA extends RecyclerView.ViewHolder{
TextView textView;
public HolderA(View itemView) {
super(itemView);
textView = (TextView) itemView;
textView.setBackgroundColor(Color.GREEN);
textView.setPadding(10,10,10,10);
}
}
class HolderB extends RecyclerView.ViewHolder{
TextView textView;
public HolderB(View itemView) {
super(itemView);
textView = (TextView) itemView;
textView.setPadding(10,10,10,10);
}
}
}