Android - 사용자 정의 - RecyclerView.LayoutManager
11314 단어 Android
전체 설정:
public class TestLayoutManager extends RecyclerView.LayoutManager {
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return null;
}
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
// item,
if (getItemCount() <= 0) return;
// preLayout,preLayout
if (state.isPreLayout()) {
return;
}
// , View Detach , Scrap
detachAndScrapAttachedViews(recycler);
//
int offsetY = 0;
for (int i = 0; i < getItemCount(); i++) {
//
View view = recycler.getViewForPosition(i);
// View RecyclerView
addView(view);
measureChildWithMargins(view, 0, 0);
int width = getDecoratedMeasuredWidth(view);
int height = getDecoratedMeasuredHeight(view);
// , View
int left = (getWidth() - width) / 2;
layoutDecorated(view, left, offsetY, getWidth() - left, offsetY + height);
// height
offsetY += height;
}
}
실행 효과:
@Override
public boolean canScrollVertically() {
return true;
}
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
offsetChildrenVertical(dy);
return dy;
}
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
//
int travel = dy;
//
if (verticalScrollOffset + dy < 0) {
travel = -verticalScrollOffset;
} else if (verticalScrollOffset + dy > totalHeight - getVerticalSpace()) {//
travel = totalHeight - getVerticalSpace() - verticalScrollOffset;
}
// +travel
verticalScrollOffset += travel;
// item
offsetChildrenVertical(-travel);
return travel;
}
/**
* RecyclerView , padding
*
* @return
*/
private int getVerticalSpace() {
return getHeight() - getPaddingBottom() - getPaddingTop();
}
아직 Item 캐시를 하지 않은 것 외에 기본적으로 LinearLayoutManager와 효과가 많이 떨어지지 않았다.
전체 소스:
/**
* Created by Chao 2018/8/15 on 16:15
* description
*/
public class TestLayoutManager extends RecyclerView.LayoutManager {
private int totalHeight = 0;
private int verticalScrollOffset = 0;
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
// item,
if (getItemCount() <= 0) return;
// preLayout,preLayout
if (state.isPreLayout()) {
return;
}
// , View Detach , Scrap
detachAndScrapAttachedViews(recycler);
//
int offsetY = 0;
totalHeight = 0;
for (int i = 0; i < getItemCount(); i++) {
//
View view = recycler.getViewForPosition(i);
// View RecyclerView
addView(view);
measureChildWithMargins(view, 0, 0);
int width = getDecoratedMeasuredWidth(view);
int height = getDecoratedMeasuredHeight(view);
// , View
int left = (getWidth() - width) / 2;
layoutDecorated(view, left, offsetY, getWidth() - left, offsetY + height);
// height
offsetY += height;
totalHeight += height;
}
}
@Override
public boolean canScrollVertically() {
return true;
}
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
//
int travel = dy;
//
if (verticalScrollOffset + dy < 0) {
travel = -verticalScrollOffset;
} else if (verticalScrollOffset + dy > totalHeight - getVerticalSpace()) {//
travel = totalHeight - getVerticalSpace() - verticalScrollOffset;
}
// +travel
verticalScrollOffset += travel;
// item
offsetChildrenVertical(-travel);
return travel;
}
/**
* RecyclerView , padding
*
* @return
*/
private int getVerticalSpace() {
return getHeight() - getPaddingBottom() - getPaddingTop();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.