垂直回收器内部的垂直回收器视图,对所有项目调用onBindViewHolder

时间:2018-10-22 13:13:28

标签: android android-recyclerview recycler-adapter nestedrecyclerview

我有一个嵌套的回收站视图。父回收者视图的一项是水平回收者视图,另一项是垂直回收器视图。

所有项目都调用了儿童回收者视图的onBindViewHolder,即使当前不可见的项目也是如此。

但是对于儿童水平回收站视图来说,它工作正常。

即使这篇文章对相同的内容也没有帮助:Recyclerview onBindViewHolder called for all items when inside LinearLayout with weights

这是相同的代码: 父级recyclerview xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:bind="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">

    <data>

        <import type="android.view.View"/>



        <variable
            name="viewModel"
            type="com.activities.ui.srp.SRPActivityVM"/>

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true"
            android:background="@color/blue"
            android:minHeight="?android:attr/actionBarSize"
            android:theme="@style/Theme.Toolbar"
            bind:layout_scrollFlags="scroll|enterAlways"
            bind:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/screen_bg">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/activity_srp_main_recyclerVw"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                android:paddingTop="@{viewModel.quickFilters.size>0?@dimen/_100sdp : @dimen/_65sdp}"
                bind:populateItems="@{viewModel.srpData}"
                tools:listitem="@layout/item_srp_lyt"
                >

            </android.support.v7.widget.RecyclerView>

        </FrameLayout>
    </LinearLayout>


</layout>

Child RecyclerView XML,水平和垂直子recyclerview使用相同的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              xmlns:tools="http://schemas.android.com/tools">

    <TextView
        android:id="@+id/item_srp_recyclervw_header"
        android:text="Recommended for you"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:fontFamily="@font/quicksand_bold"
        android:letterSpacing="-0.03"
        android:textColor="#333333"
        android:visibility="gone"
        android:layout_marginStart="@dimen/_16sdp"
        android:layout_marginEnd="@dimen/_16sdp"
        android:layout_marginTop="@dimen/_16sdp"
        android:layout_marginBottom="@dimen/_8sdp"
        android:textSize="18sp"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/item_srp_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>
</LinearLayout>

家长RecyclerView适配器:

public class SRPBaseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

  private final SRPActivity baseActivity;
  private SRPModel.Data srpData;
  private int LIST_ITEM_COUNT = 2;

  private TagsAdapter tagsAdapter;
  private SRPListAdapter srpListAdapter;


  public SRPBaseAdapter(SRPActivity baseActivity, SRPModel.Data srpData) {
    this.baseActivity = baseActivity;
    this.srpData = srpData;
  }

  public void injectAdapter(TagsAdapter tagsAdapter, SRPListAdapter srpListAdapter) {
    this.tagsAdapter = tagsAdapter;
    this.srpListAdapter = srpListAdapter;
  }

  @IntDef({ViewType.TAG, ViewType.ITEM_LIST, ViewType.DEFAULT})
  @Retention(RetentionPolicy.SOURCE)
  public @interface ViewType {
    int TAG = 0;
    int ITEM_LIST = 1;
    int DEFAULT = 2;
  }

  @NonNull
  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    switch (viewType) {
      case ViewType.TAG:
        View itemLayoutView = layoutInflater.inflate(R.layout.item_srp_recyclevw, parent, false);
        return new TagsViewHolder(itemLayoutView);
      case ViewType.ITEM_LIST:
        itemLayoutView = layoutInflater.inflate(R.layout.item_srp_recyclevw, parent, false);
        return new ItemsViewHolder(itemLayoutView);
      default:
        itemLayoutView = layoutInflater.inflate(R.layout.item_default_lyt, parent, false);
        return new DefaultHolder(itemLayoutView);
    }
  }

  public void addItems(SRPModel.Data srpData) {
    if (srpData != null) {
      this.srpData.getActivityItems().addAll(srpData.getActivityItems());
      this.srpData.getTags().addAll(srpData.getTags());
      notifyDataSetChanged();
    }
  }

  public void clearItems() {
    if (srpData.getActivityItems() != null)
      srpData.getActivityItems().clear();
    if (srpData.getTags() != null)
      srpData.getTags().clear();
  }


  @Override
  public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
    switch (getItemViewType(position)) {
      case ViewType.TAG:
        ((TagsViewHolder) holder).populateItems(srpData.getTags());
        break;
      case ViewType.ITEM_LIST:
        ((ItemsViewHolder) holder).populateItems(srpData.getActivityItems());
        break;
    }
  }

  @Override
  public int getItemCount() {
    return LIST_ITEM_COUNT;
  }

  @Override
  public int getItemViewType(int position) {
    if (srpData != null) {
      switch (position) {
        case 0:
          if (srpData.getTags() != null && srpData.getTags().size() > 0)
            return ViewType.TAG;
        case 1:
          if (srpData.getActivityItems() != null && srpData.getActivityItems().size() > 0)
            return ViewType.ITEM_LIST;
      }
    }
    return ViewType.DEFAULT;
  }

  public class ItemsViewHolder extends RecyclerView.ViewHolder {

    private RecyclerView recyclerVw;

    public ItemsViewHolder(final View itemView) {
      super(itemView);
      itemView.findViewById(R.id.item_srp_recyclervw_header).setVisibility(View.VISIBLE);
      recyclerVw = itemView.findViewById(R.id.item_srp_recycler_view);
    }

    public void populateItems(List<SRPModel.ActivityItem> itemList) {
      LinearLayoutManager mLyoutManager = new LinearLayoutManager(baseActivity);
      mLyoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
      recyclerVw.setLayoutManager(mLyoutManager);
      recyclerVw.setItemAnimator(new DefaultItemAnimator());
      recyclerVw.setAdapter(srpListAdapter);
      srpListAdapter.clearItems();
      srpListAdapter.addItems(srpData.getActivityItems());
      srpListAdapter.setListener(baseActivity);
    }
  }

  public class TagsViewHolder extends RecyclerView.ViewHolder {
    private RecyclerView recyclerVw;

    public TagsViewHolder(final View itemView) {
      super(itemView);
      recyclerVw = itemView.findViewById(R.id.item_srp_recycler_view);
    }

    public void populateItems(List<SRPModel.Tag> tagList) {
      LinearLayoutManager mLyoutManager = new LinearLayoutManager(baseActivity);
      mLyoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
      recyclerVw.setLayoutManager(mLyoutManager);
      recyclerVw.setItemAnimator(new DefaultItemAnimator());
      recyclerVw.setAdapter(tagsAdapter);
      tagsAdapter.clearItems();
      tagsAdapter.addItems(srpData.getTags());
      tagsAdapter.setListener(baseActivity);
    }
  }


  private class DefaultHolder extends RecyclerView.ViewHolder {
    public DefaultHolder(View inflate) {
      super(inflate);
    }
  }

}

0 个答案:

没有答案