Android RecyclearView를 사용하여 데이터 2 표시

13313 단어 RecyclerViewAndroid

구분선 표시



DividerItem Decoration 클래스 추가
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 RecyclearView를 사용하여 데이터 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));
    }
독후감
다만 이 세 줄의 추기는 문제없을 것 같다.
DividerItem Decoration 클래스를 추가할 필요가 없습니다.
MainAcitivity.java
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
DividerItemDecoration mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), mLayoutManager.getOrientation());
recyclerView.addItemDecoration(mDividerItemDecoration);

좋은 웹페이지 즐겨찾기