Preventing/catching “IllegalArgumentException: parameter must be a descendant of this view” error

后端 未结 15 1601
北恋
北恋 2020-11-27 13:46

I have a ListView with some focusable components inside (mostly EditTexts). Yeah, I know this isn\'t exactly recommended, but in general, almost everything is w

15条回答
  •  悲&欢浪女
    2020-11-27 14:15

    I'm using RecyclerView and none of the presented solutions worked. I got the error when removing items.

    What did work was overriding the Adapter's 'onItemDismiss(int position)' so that it first does a 'notifyDataSetChanged()' prior to removing the item and then does 'notifyItemRemoved(position)' after removing the item. Like this:

    // Adapter code
    @Override
    public void onItemDismiss(int position) {
        if (position >= 0 && getTheList() != null && getTheList().size() > position) {
            notifyDataSetChanged();  // <--- this fixed it.
            getTheList().remove(position);
            scrollToPosition(position);
            notifyItemRemoved(position);
        }
    }
    

    Also do an Override of 'removeAt(int position)' in the TabFragment to invoke the new cleanup code, like this:

    // TabFragment code
    @Override
    public void removeAt(int position) {
        mAdapter.onItemDismiss(position);
        mAdapter.notifyItemRemoved(position); // <--- I put an extra notify here too
    }
    

提交回复
热议问题