维护分段的Recyclerview状态

时间:2019-05-14 06:43:09

标签: android android-recyclerview sectionedrecyclerviewadapter

我在SectionedRecyclerview上将Fragment设置为具有多个部分。在初始阶段,所有展开了项目的部分都是打开的。当我展开特定的SectionCollapse特定部分时,滚动Recyclerview,折叠和打开部分的状态将保留在单个片段上。但是,如果我更改Tab并再次进入相同的Fragment,则所有项目都会展开,并且状态不会保留。如何保持“分段的Recyclerview打开的分段”状态,当再次打开相同的片段时如何打开相同的分段。
这是我的StatelessSection

的代码
public class SectionOfferFeed extends StatelessSection {
        boolean expanded = true;
        private String title;
        ArrayList<Entry> entries;
        private SparseBooleanArray spa = new SparseBooleanArray();

        public SectionOfferFeed(String title, ArrayList<Entry> entries) {
            super(R.layout.section_header, R.layout.offercard_new);
            this.title = title;
            this.entries = entries;
        }

        @Override
        public int getContentItemsTotal() {
            return expanded ? entries.size() : 0;
        }

        @Override
        public RecyclerView.ViewHolder getItemViewHolder(View view) {
            return new ItemViewHolder(view);

        }

        @Override
        public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
            final ItemViewHolder iHolder = (ItemViewHolder) holder;
            Entry entry = entries.get(position);
            iHolder.offerN.setText(entry.getOfferName());
            iHolder.restName.setText(entry.getRestName());
            if (entry.getIsMarked() != null) {
                    if ((!spa.get(position, false)) && (entry.getIsMarked().equals("Not Marked"))) {
                        spa.put(position, false);
                        iHolder.mark.setVisibility(View.VISIBLE);
                        iHolder.marfFilled.setVisibility(View.GONE);
                    } else if ((spa.get(position, true) && (entry.getIsMarked().equals("Marked")))) {
                        spa.put(position, true);
                        iHolder.marfFilled.setVisibility(View.VISIBLE);
                        iHolder.mark.setVisibility(View.GONE);
                    }
            }
            if (entry.getIsFinal() != null) {
                    if (entry.getIsFinal().equals("false")) {
                        iHolder.isFInal.setVisibility(View.GONE);
                    } else {
                        iHolder.isFInal.setVisibility(View.VISIBLE);
                    }
            }
            if (entry.getRest_img() != null) {
                    String string = entry.getRest_img();
                    if (!entry.getRest_img().equals("null")) {
                        byte[] imageBytes = Base64.decode(string, Base64.DEFAULT);
                        //decode base64 string to image
                        imageBytes = Base64.decode(entry.getRest_img(), Base64.DEFAULT);
                        Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
                        iHolder.rest_image.setImageBitmap(decodedImage);
                     } else {
                        iHolder.rest_image.setImageResource(R.drawable.dish);
                    }

            }    
            iHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = adapter.getSectionPosition(iHolder.getAdapterPosition());

                }
            });
            iHolder.mark.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                }
            });

            iHolder.marfFilled.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                                       }
            });


        }

        @Override
        public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
            final HeaderViewHolder hHolder = (HeaderViewHolder) holder;
            final int Hposition = hHolder.getAdapterPosition();
            hHolder.headerTitle.setText(title);
            hHolder.showCount.setText(String.valueOf("(" + entries.size() + ")"));
            hHolder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    expanded = !expanded;
                    hHolder.Arrow.setImageResource(
                            expanded ? R.drawable.arrow_drop_up : R.drawable.arrow_drop_down
                    );
                    adapter.notifyDataSetChanged();
                }
            });
        }
        @Override
        public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
            return new HeaderViewHolder(view);
        }
    }  

如何保存和保留Section状态?

0 个答案:

没有答案