如何使“回收者视图”中的相邻项目部分可见?

时间:2019-03-02 06:29:13

标签: android android-recyclerview

我正在尝试使用大尺寸图像实现RecylerView。但是,每一项的相邻元素都应该像ViewPager那样使用setPageMargin()方法时出现。此recylerView应该垂直滚动

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用RecyclerViewPager库,在build.gradle中添加:

编译'com.github.lsjwzh.RecyclerViewPager:lib:v1.2.0@aar'

xml:

<com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    app:rvp_triggerOffset="0.1"
    app:rvp_singlePageFling="true"
    android:clipToPadding="false"/>

代码:

RecyclerViewPager mRecyclerView = (RecyclerViewPager) view.findViewById(R.id.list);

// setLayoutManager like normal RecyclerView, you do not need to change any thing.
LinearLayoutManager layout = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
mRecyclerView.setLayoutManager(layout);

//set adapter
//You just need to implement ViewPageAdapter by yourself like a normal RecyclerView.Adpater.
mRecyclerView.setAdapter(new RecyclerView.Adpater<X>());

// That is all.

更新

将此maven { url "https://jitpack.io" }添加到allprojects->repositories块的build.gradle(项目级别)下,您的代码应如下所示:

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}