使用SectionedGridRecyclerViewAdapter时如何删除RecyclerView中View_Type标头下的项目

时间:2019-06-14 13:14:38

标签: android android-recyclerview recycler-adapter

我有一个视频列表,显示在今天,昨天等标题下。如果用户选择任何一个视频,则应将其删除。目前,我能够实现删除功能,但问题是要删除的项目不是正确的项目。当我尝试使用holder.getAdapterPosition获取项目的位置时,它将为我提供包括我不想要的标题的位置。

以下是我在onBindViewHolder中拨打的电话:

 @Override
    public void onBindViewHolder(SimpleViewHolder holder, final int position) {

        Uri uri = Uri.fromFile(new File(video_files.get(position).getPath()));
        Glide.with(mContext).load(uri)
                .apply(new RequestOptions().placeholder(R.drawable.video_placeholder).error(R.drawable.video_placeholder))
                .into(holder.iv_videoPreview);
        if(isLongPressClicked){
            holder.iv_delete_overlay.setVisibility(View.VISIBLE);
        }else{
            holder.iv_delete_overlay.setVisibility(View.GONE);
        }
        holder.iv_play.setOnLongClickListener(new View.OnLongClickListener(){

            @Override
            public boolean onLongClick(View v) {
                boolean isLongPressed;
                if(isLongPressClicked){
                    isLongPressed =false;
                }else{
                    isLongPressed =true;
                }

                iGalleryVideoClickListener.onVideoLongClick(v, holder.getAdapterPosition(), isLongPressed);
                return true;
            }
        });

        holder.iv_delete_overlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iGalleryVideoClickListener.onOverlayClick(v, holder.getAdapterPosition(),sections);
            }
        });
    }

设置列表标题的代码:

//This is the code to provide a sectioned grid
        List<SectionedGridRecyclerViewAdapter.Section> sections = new ArrayList<SectionedGridRecyclerViewAdapter.Section>();

        for (int i = 0; i < headerPos.size(); i++) {
            sections.add(new SectionedGridRecyclerViewAdapter.Section(headerPos.get(i), headerText.get(i)));
        }

        //Add your adapter to the sectionAdapter
        SectionedGridRecyclerViewAdapter.Section[] dummy = new SectionedGridRecyclerViewAdapter.Section[sections.size()];
        SectionedGridRecyclerViewAdapter mSectionedAdapter = new SectionedGridRecyclerViewAdapter(getActivity(), R.layout.gallery_header, R.id.section_text, mRecyclerView, mAdapter);
        mSectionedAdapter.setSections(sections.toArray(dummy));
        //Apply this adapter to the RecyclerView
        mRecyclerView.setAdapter(mSectionedAdapter);

以下是我删除文件的方法:

  public void deleteSingleVideo(View v, int pos, List<SectionedGridRecyclerViewAdapter.Section> sections){

            Uri mediaStoreUri = Uri.fromFile(new File(video_files.get(pos).getPath()));
            File fdelete = new File(mediaStoreUri.getPath());
            fdelete.delete();
            ((ReviewFragment)fragment).refreshScreenAfterDeletion();
    }

一切正常,只是我没有得到正确的位置。如何获取仅视频文件的位置(不包括标题位置)。

1 个答案:

答案 0 :(得分:0)

您似乎正在使用SectionedGridRecyclerViewAdapter

您将需要使用mSectionedAdapter.sectionedPositionToPosition(int position)来获取商品的实际位置。