public class MyAdapter extends RecyclerView.Adapter
...
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
...
}
@Override
public int getItemCount() {
...
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View view) {
super(view);
view.setLayoutParams(new RecyclerView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
}
通过以上两种方法,我们可以解决Android Studio Recycler View行之间存在间隙的问题。