2016-04-17 23 views
0

上に複数の線を描画します。 これらの行は正確な位置になければなりません。なぜなら、それらの行の間に収まる必要がある要素のリストがあるからです。リストに要素が追加されていなくても、各要素に線を追加するだけでよいが、線を描く必要がある。は、私がRecyclerViewの背景に複数の水平線を描画しようとしているRecyclerView背景

にはどうすればいいの背景に線を引くことができますか? (私は.xmlからできません)あなたの時間をありがとう!

Example image

+0

イメージを投稿できますか? –

+0

サンプル画像を投稿しました! – Starivore

答えて

0

リスト仕切りを描きたいように見えます。私は、あなたがtranslationYを占め確認するデコレータを書くときItemDecoration

を使用したいと思います(例えばlayoutManager.getDecoratedBottom(ビュー))

他の装飾品からアイテムオフセット(アイテムが追加/アニメーションを削除扱い)
public class DividerItemDecoration extends RecyclerView.ItemDecoration { 
    private static final int[] ATTRS = new int[]{ 
      android.R.attr.listDivider 
    }; 

    private Drawable mDivider; 

    public DividerItemDecoration(Context context) { 
     final TypedArray a = context.obtainStyledAttributes(ATTRS); 
     mDivider = a.getDrawable(0); 
     a.recycle(); 
    } 

    @Override 
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { 
     int left = parent.getLeft(); 
     int right = parent.getRight(); 
     RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 

     int childCount = parent.getChildCount(); 
     for (int i = 0; i < childCount; i++) { 
      View child = parent.getChildAt(i); 
      int ty = (int) (child.getTranslationY() + 0.5f); 
      int top = layoutManager.getDecoratedBottom(child) + ty; 
      int bottom = top + mDivider.getIntrinsicHeight(); 
      mDivider.setBounds(left, top, right, bottom); 
      mDivider.draw(c); 
     } 
    } 
} 


recyclerView.addItemDecoration(new DividerItemDecoration(context)); 
+0

は魅力的に機能します!私はすべてのrecycleview要素が作成されていない場合でも、描画された行をすべて表示する必要があります。私は基本的にメモ帳ページを模倣する必要があります。どのようにして、リサイクルビューのリストを維持できますか?あなたの時間をありがとう! – Starivore

+0

bottom> = parent.bottom();まで分割線を引き続き最初のループの後に2番目のループを追加する必要があります。 – cyroxis

+0

bottom <= parent.getBottomを使用していただきありがとうございます! – Starivore

関連する問題