我有一个RecyclerView。当您查看图片时,我遇到了问题,即RecyclerView的项目不会填满整个屏幕空间。 (我的意思是item_note和屏幕边缘之间的空间......
这是我的main_activity.XML:
<android.support.v7.widget.RecyclerView
android:id="@+id/rvNoteList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
这是我的item_layout.XM:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:background="@drawable/note_bg">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="9"
android:orientation="vertical">
<TextView
android:id="@+id/tvTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Title"
android:textStyle="bold"
android:textSize="18sp" />
<TextView
android:id="@+id/tvContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Text for Content" />
</LinearLayout>
以下是我在MainActivity.java中设置布局的方法:
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
recyclerView.setItemAnimator(new DefaultItemAnimator());
修改 click to see
编辑2: click to see
编辑3:
适配器:
adapter = new FirestoreRecyclerAdapter<Note, NoteViewHolder>(response) {
@Override
protected void onBindViewHolder(NoteViewHolder holder, int position, Note model) {
final Note note = notesList.get(position);
holder.title.setText(note.getTitle());
holder.content.setText(note.getContent());
}
@Override
public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note, parent, false);
return new NoteViewHolder(view);
}
@Override
public void onError(FirebaseFirestoreException e) {
Log.e("error", e.getMessage());
}
};
编辑4:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#CABBBBBB"/>
<corners android:radius="2dp" />
</shape>
</item>
<item
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
答案 0 :(得分:1)
如果您不想要如您所述的差距,您可能不想使用StaggeredGridLayoutManager
,因为它会尝试在某些行的末尾添加间隙创建锯齿状边缘效果。有关详细信息,请参阅https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html。
对于间隔均匀的项目,您应使用GridLayoutManager
。所以,试着改变
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
要
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
有关如何使用GridLayoutManager
的详细信息,请参阅https://developer.android.com/reference/android/support/v7/widget/GridLayoutManager.html#GridLayoutManager(android.content.Context,%20int)。
如果您想坚持StaggeredGridLayoutManager
,可以尝试将差距处理策略设置为GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
,如下所示:
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
recyclerView.setLayoutManager(layoutManager);
尝试将item_layout.xml
的内容更改为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:background="@drawable/note_bg">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Title"
android:textStyle="bold"
android:textSize="18sp" />
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Text for Content" />
</LinearLayout>
使用替代解决方案2 和StaggeredGridLayoutManager
,我能够获得以下结果(使用您商品中的相同文字):