Android 如何仅在滚动时在项目之间添加空间
我有水平循环视图,显示在全屏上。 这是我的活动布局:Android 如何仅在滚动时在项目之间添加空间,android,android-recyclerview,Android,Android Recyclerview,我有水平循环视图,显示在全屏上。 这是我的活动布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <androi
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/photos_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
在屏幕截图上,我展示了我想要的空间:
要获得此空间,应使用DividerItemDecoration。您可以设置它的宽度和颜色。比如说, 可提取: 或者在回收机闲置时,像这样移除
recyclerView.removeItemDecoration(dividerItemDecoration);
要检测回收器是否正在滚动,请实现RecyclerView.OnScrollListener
RecyclerView photoList = findViewById(R.id.photos_list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false);
photoList.setLayoutManager(linearLayoutManager);
PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
pagerSnapHelper.attachToRecyclerView(photoList);
PhotoListAdapter photoListAdapter = new PhotoListAdapter(this, photoResults);
photoList.setAdapter(photoListAdapter);
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="1dp"
android:height="1dp" />
<solid android:color="@color/primary" />
</shape>
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(),
LinearLayoutManager.VERTICAL);//or HORIZONTAL
dividerItemDecoration.setDrawable(getContext().getResources().getDrawable(R.drawable.sk_line_divider));
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.removeItemDecoration(dividerItemDecoration);