Rebel-Cat
@Rebel-Cat

Как Удалить дочерний элемент из основного через swipe (оба содержат RecyclerView)?

На java
Есть основная Активити - MainActivity
Основной адаптер - MainRecyclerAdapter
демонстрирующий List sectionList;

Основной элемент - Section, содержащий :
String sectionName,
List sectionItems

Дочерний адаптер - ChildRecyclerAdapter
демонстрирующий List sectionItems

Возникла проблема определения позиции элемент. Все мои попытки вызывали хаотичное удаление элементов и непредсказуемое поведение адаптера с периодическим исключением IndexOutOfBoundException
Кто нибудь реализовывал подобное?

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

    List<Section> sectionList;
    ChildRecyclerAdapter childRecyclerAdapter;


    public MainRecyclerAdapter(List<Section> sectionList) {
        this.sectionList = sectionList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.section_row, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        Section section = sectionList.get(position);
        String sectionName = section.getSectionName();
        List<String> items = section.getSectionItems();

        holder.sectionNameTextView.setText(sectionName);

        childRecyclerAdapter = new ChildRecyclerAdapter(items);
        holder.childRecyclerView.setAdapter(childRecyclerAdapter);
        
     
    }

    @Override
    public int getItemCount() {
        return sectionList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView sectionNameTextView;
        RecyclerView childRecyclerView;

        public ViewHolder(@NonNull final View itemView) {
            super(itemView);

            sectionNameTextView = itemView.findViewById(R.id.sectionNameTextView);
            childRecyclerView = itemView.findViewById(R.id.childRecyclerView);


            ItemTouchHelper helper = new ItemTouchHelper(
                    new MainRecyclerAdapter.ItemTouchHandler(getAdapterPosition(),
                            ItemTouchHelper.LEFT)
            );

            helper.attachToRecyclerView(childRecyclerView);
        }
    }

    private class ItemTouchHandler extends ItemTouchHelper.SimpleCallback {

        ItemTouchHandler(int dragDirs, int swipeDirs) {
            super(dragDirs, swipeDirs);
        }

        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            int from = viewHolder.getAdapterPosition();
            int to = target.getAdapterPosition();

            Collections.swap(sectionList.get(viewHolder.getAdapterPosition()).getSectionItems(), from, to);
            notifyItemMoved(from, to);

            return true;
        }


        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {

//I don't understand how to determine the position

            sectionList.get( ?? ).getSectionItems().remove(viewHolder.getAdapterPosition());
            childRecyclerAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());

        }
    }
}


5f787dc4b151c983808930.jpeg
  • Вопрос задан
  • 202 просмотра
Решения вопроса 1
zagayevskiy
@zagayevskiy Куратор тега Java
Android developer at Yandex
Вложенный ресайклер в этом случае не нужен. Секции разруливаются вьютайпами.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы