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);
Reference
이 문제에 관하여(Android RecyclearView를 사용하여 데이터 2 표시), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/joji/items/2443cd6ed92dbfc286fc텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)