AndroidのRecyclerViewを使ってデータを表示する2


###区切り線を表示する

![Screenshot (2017_01_30 午前1_02_45).png](https://qiita-image-store.s3.amazonaws.com/0/39073/ddce8f37-621b-8428-dd56-9b8303864ab3.png "Screenshot (2017_01_30 午前1_02_45).png")


DividerItemDecorationクラスを追加

```DividerItemDecoration.java
package com.herokuapp.aqueous_spire_22637.recycler1;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    private final int dividerHeight;
    private Drawable divider;

    public DividerItemDecoration(Context context) {
        final TypedArray a = context.obtainStyledAttributes(new int[] {android.R.attr.listDivider});
        divider = a.getDrawable(0);

        dividerHeight = divider.getIntrinsicHeight();
        a.recycle();
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
//        super.onDrawOver(c, parent, state);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        final int lineLeft = parent.getPaddingLeft();
        final int lineRight = parent.getWidth() - parent.getPaddingRight();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++){
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int childTransitionY = Math.round(ViewCompat.getTranslationY(child));
            final int top = child.getBottom() + params.bottomMargin + childTransitionY;
            final int bottom = top + dividerHeight;

            divider.setBounds(lineLeft, top, lineRight, bottom);
            divider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.set(0, 0, 0, dividerHeight);
    }
}
```




AndroidのRecyclerViewを使ってデータを表示する1からの変更箇所
追加

```java
recyclerView.addItemDecoration(new DividerItemDecoration(this));
```


```java:MainAcitivity.java

    private void setupView(){
        recyclerView = (RecyclerView) findViewById(R.id.simple_recycler_view);

        simpleStringAdapter = new SimpleStringAdapter(DummyDataGenerator.generateStringListData());
        //ボタンを押した時のアクション用
        simpleStringAdapter.setOnItemClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(), "Position:" + recyclerView.getChildAdapterPosition(view) + "がクリックされました。", Toast.LENGTH_SHORT).show();
            }
        });

        recyclerView.setAdapter(simpleStringAdapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this));
    }
```


コメントを読んでの追記；
この3行の追記だけで、大丈夫みたいです。
DividerItemDecorationクラスを追加する必要はありませんでした。

```MainAcitivity.java
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
DividerItemDecoration mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), mLayoutManager.getOrientation());
recyclerView.addItemDecoration(mDividerItemDecoration);
```