展开回收者视图的父项时填充子项目

Filling the Child items when expanding the parent of recycler view

在我的项目中,我必须在列表视图或回收视图中使用可扩展功能。我选择了 recyclerview,因为它的性能更好。

但我必须制作可扩展版本所以我得到了非常好的库 here 它真的很好用。但是后来我遇到了一个问题,那就是

请帮助我并告诉我正确的方法。至少对于第一次混淆。提前致谢

更新 1:

我在下面分享的源代码是这个库的演示代码 在这个代码片段中你可以看到他在创建ParentList(组列表)的对象的同时设置了子列表(Item List)。从 web 服务中获取后我想在哪里填充子列表

public class Recipe implements ParentListItem {

private String mName;
private List<Ingredient> mIngredients;

public Recipe(String name, List<Ingredient> ingredients) {
    mName = name;
    mIngredients = ingredients;
}

public String getName() {
    return mName;
}

@Override
public List<?> getChildItemList() {
    return mIngredients; // How can I return list after getting data from the web service 
}

@Override
public boolean isInitiallyExpanded() {
    return false;
}

}

然后他设置为

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycler_view_sample);

    Ingredient beef = new Ingredient("beef");
    Ingredient cheese = new Ingredient("cheese");
    Ingredient salsa = new Ingredient("salsa");
    Ingredient tortilla = new Ingredient("tortilla");
    Ingredient ketchup = new Ingredient("ketchup");
    Ingredient bun = new Ingredient("bun");

    Recipe taco = new Recipe("taco", Arrays.asList(beef, cheese, salsa, tortilla));
    Recipe quesadilla = new Recipe("quesadilla", Arrays.asList(cheese, tortilla));
    Recipe burger = new Recipe("burger", Arrays.asList(beef, cheese, ketchup, bun));
    final List<Recipe> recipes = Arrays.asList(taco, quesadilla, burger);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    mAdapter = new RecipeAdapter(this, recipes);
    mAdapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
        @Override
        public void onListItemExpanded(int position) {
            Recipe expandedRecipe = recipes.get(position);

            String toastMsg = getResources().getString(R.string.expanded, expandedRecipe.getName());
            Toast.makeText(VerticalLinearRecyclerViewSampleActivity.this,
                    toastMsg,
                    Toast.LENGTH_SHORT)
                    .show();
        }

        @Override
        public void onListItemCollapsed(int position) {
            Recipe collapsedRecipe = recipes.get(position);

            String toastMsg = getResources().getString(R.string.collapsed, collapsedRecipe.getName());
            Toast.makeText(VerticalLinearRecyclerViewSampleActivity.this,
                    toastMsg,
                    Toast.LENGTH_SHORT)
                    .show();
        }
    });

    recyclerView.setAdapter(mAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

error Log 当我尝试从 web 服务填充成分列表时发生此错误。我所做的是我将成分列表设为静态,并从 webservice 中静态设置它并从覆盖函数

中检索它

Process: com.naziraschool.nazraschoolsystem, PID: 484 java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.remove(ArrayList.java:403) at com.bignerdranch.expandablerecyclerview.Adapter.ExpandableRecyclerAdapter.collapseParentListItem(ExpandableRecyclerAdapter.java:604) at com.bignerdranch.expandablerecyclerview.Adapter.ExpandableRecyclerAdapter.onParentListItemCollapsed(ExpandableRecyclerAdapter.java:274) at com.bignerdranch.expandablerecyclerview.ViewHolder.ParentViewHolder.collapseView(ParentViewHolder.java:164) at com.bignerdranch.expandablerecyclerview.ViewHolder.ParentViewHolder.onClick(ParentViewHolder.java:124)

阅读这个库的文档,我看到以下片段可以让你监听父扩展

MyAdapter adapter = new MyAdapter(this, recipes);

adapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
    @Override
    public void onListItemExpanded(int position) {
        Recipe expandedRecipe = recipes.get(position);
        // ...
    }

    @Override
    public void onListItemCollapsed(int position) {
        Recipe collapsedRecipe = recipes.get(position);
        // ...
    }
});

mRecyclerView.setAdapter(adapter);

onListItemExpanded 方法中,您应该执行网络调用并在适配器中加载数据,然后调用 notifyDataSetChanged() 刷新数据。