1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2017-01-29

区切り線を表示する

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からの変更箇所
追加

recyclerView.addItemDecoration(new DividerItemDecoration(this));
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);
1
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?