2017-04-21 56 views
0

在我的應用程序中,我使用的回收視圖,其中包含一些項目,我想顯示項目之間的一個溺愛分隔符(分隔線),但它不工作。我試圖創建一個可繪製的形狀,但添加繪圖到DividerItemDecoration沒有空間或行之間顯示的回收商查看項目。 我曾嘗試創建自定義的DividerItemDecoration類,但沒有爲我工作。 注意:目前在我的可繪製形狀設置爲矩形我也試過線。 如何實現。任何幫助將不勝感激。 這是我的代碼。分隔線不顯示在回收查看項目

繪製對象:(customdrawableshape.xml)

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <stroke 
     android:width="3dp" 
     android:height="2dp" 
     android:color="#000000" 
     android:dashGap="10dp" 
     android:dashWidth="5dp" /> 

</shape> 

自定義行的回收視圖中的項目

<?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:layout_marginBottom="2dp"> 

    <TextView 
     android:id="@+id/tv_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

和代碼的部分我在哪裏設置項裝飾recyclerview

DividerItemDecoration dividerItemDecoration; 
recyclerview.setLayoutManager(linearlayoutmanager); 
dividerItemDecoration = new DividerItemDecoration(recyclerview.getContext(), 
     linearlayoutmanager.getOrientation()); 
dividerItemDecoration.setDrawable(ContextCompat.getDrawable(context, R.drawable.customdrawableshape)); 
recyclerview.addItemDecoration(dividerItemDecoration); 
+0

是recyclerview一個錯字後,額外的點'.'? – BlackHatSamurai

+0

[Android的 - 如何爲RecyclerView添加虛線/點分隔線?](http://stackoverflow.com/questions/39667195/android-how-to-add-a-dashed-dotted-separator-line -for-recyclerview) – BlackHatSamurai

+0

@BlackHat武士哦是的,這是一個打字錯誤在這裏 – User

回答

3

您可以使用DividerItemDecoration類添加行。

這裏是示例代碼

recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL)); 
+1

但我想要線被溺愛,這就是爲什麼我是自定義形狀 – User

+1

好吧,那麼你需要通過擴展RecyclerView.ItemDecoration類創建自定義分隔線 – LearnPainLess