ExpandableRecyclerAdapter将数据保存在片段之间

时间:2017-06-14 17:09:07

标签: android android-fragments

我在使用bignerdranch's ExpandableRecyclerView时遇到了困难。我即将开始研究片段(见图1和图2)。但是,当我选择具有不同布局的不同片段时,数据似乎保留在ExpandableRecyclerView中(参见图片3和4)。

(我没有足够的代表发布个人链接,因此照片在imgur album - 对不起!)

这是我的ExpandableRecyclerAdapter

toString()

1 个答案:

答案 0 :(得分:0)

对于遇到此问题的任何人来说,解决方案非常简单,但值得理解这个问题。在我的ExpandableRecyclerAdapter中,您可以看到我正在尝试使用

将其设为模板
if (o instanceof assignment_class)

但是,我没有考虑我的TitleCreator类负责创建每个“行”,其中包含负责子项的ParentViewClass列表(下拉项)。

解决方案如下:

static TitleCreator _titleCreator; 
private static List<?> ListOfObjects,OldList;
List<ParentViewClass> ClassNameArrayList;

public TitleCreator(Context context) {
    ClassNameArrayList = new ArrayList<>();
    OldList = ListOfObjects;
    if (DetermineType() == RecylerAdapter.typeOfObjectsList.ASSIGN_CLASS) {
        for (int i = 0; i < ListOfObjects.size(); i++) {
            ParentViewClass ParentView = new ParentViewClass(((assignment_class) ListOfObjects.get(i)).ass_name.toString());
            ClassNameArrayList.add(ParentView);
        }
    }
    if (DetermineType() == RecylerAdapter.typeOfObjectsList.QUEUETASK) {
        for (int i = 0; i < ListOfObjects.size(); i++) {
            ParentViewClass ParentView = new ParentViewClass((ListOfObjects.get(i)).toString());
            ClassNameArrayList.add(ParentView);
        }
    }
}

 public static TitleCreator get(Context context, List<?> classList) {
    ListOfObjects = classList;
    if (_titleCreator == null || OldList != classList)
        _titleCreator = new TitleCreator(context);

   return _titleCreator;
}

//This lets us template RecyclerAdapter so
public RecylerAdapter.typeOfObjectsList DetermineType() {
    //Since we don't know the type, we have to deduce it. This is done by comparing the 0th element to expected types
    if (!ListOfObjects.isEmpty()) {
        if (ListOfObjects.get(0) instanceof myQueueTask)
            return RecylerAdapter.typeOfObjectsList.QUEUETASK;
        if (ListOfObjects.get(0) instanceof assignment_class)
            return RecylerAdapter.typeOfObjectsList.ASSIGN_CLASS;
    }
    if (!ListOfObjects.isEmpty()) {
        Log.d("ClassType", ListOfObjects.get(0).getClass().toString() + " has not been defined in TitleCreator.java");
    } else {
        Log.d("TitleCreator", "Encountered a unknown class type. You're probably not seeing anything so go to TitleCreator.java to define what you want to view ");
    }
    return RecylerAdapter.typeOfObjectsList.NAN;
}

public List<ParentViewClass> getAll() {
    return ClassNameArrayList;
}


public enum typeOfObjectsList {
    ASSIGN_CLASS, QUEUETASK, NAN
}

该行

OldList = ListOfObjects;
调用新的TitleCreator(上下文)时调用

。在两种情况下调用此方法:
1)if( _titleCreator == null || OldList!= classList)由于短路评估,它最初将评估为_titleCreator = null,因为它尚未启动。

2)if(_titleCreator == null || OldList!= classList )如果存在已存在的_titleCreator和OldList(在创建新的TitleCreator时设置为classlist),将对此进行评估)等于参数中传递的List。

虽然我不熟悉软件设计,但我认为问题在于我使用TitlerCreator作为Singleton Pattern,因此信息在片段之间保存。