简体   繁体   English

回收站视图滚动混乱

[英]Recycler View Scrolling messed up

I am trying a heterogeneous Recycler view, following this tutorial. 在本教程之后,我尝试使用异构的Recycler视图。 Heterogeneous Layouts All is working fine expect for the part where i scroll the Recycler view, the layouts aren't displayed properly. 异构布局对于滚动“回收站”视图的部分,一切工作正常,布局未正确显示。 I have 2 layouts, one has text and other has images, on scrolling I am left with alot of blank space in the text section,giving a feel to the viewer that there was some image previously here. 我有2种布局,一种具有文本,另一种具有图像,在滚动时,我在文本部分中留有很多空白,使观看者感到以前有一些图像。 I have checked links online but nothing solved the issue.Your help is needed and appreciated. 我已经在线检查了链接,但没有解决任何问题。需要您的帮助,我们感谢您。
Here is my code 这是我的代码

ComplexRecyclerViewAdapter ComplexRecyclerViewAdapter

public class ComplexRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Object> items;
private final int STATUS = 0, IMAGE_STATUS = 1;
public ComplexRecyclerViewAdapter(List<Object> items) {
    this.items = items;
}
@Override
public int getItemCount() {
    return this.items.size();
}
@Override
public int getItemViewType(int position) {
    if (items.get(position) instanceof ArrayFeedItem) {
        return STATUS;
    } else if (items.get(position) instanceof ArrayFeedItemWithImage) {
        return IMAGE_STATUS;
    }
    return -1;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

    RecyclerView.ViewHolder viewHolder;
    LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

    if (viewType == STATUS){
        View v1 = inflater.inflate(R.layout.layout_viewholder1, viewGroup, false);
        viewHolder = new ViewHolder1(v1);
    }
    else if(viewType ==IMAGE_STATUS){
        View v2 = inflater.inflate(R.layout.layout_viewholder2, viewGroup, false);
        viewHolder = new ViewHolder2(v2);
    }
    else
    viewHolder=null;
    return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

    if (viewHolder.getItemViewType() == STATUS )
    {
        ViewHolder1 vh1 = (ViewHolder1) viewHolder;
        configureViewHolder1(vh1, position);
        vh1.setIsRecyclable(false);
    }
    else
    {
        ViewHolder2 vh2 = (ViewHolder2) viewHolder;
        configureViewHolder2(vh2, position);
        vh2.setIsRecyclable(false);
    }
}
private void configureViewHolder1(ViewHolder1 vh1, int position) {
    ArrayFeedItem item = (ArrayFeedItem) items.get(position);
    if (item != null) {
        vh1.getHolderImage().setImageResource(item.img);
        vh1.getHolderText().setText(item.txtname);
        vh1.getStatusMsg().setText(item.StatusMsg);
        vh1.getTimestamp().setText(item.Timestamp);
        vh1.getUrl().setText(item.URL);
    }
}

private void configureViewHolder2(ViewHolder2 vh2, int position) {
    ArrayFeedItemWithImage item = (ArrayFeedItemWithImage) items.get(position);
    if (item != null) {
        vh2.getHolderImage().setImageResource(item.img);
        vh2.getHolderText().setText(item.txtname);
        vh2.getStatusMsg().setText(item.StatusMsg);
        vh2.getTimestamp().setText(item.Timestamp);
        vh2.getUrl().setText(item.URL);
        vh2.getFeedImage().setImageResource(item.feedImage1);
    }
}
}

This is how I am binding the adapter to the recycle view. 这就是我将适配器绑定到回收视图的方式。

Home Activity 家庭活动

list=(RecyclerView) findViewById(R.id.list);
adapter =new ComplexRecyclerViewAdapter(items);
list.setNestedScrollingEnabled(false);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
list.setLayoutManager(mLayoutManager);
list.setItemAnimator(new DefaultItemAnimator());
list.setAdapter(adapter);

ViewHolder1 ViewHolder1

public class ViewHolder1 extends RecyclerView.ViewHolder {
private ImageView Image;
private TextView Text,Timestamp,StatusMsg,Url;
public ViewHolder1(View itemView) {
    super(itemView);
    Image=(ImageView) itemView.findViewById(R.id.img);
    Text=(TextView)itemView.findViewById(R.id.txt);
    Timestamp=(TextView)itemView.findViewById(R.id.timestamp);
    StatusMsg=(TextView)itemView.findViewById(R.id.txtStatusMsg);
    Url=(TextView)itemView.findViewById(R.id.txtUrl);

}
public ImageView getHolderImage() {
    return Image;
}

public void setHolderImage(ImageView image) {
    this.Image = image;
}
public TextView getHolderText() {
    return Text;
}

public void setHolderText(TextView text) {
    this.Text = text;
}
public TextView getTimestamp() {
    return Timestamp;
}

public void setTimestamp(TextView timestamp) {
    this.Timestamp = timestamp;
}
public TextView getStatusMsg() {
    return StatusMsg;
}

public void setStatusMsg(TextView statusmsg) {
    this.StatusMsg = statusmsg;
}
public TextView getUrl() {
    return Url;
}

public void setUrl(TextView url) {
    this.Url = url;
}

}

layout_viewholder1 layout_viewholder1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/feed_bg"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="@dimen/feed_item_margin"
    android:layout_marginRight="@dimen/feed_item_margin"
    android:layout_marginTop="@dimen/feed_item_margin"
    android:background="@drawable/bg_parent_rounded_corner"
    android:orientation="vertical"
    android:paddingBottom="@dimen/feed_item_padding_top_bottom"
    android:paddingTop="@dimen/feed_item_padding_top_bottom"
    android:id="@+id/layout1"
    >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="@dimen/feed_item_padding_left_right"
        android:paddingRight="@dimen/feed_item_padding_left_right" >

        <ImageView
            android:id="@+id/img"
            android:layout_width="@dimen/feed_item_profile_pic"
            android:layout_height="@dimen/feed_item_profile_pic"
            android:scaleType="fitCenter" >
        </ImageView>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="@dimen/feed_item_profile_info_padd" >

            <TextView
                android:id="@+id/txt"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/feed_item_profile_name"
                android:textStyle="bold"
                android:textColor="@color/black"/>

            <TextView
                android:id="@+id/timestamp"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/timestamp"
                android:textSize="@dimen/feed_item_timestamp"
                />
        </LinearLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/txtStatusMsg"
        android:textColor="@color/black"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:paddingLeft="@dimen/feed_item_status_pad_left_right"
        android:paddingRight="@dimen/feed_item_status_pad_left_right"
        android:paddingTop="@dimen/feed_item_status_pad_top" />

    <TextView
        android:id="@+id/txtUrl"
        android:textColor="@color/black"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:linksClickable="true"
        android:paddingBottom="10dp"
        android:paddingLeft="@dimen/feed_item_status_pad_left_right"
        android:paddingRight="@dimen/feed_item_status_pad_left_right"
        android:textColorLink="@color/link" />
</LinearLayout>

</LinearLayout>

Thanks in advance. 提前致谢。

I have faced this issue and realised that in my case I was toggling view visibility inside public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {} 我已经遇到了这个问题,并意识到在我的情况下,我是在public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {}切换视图可见性

So what was happening is due to bad code on my part sometimes some of the views were getting completely hidden and hence were not getting removed or recylcled. 因此,发生的事情是由于我的代码错误,有时某些视图被完全隐藏了,因此没有被删除或删除。 This caused the randomness in the view. 这导致了视图的随机性。 So if you have some code that changes the view visibility then remove that and test if the issue is still there. 因此,如果您有一些更改视图可见性的代码,请删除该可见性并测试问题是否仍然存在。

pay attention to this point please! 请注意这一点!

if your app need remove item for any reason and you want to see removed item process in recyclerview so setISRecyclable to false is not good ,check it! 如果您的应用出于某种原因需要删除项目,并且您想在recyclerview中查看删除项目的过程,那么将setISRecyclable设置为false不好,请检查它! so for every body wants to add this to adapter except(those that dont want to recycle some item for any reason) i have message: 所以对于每个人都想将其添加到适配器中(除了那些出于某种原因不想回收某些物品的人),我有消息:

please consider this point that in your adapter in onBindViewHolder for cases that need kind of toggle stuff Every call to onBindViewHolder must include either a load() call or a clear() call. 请考虑这一点,在需要某种切换内容的情况下,在onBindViewHolder的适配器中,对onBindViewHolder的每次调用都必须包括load()调用或clear()调用。

for example if you check the Boolean or 0-1 for setting some part of your RecyclerView item layout to gone or visible you should consider both of them in your If-Statement Block. 例如,如果您检查布尔值或0-1,以将RecyclerView项目布局的某些部分设置为不可见或可见,则应在If语句块中同时考虑两者。

for example in this question i cant see else block inside configureViewHolder2 and 1 so messed up items happen maybe. 例如在这个问题中我看不到configureViewHolder2和1内的else块,所以可能发生了混乱的情况。

tnx to read patient reader tnx读取患者阅读器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM