在展开回收站视图的父级时填充子项目

时间:2016-08-17 03:34:32

标签: android android-recyclerview expandablerecyclerview

在我的项目中,我必须使用listview中的Expandable功能或recyclerview。我选择了recyclerview,因为它的性能更好。

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

一样
  • 我想填写来自Web服务的子视图信息,当用户点击父项时应该调用它。我不知道如何使用相同的库
  • 我想制作永无止境的RecyclerView,所以它的意思是我必须使用一些东西,如拉动刷新类型的东西,但这将发生在用户将到达RecyclerView的末尾。知道如何使用相同的库实现这两个功能吗?

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

更新1:

我在下面分享的源代码是该库演示的代码 在此代码snippt中,您可以看到他在创建ParentList(组列表)的对象时设置子列表(Item List)。我想从webservice获取后填写子列表

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));

错误日志当我尝试从webservice填充成分列表时,会发生此错误。我做了什么我把成分列表静态化,从webservice我静态设置它并从覆盖函数中检索它

  

流程:com.naziraschool.nazraschoolsystem,PID:484                                                                                   java.lang.IndexOutOfBoundsException:索引4无效,大小为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)                                                                                       在com.bignerdranch.expandablerecyclerview.ViewHolder.ParentViewHolder.collapseView(ParentViewHolder.java:164)                                                                                       在com.bignerdranch.expandablerecyclerview.ViewHolder.ParentViewHolder.onClick(ParentViewHolder.java:124)

1 个答案:

答案 0 :(得分:0)

阅读此库的文档,我看到以下代码段可以让您听取父扩展

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()来刷新数据。