Firefox上的FirebaseRecyclerAdapter和multiply项类型

时间:2016-08-14 08:52:56

标签: android firebase android-recyclerview firebase-realtime-database firebaseui

我想添加两种类型: -

  1. with image
  2. 无图像
  3. 在我的recyclerview中。我知道我需要覆盖FirebaseRecyclerAdapter的方法,但我不知道如何。 请帮帮我。

    @Override
    public int getItemViewType(int position) {
        return mModelLayout;
    }
    
    
    abstract protected void populateViewHolder(VH viewHolder, T model, int position);
    

    所以我想通过int(1 - 带图像,2 - 没有)从T模型类型的项目中获取并根据此信息更改mModelLayout。我可以在那里添加if else语句吗?

4 个答案:

答案 0 :(得分:2)

这个答案建立在hatboysam的答案之上。我花了很长时间才弄清楚所有东西是如何组合在一起所以我认为我在这里彻底描述了解决方案:

    mRecyclerViewAdapter = new FirebaseRecyclerAdapter<DataObjectModel, ViewHolder>(
            DataObjectModel.class, R.layout.defaultitem_layout, ViewHolder.class, mDatabaseRef) {

        @Override
        public void populateViewHolder(ViewHolder viewholder, final DataObjectModel dataobject, int position) {

            if (getItemViewType(position) == R.layout.my_image_item){
                viewholder.setName(dataobject.getName()); 
            }
            else if (getItemViewType(position) == R.layout.my_no_image_item{
                viewholder.setText(dataobject.getText());
        }


        @Override
        public int getItemViewType(int position) {
            DataObjectModel dataobject = getItem(position);
            if (dataobject.getType() != null && dataobject.getType().equals("image")) {
                // Layout for an item with an image
                return  R.layout.my_image_item);
            } else if (dataobject.getType() != null && dataobject.getType().equals("noimage")) {
                // Layout for an item without an image
                return  R.layout.my_no_image_item;
            }
        }
    };

// getName()和setText()是您必须在&#34;公共静态类中编写的方法ChatHolder扩展了RecyclerView.ViewHolder&#34;类

//这里是ViewHolder Class for good measure

    public static class ViewHolder extends RecyclerView.ViewHolder {
        View mView;

        public ViewHolder (View itemView) {
            super(itemView);
            mView = itemView;
        }

        public void setName(String name) {
            TextView field = (TextView) mView.findViewById(R.id.name_text);
            field.setText(name);
        }

        public void setText(String text) {
            TextView field = (TextView) mView.findViewById(R.id.message_text);
            field.setText(text);
        }
}

答案 1 :(得分:1)

您需要覆盖getItemViewType方法以返回要加载的布局的资源ID。像这样:

@Override
public int getItemViewType(int position) {
    if (hasImage(position)) {
        // Layout for an item with an image
        return R.layout.my_image_item;
    } else {
        // Layout for an item without an image
        return R.layout.my_no_image_item;
    }
}

这会导致onCreateViewHolder为每个项目夸大正确的布局。您的ViewHolder类需要能够处理这两种项目类型。然后在populateViewHolder中,您可以根据项目位置填充相应的视图。

答案 2 :(得分:0)

尝试使其覆盖。

mAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(options) {

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

答案 3 :(得分:-1)

我在web-site找到了答案。

 mAdapter = new FirebaseRecyclerAdapter<User, RecyclerView.ViewHolder>(
            User.class, R.layout.item_user, RecyclerView.ViewHolder.class, ref) {
        @Override
        protected void populateViewHolder(final RecyclerView.ViewHolder viewHolder, final User user,
                                          final int position) {
            switch (user.getType()) {
                case Constants.USER_TYPE_1:
                    populateType1((ViewHolder1) viewHolder, user, position);
                    break;
                case Constants.USER_TYPE_2:
                    populateType2((ViewHolder2) viewHolder, user, position);
                    break;
                case Constants.USER_TYPE_3:
                    populateType3((ViewHolder3) viewHolder, user, position);
                    break;
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            switch (viewType) {
                case USER_TYPE_1:
                    View userType1 = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.item_type1, parent, false);
                    return new ViewHolder1(userType1);
                case USER_TYPE_2:
                    View userType2 = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.item_type2, parent, false);
                    return new ViewHolder2(userType2);
                case USER_TYPE_3:
                    View userType3 = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.item_type3, parent, false);
                    return new ViewHolder3(userType3);
            }
            return super.onCreateViewHolder(parent, viewType);
        }

        @Override
        public int getItemViewType(int position) {
            User user = getItem(position);
            switch (user.getType()) {
                case Constants.USER_TYPE_1:
                    return USER_TYPE_1;
                case Constants.USER_TYPE_2:
                    return USER_TYPE_2;
                case Constants.USER_TYPE_3:
                    return USER_TYPE_3;
            }
            return super.getItemViewType(position);
        }
    };