我正在尝试创建一个简单的DiffUtil适配器以与RecyclerView一起使用,但没有任何显示(我只得到一个空白活动)。这是代码:
MainActivity.java
:
public class MainActivity extends AppCompatActivity {
private MyAdapter mAdapter;
private ArrayList<User> myDataset;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter();
myDataset = new ArrayList<User>();
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
myDataset.add(new User("User1"));
myDataset.add(new User("User2"));
myDataset.add(new User("User3"));
mRecyclerView.setAdapter(mAdapter);
mAdapter.updateList(myDataset);
}
}
这里是MyAdapter.java
:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private AsyncListDiffer<User> mAsyncListDiffer;
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView singleItemTextView;
public View layout;
public ConstraintLayout constraintLayout;
public MyViewHolder(View v) {
super(v);
layout = v;
singleItemTextView = (TextView) v.findViewById(R.id.singleItemTextView);
constraintLayout = (ConstraintLayout) v.findViewById(R.id.constraintLayout);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter() {
DiffUtil.ItemCallback<User> diffUtilCallback = new DiffUtil.ItemCallback<User>() {
@Override
public boolean areItemsTheSame(@NonNull User newUser, @NonNull User oldUser) {
return newUser.getUserId().equals(oldUser.getUserId());
}
@Override
public boolean areContentsTheSame(@NonNull User newUser, @NonNull User oldUser) {
return newUser.equals(oldUser);
}
};
mAsyncListDiffer = new AsyncListDiffer<>(this, diffUtilCallback);
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_text_view, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
User user = mAsyncListDiffer.getCurrentList().get(position);
holder.singleItemTextView.setText(user.getUserId());
}
@Override
public int getItemCount() {
return mAsyncListDiffer.getCurrentList().size();
}
public void updateList(ArrayList<User> newList) {
mAsyncListDiffer.submitList(newList);
}
}
这里是User.java
:
public class User {
private String mUserId;
public User(String userId) {
mUserId = userId;
}
public String getUserId() {
return mUserId;
}
}
以下是XML文件:
acivity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是single_text_view.xml
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/singleItemTextView"
android:layout_width="78dp"
android:layout_height="78dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我知道DiffUtil应该比较两个不同的列表,这里我只设置了初始列表,但是仍然不应该至少显示第一个列表(在这种情况下为3个用户)?
答案 0 :(得分:0)
您是否检查自己的getItemCount()
方法是否返回大于0的值?
如果是,则应查看项目视图。首先尝试删除此视图上的约束,以查看屏幕上是否出现任何内容。
答案 1 :(得分:0)
使用Recyclerview设置LayoutManager
// add data on list
private ArrayList<User> myDataset=new ArrayList;
myDataset.add(new User("abc"));
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
LayoutManager layoutManager=new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
MyAdapter myAdapter=new MyAdapter();
mAdapter.updateList(myDataset);
mRecyclerView.setAdapter(mAdapter);