加载后一一显示回收者视图项目,而不是全部显示

时间:2020-04-23 16:39:02

标签: android android-recyclerview adapter

我有一个包含多个对象和一个自定义适配器的列表。

ExampleAdapter adapter = new ExampleAdapter((ArrayList<MatchItem>) outputList, GamesActivity.this);
recyclerView.setAdapter(adapter);

问题在于,对于列表中的每个项目,在所有视图之后都会显示回收站视图,但是我想在加载后立即显示每个项目。

我不确定是否必须创建适配器,以便它一次仅将一个项目作为参数,并在添加每个项目之后更新数据。

我正在异步任务中填充列表,并在post execute方法中设置适配器

1 个答案:

答案 0 :(得分:0)

而不是将列表传递给适配器的构造函数,请尝试在适配器中添加一个方法,该方法一次接受一个项目,然后在适配器中填充列表:

public class ExampleAdapter extends......{

private List< MatchItem > list = new ArrayList<MatchItem>();

public ExampleAdapter(GamesActivity.this){


}

//this method will accept items one by one
public void addNewItem(MatchItem item){

list.add(item);

this.notifyDataSetChanged();

}

}

并在物品准备好时开始逐个传递物品

//put these as a one time thing
ExampleAdapter adapter = new ExampleAdapter(GamesActivity.this);
recyclerView.setAdapter(adapter);

//when you read items
adapter.addNewItem(item);
相关问题