对话框显示后如何从 RecyclerView 恢复刷过的项目?

How to restore swiped item from RecyclerView after dialog show?

我正在使用 ItemTouchHelperAlertDialog。问题是当用户将元素滑动到右侧时。 AlertDialogEditText 显示在屏幕上。现在我想做这样的事情:

所以我尝试了两种解决方案,但都没有成功。

itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
                ItemTouchHelper.RIGHT) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                return false;
            }
            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
                    position = viewHolder.getAbsoluteAdapterPosition(); // 2. solution
                    openDialog();
                    vieholder2 = viewHolder; // 1. Solution
            }
        });
        itemTouchHelper.attachToRecyclerView(recyclerView);

首先我尝试实现自己的界面,在用户按下“取消”后 button 界面应该被调用并设置如下代码:
itemTouchHelper.startSwipe(vieholder2);

第二个:

接口调用后我设置mAdapter.notifyItemChanged(position);
两种解决方案都很好,直接在 ItemTouchHelper 中的 onSwiped 方法中,但是在触发方法并且显示 Dialog 之后。解决方案无效。

那么如何在 Dialog 上单击“取消”button 后 return 将不可见的项目返回到 RecyclerView 列表?

如果有帮助,我正在使用 LiveData。

编辑
片段 class.

package com.example.application;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Main_Database_fragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Main_Database_fragment extends Fragment implements Dialog_Amount.Dialog_AmountListener {

    private View rootView;
    private RecyclerView recyclerView;
    private Recycler_Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private Item_get_VIEWMODEL item_get_viewmodel;
    private ActivityResultLauncher<Intent> start_next_activity;
    private Item_get item_get;
    int pospos;
    ItemTouchHelper itemTouchHelper;
    RecyclerView.ViewHolder viewholder;
    int deleted = 0;
    private int id;
    private int type;
    private int package_category;
    Item_get item_get2;
    int positionItem;
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public Main_Database_fragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Main_Database_fragment.
     */
    // TODO: Rename and change types and number of parameters
    public static Main_Database_fragment newInstance(String param1, String param2) {
        Main_Database_fragment fragment = new Main_Database_fragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_main__database_fragment,container,false);
        return rootView;
    }


    @Override
    public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
        build_RecyclerView();
        mAdapter.setOnItemClickListener(new Recycler_Adapters_items() {

            @Override
            public void onEditClick(Item_get item_get) {
                Intent intent = new Intent(getActivity(), Edit_Delete_details.class);
                type = item_get.food.getCode();
                package_category = item_get.package_type.getCode();
                intent.putExtra(Edit_Delete_details.EXTRA_DATA, item_get);
                intent.putExtra(Edit_Delete_details.EXTRA_ID, item_get.getId());
                intent.putExtra(Edit_Delete_details.EXTRA_CATEGORY, type);
                intent.putExtra(Edit_Delete_details.EXTRA_PACKAGE_TYPE,package_category);
                start_next_activity.launch(intent);
            }

            @Override
            public void onDeleteClick(Item_get item_get) {
                item_get_viewmodel.delete(item_get);
            }

            @Override
            public void onItemClick(Item_get item_get) {
                Intent intent = new Intent(getActivity(), Product_specific_detail.class);
                intent.putExtra(Product_specific_detail.EXTRA_ID_2, item_get.getId());
                start_next_activity.launch(intent);

            }
        });
        item_get_viewmodel = new ViewModelProvider(this).get(Item_get_VIEWMODEL.class);
        item_get_viewmodel.getAll_ItemsModel().observe(getViewLifecycleOwner(), new Observer<List<Item_get>>() {
            @Override
            public void onChanged(List<Item_get> item_gets) {
                mAdapter.submitList(item_gets);
                mAdapter.copylist(item_gets);
            }
        });

        itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
                ItemTouchHelper.RIGHT) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                return false;
            }

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

                positionItem = viewHolder.getAbsoluteAdapterPosition();
                openDialog();
                item_get2 = mAdapter.getItemAt(viewHolder.getAbsoluteAdapterPosition());
                deleted = viewHolder.getAbsoluteAdapterPosition();
                viewholder = viewHolder;
            }
        });
        itemTouchHelper.attachToRecyclerView(recyclerView);
        super.onViewCreated(view, savedInstanceState);
    }
    public void build_RecyclerView(){
        recyclerView = rootView.findViewById(R.id.recycler_edit_del_product);
        recyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        mAdapter = new Recycler_Adapter();
        recyclerView.setAdapter(mAdapter);
        start_next_activity = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if(result.getResultCode() == Activity.RESULT_OK){
                            Intent intent = result.getData();
                            assert intent != null;
                            id = intent.getIntExtra(Edit_Delete_details.EXTRA_ID,-1);
                            if(id == -1){
                                Toast.makeText(getContext(),"Error", Toast.LENGTH_SHORT).show();
                                return;
                            }else if(id == -5){
                                item_get = intent.getParcelableExtra(Edit_Delete_details.EXTRA_DATA);
                                item_get.setId(id);
                                item_get_viewmodel.delete(item_get);
                                Toast.makeText(getContext(),"Delete",Toast.LENGTH_SHORT).show();
                            }else {
                                item_get = intent.getParcelableExtra(Edit_Delete_details.EXTRA_DATA);
                                type = intent.getIntExtra(Edit_Delete_details.EXTRA_CATEGORY, -1);
                                package_category = intent.getIntExtra(Edit_Delete_details.EXTRA_PACKAGE_TYPE, -1);
                                if(type == -1){
                                    Toast.makeText(getContext(),"Category may not be updated", Toast.LENGTH_LONG).show();
                                }else {
                                    set_food_type(type);
                                }
                                if(package_category == -1){
                                    Toast.makeText(getContext(),"Packages may not be updated", Toast.LENGTH_LONG).show();
                                }else {
                                    set_packages_type(package_category);
                                }
                                item_get.setId(id);
                                item_get_viewmodel.update(item_get);
                                Toast.makeText(getContext(),"Update",Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                }
        );

    }


    //Set our foodType from int to enum and later we pass it to db to update;
    public void set_food_type(int type){
        switch (type){
            case 1:
                item_get.food = Item_get.Food.SWEETS;
                break;
            case 2:
                item_get.food = Item_get.Food.VEGETABLES_FRUITS;
                break;
            case 3:
                item_get.food = Item_get.Food.DRINKS;
                break;
            case 4:
                item_get.food = Item_get.Food.OTHER;
                break;
            default:
                break;
        }
    }
    // Same as it is with food. Difference is Package Category enum not Package Type enum;
    public void set_packages_type(int type){
        switch (type){
            case 1:
                item_get.package_type = Item_get.Package_type.PACKAGE_BOX;
                break;
            case 2:
                item_get.package_type = Item_get.Package_type.PIECE;
                break;
            case 3:
                item_get.package_type = Item_get.Package_type.OTHER_PACK;
                break;
            default:
                break;
        }
    }
    public void openDialog(){
        Dialog_Amount dialog_amount = new Dialog_Amount();
        dialog_amount.setCancelable(false);
        dialog_amount.show(getChildFragmentManager(),"This is dialog");
    }

    @Override
    public void applyText(int text) {
        if(text != -1){
            item_get2.setNameProduct(item_get2.getNameProduct());
            item_get2.setDate_buy(null);
            item_get2.setAmount(0);
            int a = item_get2.food.getCode();
            int b = item_get2.package_type.getCode();
            item_get2.setPrice(0.0);
            item_get2.setValue(0.0);
            item_get2.setTo_shop(1);
            item_get2.setAmount_toShop(text);
            item_get_viewmodel.update(item_get2);

        }else {
            mAdapter.add_SingleItem(positionItem,item_get2);
            mAdapter.notifyItemInserted(positionItem);
        }
    }
}

我的适配器。

package com.example.application;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

public class Recycler_Adapter extends ListAdapter<Item_get,Recycler_Adapter.recyclerViewHolder> {

    public Recycler_Adapters_items clickListener;
    private List<Item_get> data_list2;
    private List<Item_get> data_list2_full;
    public Recycler_Adapter() {
        super(DIFF_CALLBACK);

    }
    private static final DiffUtil.ItemCallback<Item_get> DIFF_CALLBACK = new DiffUtil.ItemCallback<Item_get>() {
        @Override
        public boolean areItemsTheSame(@NonNull Item_get oldItem, @NonNull Item_get newItem) {
            return oldItem.getId() == newItem.getId();
        }

        @Override
        public boolean areContentsTheSame(@NonNull Item_get oldItem, @NonNull Item_get newItem) {
            return oldItem.getNameProduct().equals(newItem.getNameProduct()) && oldItem.getAmount() == newItem.getAmount()
                    && oldItem.food.getCode() == newItem.food.getCode() && oldItem.package_type.getCode() == newItem.package_type.getCode();
        }
    };

    public void copylist(List<Item_get> item_gets){
        if (data_list2 != null){
            data_list2.clear();
        }else {
            data_list2 = new ArrayList<>();
        }
        data_list2.addAll(item_gets);
        if(data_list2_full != null){
            data_list2_full.clear();
            data_list2_full.addAll(data_list2);
        }else{
            data_list2_full = new ArrayList<>(data_list2);
        }

    }

   public void add_SingleItem(int position, List<Item_get> item_get){
        item_get.add(position, (Item_get) item_get);
    }

    public void setOnItemClickListener(Recycler_Adapters_items mClickListener) {
        clickListener = mClickListener;
    }


    public  class recyclerViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImageView1;
        public ImageView mImageView2;
        public ImageView mImageView3;
        public TextView mTextView1;
        public TextView mTextView2;
        public TextView mTextView3;
        public TextView mTextView4;

        public recyclerViewHolder(final View itemView, final Recycler_Adapters_items listener) {
            super(itemView);
            mImageView1 = itemView.findViewById(R.id.rec_image);
            mImageView2 = itemView.findViewById(R.id.rec_image_edit);
            mImageView3 = itemView.findViewById(R.id.rec_image_del);
            mTextView1 = itemView.findViewById(R.id.rec_text_1);
            mTextView2 = itemView.findViewById(R.id.rec_text_2);
            mTextView3 = itemView.findViewById(R.id.rec_text_3);
            mTextView4 = itemView.findViewById(R.id.rect_text_4);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAbsoluteAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onItemClick(getItem(position));
                        }
                    }
                }
            });
            mImageView2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAbsoluteAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onEditClick(getItem(position));
                        }
                    }
                }
            });
            mImageView3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAbsoluteAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onDeleteClick(getItem(position));
                        }
                    }
                }
            });
        }
    }


    @NonNull
    @Override
    public recyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_template, parent, false);
        return new recyclerViewHolder(v,clickListener);

    }

    @Override
    public void onBindViewHolder(@NonNull recyclerViewHolder holder, int position) {
        Item_get currentItem = getItem(position);
        currentItem.setId(currentItem.getId());
        holder.mTextView1.setText(currentItem.getNameProduct());

        if (currentItem.food == Item_get.Food.SWEETS) {
            holder.mTextView2.setText(R.string.sweets);
        }
        if (currentItem.food == Item_get.Food.Vegetables) {
            holder.mTextView2.setText(R.string.vegetables);
        }
        if (currentItem.food == Item_get.Food.drinks) {
            holder.mTextView2.setText(R.string.drinks);
        }
        if (currentItem.food == Item_get.Food.other) {
            holder.mTextView2.setText(R.string.other);
        }


        String amount = String.valueOf(currentItem.getAmount());
        holder.mTextView3.setText(amount);

        if(currentItem.package_type == Item_get.Package_type.PACKAGE_BOX){
            holder.mTextView4.setText(R.string.product_cat_pack_short);
        }else if(currentItem.package_type == Item_get.Package_type.PIECE){
            holder.mTextView4.setText(R.string.product_cat_piece);
        } else if(currentItem.package_type == Item_get.Package_type.OTHER_PACK){
            holder.mTextView4.setText(R.string.product_amount);
        }
        else {
            holder.mTextView4.setText(R.string.product_amount);
        }
    }

    public Item_get getItemAt(int position){
        return getItem(position);
    }
}

您需要在片段中执行此操作 class

  madapter.notifyItemChanged(viewholder.getAbsoluteAdapterPostion());