recyclerview adapter-oncreateviewholder接收到未知的viewtype

w9apscun  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(329)

我在自定义适配器的oncreateviewholder函数中遇到了一个奇怪的异常。可能是因为getitemviewtype函数按预期返回不同的值。
getitemviewtype函数如下所示:

private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;

@Override
public int getItemViewType(int position) {
  if (position == 0)
    return TYPE_HEADER;

  return TYPE_ITEM;
}

如您所见,它只返回0或1,没有其他选项。
oncreateviewholder函数如下所示:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  if (viewType == TYPE_HEADER) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_groups, null);
    return new HeaderHolder(view);
  } else if(viewType == TYPE_ITEM) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.explore_group_row, null);
    return new ItemHolder(view);
  }

  throw new RuntimeException("there is no type that matches the type " + viewType + " make sure your using types correctly");
}

但是,我从crashlytics中得到一些崩溃,即没有与viewtypes 6、7、8、11等匹配的类型。。不知道从哪里来的。。。此外,我无法重现这个问题,不知道它是如何发生的。
我在这个线程中看到了类似的问题,但没有解决方案。
在这里您可以看到crashlytics的异常:

Fatal Exception: java.lang.RuntimeException: there is no type that matches the type 6 make sure your using types correctly
       at com.example.eliran.forum.GroupsFragment$GroupsAdapter.onCreateViewHolder(GroupsFragment.java:72)
       at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5)
       at androidx.recyclerview.widget.RecyclerView$Recycler.a(RecyclerView.java:295)
       at androidx.recyclerview.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:14)
       at androidx.recyclerview.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:15)
       at androidx.recyclerview.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:22)
       at androidx.recyclerview.widget.GapWorker.a(GapWorker.java:3)
       at androidx.recyclerview.widget.GapWorker.run(GapWorker.java:71)
       at android.os.Handler.handleCallback(Handler.java:873)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:7078)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

下面是适配器的完整代码:

private class GroupsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private static final int TYPE_HEADER = 0;
        private static final int TYPE_ITEM = 1;

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == TYPE_HEADER) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_groups, null);
                return new HeaderHolder(view);
            } else if(viewType == TYPE_ITEM) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.explore_group_row, null);
                return new ItemHolder(view);
            }

            throw new RuntimeException("there is no type that matches the type " + viewType + " make sure your using types correctly");
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int i) {
            if (holder instanceof ItemHolder) {
                ItemHolder theHolder = (ItemHolder) holder;
                Group group = groups.get(i - 1);
                theHolder.title.setText(group.getTitle());
                theHolder.description.setText(group.getDescription());

                if(group.getImage() != null) {
                    Picasso.get()
                            .load(group.getImage())
                            .fit()
                            .into(theHolder.image);
                } else {
                    Picasso.get()
                            .load(R.drawable.profile_picture_holder)
                            .fit()
                            .into(theHolder.image);
                }
            } else if (holder instanceof HeaderHolder){
                HeaderHolder theHolder = (HeaderHolder) holder;
                if (showMyGroupsPb) {
                    theHolder.myGroupsPb.setVisibility(View.VISIBLE);
                    theHolder.myGroupSectionHeader.setOnBtnListener(null);
                } else {
                    theHolder.myGroupsPb.setVisibility(View.GONE);
                    theHolder.myGroupSectionHeader.setOnBtnListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(rootView.getContext(), MyGroupsActivity.class);
                            intent.putExtra("groups", myGroups);
                            startActivityForResult(intent, TheFinals.JOIN_LEAVE_GROUP_CODE);
                        }
                    });
                }
            }
        }

        @Override
        public int getItemViewType(int position) {
            if (position == 0)
                return TYPE_HEADER;

            return TYPE_ITEM;
        }

        @Override
        public int getItemCount() {
            return groups.size() + 1;
        }

        class HeaderHolder extends RecyclerView.ViewHolder {

            private ProgressBar myGroupsPb;
            private SectionHeader myGroupSectionHeader;

            public HeaderHolder(View view) {
                super(view);

                myGroupsRv = view.findViewById(R.id.groupsRecyclerView);
                myGroupsPb = view.findViewById(R.id.pb);
                LinearLayoutManager horizontalLayoutManager
                        = new LinearLayoutManager(rootView.getContext(), LinearLayoutManager.HORIZONTAL, false);
                myGroupsRv.setLayoutManager(horizontalLayoutManager);
                myGroupsRv.setAdapter(myGroupAdapter);
                myGroupSectionHeader = view.findViewById(R.id.myGroupSectionHeader);
            }

        }

        class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            protected TextView title;
            protected ImageView image;
            protected TextView description;

            public ItemHolder(View view) {
                super(view);
                this.title = view.findViewById(R.id.title);
                this.image = view.findViewById(R.id.image);
                this.description = view.findViewById(R.id.description);
                view.setOnClickListener(this);
            }

            @Override
            public void onClick(View view) {
                int itemPosition = groupsRecyclerView.getChildLayoutPosition(view) - 1;
                selectedGroupIndex = itemPosition;
                Intent intent = new Intent(rootView.getContext(), GroupActivity.class);
                intent.putExtra("group", groups.get(itemPosition));
                startActivityForResult(intent, TheFinals.JOIN_LEAVE_GROUP_CODE);
            }
        }
    }
6ju8rftf

6ju8rftf1#

你把if条件写错了。您需要完成if条件,如下所示:

private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;

@Override
public int getItemViewType(int position) {
  if (position == 0){
    return TYPE_HEADER;
    }
  return TYPE_ITEM;
}

相关问题