0
私は、シンプルなラインセパレータデコレーションを使用するRecyclerViewを持っています。RecyclerViewデコレーションが正しくスクロールしない
リサイクラの下部に新しいアイテムが追加され、スクロールされて表示される場合を除いて、装飾はうまくいきます。装飾は最初に最終位置に描画され、その下のビューが所定の場所にスクロールします。スクロール。
デコレーションが開始位置に描画され、次に新しいアイテムと共に位置にスクロールされることが望ましい動作です。
ここでは装飾の関連部分です:
class NormalDecoration extends RecyclerView.ItemDecoration {
private int spacing;
private Drawable drawable;
NormalDecoration(Context context) {
spacing = context.getResources().getDimensionPixelOffset(R.dimen.chat_separator_height);
drawable = ContextCompat.getDrawable(context, R.drawable.chat_divider);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (applySpacing(view, parent)) {
outRect.top += spacing;
}
}
boolean applySpacing(View view, RecyclerView parent) {
int position = parent.getChildAdapterPosition(view);
return position != -1 && position < mItems.size();
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int dividerLeft = parent.getPaddingLeft();
int dividerRight = parent.getWidth() - parent.getPaddingRight();
for(int index = parent.getChildCount() - 1 ; index >= 0 ; --index) {
View view = parent.getChildAt(index);
if(applySpacing(view, parent)) {
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
int dividerTop = view.getBottom() + params.bottomMargin;
int dividerBottom = dividerTop + spacing;
drawable.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom);
drawable.draw(c);
}
}
}
}
私のアダプタには、以下のヘルパー含まれています
class Adapter {
public void add(final int index, final IChatMessageItem item) {
mItems.add(index, item);
notifyItemInserted(index);
}
}
をそしてここで私は受け入れられないスクロール動作につながるリサイクル業者に項目を追加する方法は次のとおりです。
...
adapter.add(index, item);
layout.scrollToPosition(index);
...
私はいくつかの装飾のうちの1つを使用しますが、これは単なる最も簡単なものでした。しかし、それらはすべて同じ挙動を示す。私はその記事を見ていきます。 –
@DavidBerryまた、いくつかのサンプルデコレーションがあります:https://github.com/bleeding182/recyclerviewItemDecorations –
ありがとう!トリックは実際にアルファではなく、翻訳であるようですが、重要なのはそれが機能することです。 –