recylerview中等效的nameList.add(name)

时间:2016-04-19 05:39:54

标签: android

我通常习惯在列表视图中添加项目

            Name name = new Name("Via Button");
            nameList.add(name);
            mAdapter.notifyDataSetChanged();

我想在RecyclerView中了解相同的内容。非常感谢。

1 个答案:

答案 0 :(得分:0)

你可以定义一个循环器视图适配器,它取一个列表作为一个参数...在你的情况下列表...一旦你定义了适配器,你可以传递给这个适配器的类级别的列表。一旦获取数据(通过api调用或任何其他方式),您可以添加添加所有数据并将通知数据集调用到自定义适配器。 示例:

// custructor of your custom adapter
public CustomAdapter(List<Name> myList) {
    this.myList = myList;
}

//your class implementation
public class myClass{
private ArrayList<Name> myList = new ArrayList<>();
CustomAdapter myAdapter;
.
.
.
public void init(){
 //more code and init of components

 myAdapter = new CustomAdapter(myList);
 recyclerView.setAdapter(myAdapter);
}


  //more codes
//once you receive data from api call (maybe)
Name name = new Name("Via Button");
        myList.add(name);
 myAdapter.notifyDatasetchanged();
//more codes
}