在我的项目中,我必须使用listview中的Expandable功能或recyclerview。我选择了recyclerview,因为它的性能更好。
但我必须制作可扩展版本所以我在here上获得了非常好的库 它真的很好用。但后来我遇到了问题,就像
一样请帮助我并告诉我正确的方法。至少是第一次混乱。提前致谢
更新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)
答案 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()
来刷新数据。