我在主布局上以这种方式创建简单的recyclerView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.safarayaneh.recycletest.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
然后为它创建适配器:
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyHolder> {
private List<User> users;
public RecycleAdapter(List<User> users) {
this.users = users;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_item, parent, false);
MyHolder holder = new MyHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
User user = users.get(position);
holder.name.setText(user.getName());
holder.lName.setText(user.getlName());
}
@Override
public int getItemCount() {
return 0;
}
public class MyHolder extends RecyclerView.ViewHolder {
TextView name;
TextView lName;
public MyHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.tvName);
lName = (TextView) itemView.findViewById(R.id.tvfamily);
}
}
}
这是我的Activity类:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<User> users = new ArrayList<>();
for (int i = 0; i < 20; i++) {
users.add(new User("pedro " + i, "ggg " + i));
}
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycleView);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
RecycleAdapter adapter = new RecycleAdapter(users);
recyclerView.setAdapter(adapter);
}
}
和我的pojo用户类:
public class User implements Serializable {
private String name;
private String lName;
public User(String name, String lName) {
this.name = name;
this.lName = lName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
}
但我不知道为什么我的简单列表不会出现在我的回收站视图中? 我以前创造过,一切都很好,但现在我不知道为什么我不能! 究竟我的错误在哪里?
答案 0 :(得分:4)
只需在RecycleAdapter上更新:
@Override
public int getItemCount() {
return users.size();
}
答案 1 :(得分:2)
尝试此更改 List
getItemCount
答案 2 :(得分:0)
还尝试使用以下行:(在recyclerView.setLayoutManager(manager)之后)
recyclerView.setItemAnimator(new DefaultItemAnimator());
也在适配器类中:
@Override
public int getItemCount() {
return (null != this.users ? this.users.size() : 0);
}
答案 3 :(得分:-1)
recyclerView项目的计数来自方法getItemCount(),因此您需要返回大小。
public int getItemCount(){return users.size();}