簡體   English   中英

回收站視圖滾動混亂

[英]Recycler View Scrolling messed up

在本教程之后,我嘗試使用異構的Recycler視圖。 異構布局對於滾動“回收站”視圖的部分,一切工作正常,布局未正確顯示。 我有2種布局,一種具有文本,另一種具有圖像,在滾動時,我在文本部分中留有很多空白,使觀看者感到以前有一些圖像。 我已經在線檢查了鏈接,但沒有解決任何問題。需要您的幫助,我們感謝您。
這是我的代碼

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);
    }
}
}

這就是我將適配器綁定到回收視圖的方式。

家庭活動

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

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

<?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>

提前致謝。

我已經遇到了這個問題,並意識到在我的情況下,我是在public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {}切換視圖可見性

因此,發生的事情是由於我的代碼錯誤,有時某些視圖被完全隱藏了,因此沒有被刪除或刪除。 這導致了視圖的隨機性。 因此,如果您有一些更改視圖可見性的代碼,請刪除該可見性並測試問題是否仍然存在。

請注意這一點!

如果您的應用出於某種原因需要刪除項目,並且您想在recyclerview中查看刪除項目的過程,那么將setISRecyclable設置為false不好,請檢查它! 所以對於每個人都想將其添加到適配器中(除了那些出於某種原因不想回收某些物品的人),我有消息:

請考慮這一點,在需要某種切換內容的情況下,在onBindViewHolder的適配器中,對onBindViewHolder的每次調用都必須包括load()調用或clear()調用。

例如,如果您檢查布爾值或0-1,以將RecyclerView項目布局的某些部分設置為不可見或可見,則應在If語句塊中同時考慮兩者。

例如在這個問題中我看不到configureViewHolder2和1內的else塊,所以可能發生了混亂的情況。

tnx讀取患者閱讀器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM